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