-1

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?

IgorAlves
  • 5,086
  • 10
  • 52
  • 83
  • 1
    You need a space before the closing bracket. – MrTux Nov 22 '17 at 15:29
  • I recommend you use getopts instead parse the arguments manually. GetOpts Tutorial: https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/ – A.Villegas Nov 22 '17 at 15:30
  • Flagging to close as *This question was caused by a problem that can no longer be reproduced or **a simple typographical error***. – iBug Nov 22 '17 at 15:32
  • getOpts is a good option. I didn't know that! I will change the code later – IgorAlves Nov 22 '17 at 15:37
  • 2
    use https://www.shellcheck.net/ to spot such issues easily as well as to get suggestions on potential issues – Sundeep Nov 22 '17 at 15:41

1 Answers1

4

Go get a space here:

if [ $# -eq 0 -o $# -gt 2 ]
                         ^

Bash separates arguments by whitespaces, specifically, spaces and tabs. It will be treated as a single argument if written as 2].

iBug
  • 35,554
  • 7
  • 89
  • 134