views:

55

answers:

3

I want the user to input something at the command line either -l or -e. so e.g. $./report.sh -e I want an if statement to split up whatever decision they make so i have tried...

if [$1=="-e"]; echo "-e"; else; echo "-l"; fi

obviously doesn't work though Thanks

+3  A: 

I use:

if [[ "$1" == "-e" ]]; then
    echo "-e"
else
    echo "-l";
fi

However, for parsing arguments, getopts might make your life easier:

while getopts "el" OPTION
do
     case $OPTION in
         e)
             echo "-e"
             ;;
         l)
             echo "-l"
             ;;
     esac
done
David Wolever
You say "getopt" and "getopts". They're two different things. The former is a separate executable and the latter is a shell builtin.
Dennis Williamson
D'oh - fixed. Thanks.
David Wolever
+1  A: 

If you want it all on one line (usually it makes it hard to read):

if [ "$1" = "-e" ]; then echo "-e"; else echo "-l"; fi
Dennis Williamson
A: 

You need spaces between the square brackets and what goes inside them. Also, just use a single =. You also need a then.

if [ $1 = "-e" ]
then
   echo "-e"
else
   echo "-l"
fi

The problem specific to -e however is that it has a special meaning in echo, so you are unlikely to get anything back. If you try echo -e you'll see nothing print out, while echo -d and echo -f do what you would expect. Put a space next to it, or enclose it in brackets, or have some other way of making it not exactly -e when sending to echo.

Phil