-1
私は、必須の第1引数をユーザーが指定しなければならないスクリプトを作成しています。第2引数はオプションです。引数が1より小さいか、引数が2より大きい場合は、エラーをスローする必要があります。引数が与えられ、BASHで2未満であるかどうかをテストするには?
if [ $# -eq 0 -o $# -gt 2]
then
echo " *** ! No arguments were supplied. *** !"
echo " Usage example is: sudo myserver pathToYourFolder [URL]"
echo ""
echo " The first argument 'pathToYourFolder' is mandatory.
It is the path to your mysite folder.
Please use like this example: sudo myserver /Users/jhon/Documents/mysite"
echo ""
echo " The second argument 'URL' is optional. It shuld be the desired URL to run with the server in Docker.
If not provided the default will be 'my-dev.com'.
If you want to set yours, please use llike this example: my-url.com.
Please note: It will be recorded in your /etc/hosts."
echo ""
else
if [ $1 -eq 0 ]
then
FULLPATH="/Users/jhon/Documents/mysite"
else
FULLPATH="$1"
fi
if [ $2 -eq 0 ]
then
DOMAIN="my-dev.com"
else
DOMAIN="$2"
fi
echo ""
echo "Sit path to $FULLPATH"
echo ""
echo "HTTP_PORT to $HTTP_PORT"
echo ""
echo "Sit domain to $DOMAIN"
echo ""
if ! grep -lq $DOMAIN /etc/hosts;
then
echo "127.0.0.1 $DOMAIN" >> /etc/hosts
echo "$DOMAIN saved in /etc/hosts"
else
echo "$DOMAIN already in /etc/hosts"
fi
echo ""
echo ""
docker run -p 80:80 -v $FULLPATH:/var/www/html myorg/srv_php56:v001
fi
問題は、最初の条件付きif [ $# -eq 0 -o $# -gt 2]
である:
これは私がこれまで何をしたかです。引数が0の場合、または2より大きい場合は、エラーがスローされます。
私が取得エラーメッセージは、私が間違って何をやっている
./myserver.sh: line 32: [: missing `]'
./myserver.sh: line 50: [: -eq: unary operator expected
./myserver.sh: line 58: [: -eq: unary operator expected
のですか?
閉じる括弧の前にスペースが必要です。 – MrTux
代わりにgetoptsを使用して、引数を手動で解析することをお勧めします。 GetOptsチュートリアル:https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/ –
閉じるようにフラグを立てる*この質問は、もはや再現できない問題や簡単な誤植***。 – iBug