I am creating a script where the user must provide the first argument that is mandatory and the second argument is optional. Should throw an error if it is less than 1 or bigger than 2 arguments.
This is what I did so far:
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
The issue is in the first conditional if [ $# -eq 0 -o $# -gt 2]
. The condition is if arguments is zero OR great than 2 throw the error.
The error message I get is
./myserver.sh: line 32: [: missing `]'
./myserver.sh: line 50: [: -eq: unary operator expected
./myserver.sh: line 58: [: -eq: unary operator expected
What am I doing wrong?