5
#!/bin/bash
until [read command -eq "end"]
do
echo What command would like to run?
read command
if [$command -eq "my-tweets"]; then
node liri.js $command
fi
if [$command -eq "do-what-it-says"];then
node liri.js $command
fi
if [$command -eq "spotify-this-song"]; then
echo What item would like to query?
read item
node liri.js $command $item
fi
if [$command -eq "movie-this"]; then
echo What item would like to query?
read item
node liri.js $command $item
fi
done

I am trying to create a case/if statement to check the value of a variable before running the next part of the code. I want to check the value of $command to create this case/if statement according to the value of the user input. I keep getting the command not found error.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Zac Saltzman
  • 51
  • 1
  • 3

3 Answers3

3

In addition to the syntax errors @PSkocik pointed out, when you have a number of mutually exclusive if conditions, it's generally clearer/better to use if ... elif... instead of a bunch if separate if blocks:

if [ "$command" = "my-tweets" ]; then
    node liri.js "$command"

elif [ "$command" = "do-what-it-says" ];then
    node liri.js "$command"

elif [ "$command" = "spotify-this-song" ]; then
...etc

But when you're comparing a single string ("$command") against a bunch possible strings/patterns, case is an even clearer way to do it:

case "$command" in
    "my-tweets")
        node liri.js "$command" ;;

    "do-what-it-says")
        node liri.js "$command" ;;

    "spotify-this-song")
...etc
esac

Additionally, when several different cases all execute the same code, you can include multiple matches in a single case. Also, it's a good idea to include a default pattern to deal with strings that don't match anything else:

case "$command" in
    "my-tweets" | "do-what-it-says")
        node liri.js "$command" ;;

    "spotify-this-song" | "movie-this")
        echo What item would like to query?
        read item
        node liri.js "$command" "$item" ;;

    *)
        echo "Unknown command: $command" ;;
esac

And as for the loop: generally, you'd either use something like while read command; do (note the lack of [ ], because we're using the read command, not the test aka [ command); or just use while true; do read ... and then check for the end condition and break out from inside the loop. Here, it's probably best to do the latter:

while true; do
    echo "What command would like to run?"
    read command
    case "$command" in
        "my-tweets" | "do-what-it-says")
            node liri.js "$command" ;;

        "spotify-this-song" | "movie-this")
            echo What item would like to query?
            read item
            node liri.js "$command" "$item" ;;

        "end")
            break ;;

        *)
            echo "Unknown command: $command" ;;
    esac
done
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Thank you so much. This was a great crash course into exactly what I was looking for. The code is working perfectly right now. – Zac Saltzman Aug 15 '17 at 22:13
2

The brackets need spaces around them. [ ] isn't a shell language feature, [ is a command name that requires a closing ] argument to make things look pretty ([read will search for a command (executable or builtin) literally named [read).

String comparison inside [ ] is done with =, -eq is for integer comparison.

You should peruse the dash(1) manpage or the POSIX shell language specification. They're not that big (Bash is bigger). You'll find the syntax for the case statement in there too.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 2
    Also, double-quote all variable references. `if [ $command = "my-tweets" ]` will give an error if `$command` is blank or contains multiple words or... several other conditions. `if [ "$command" = "my-tweets" ]` will work. – Gordon Davisson Aug 15 '17 at 17:33
  • 1
    Thank you so much. This really helped. I was able to quickly get my code to evaluate whether a command was equal to a set value and go from there. – Zac Saltzman Aug 15 '17 at 22:11
0

Simple usage of case in bash based on a argument.

case "$1" in
    argument1)
        function1()
        ;;

    argument2)
        function2()
        ;;  
    *)
        defaultFunction()
        ;;  

esac
nagendra547
  • 5,672
  • 3
  • 29
  • 43