shirinmalina
shirinmalina

Reputation: 33

Bash command (if statement) Terminal

I have the following question:

if [ (airport en1 -s|awk 'END{print NR—}’) -lt 11]; then
    open  ~/Desktop/1-10.mp3
else if -ge 10 -lt 15
    then ~/Desktop/11-14.mp3
else if -ge 14 -lt 18
    then ~/Desktop/15-17.mp3
else if -ge 17 -lt 21
    then ~/Desktop/18-20.mp3
else if -ge 20 -lt 24
    then ~/Desktop/21-23.mp3
else if -ge 23 -lt 27
    then ~/Desktop/24-26.mp3
else if -ge 26 -lt 30
    then ~/Desktop/24-27.mp3
else if -ge 29 -lt 36
    then ~/Desktop/30-36.mp3
else if -ge 35 -lt 41
    then ~/Desktop/36-40.mp3
fi

Basically I want it to scan how many wifis there are Airport en1 -s) Then I let it count without the first line |awk 'END{print NR—}’ and then I have a series of if commands which I all need to get a more sensitive result. But it is not working right now.

Can anybody help me out please? (and yes I already read a bash tutorial ...)

Upvotes: 0

Views: 1688

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

There are quite a lot of issues even in this much of the code (the remainder just repeats the problems shown here):

if [ (airport en1 -s|awk 'END{print NR—}’) -lt 11]; then
    open  ~/Desktop/1-10.mp3
else if -ge 10 -lt 15
    then ~/Desktop/11-14.mp3
  • The ] is a separate argument to the [ command; spacing in the shell is crucial.

  • The should be a single quote (apostrophe) ' (noted by Eric Renouf in his comment).

  • The else if should be spelled elif. As written, you'll need an extra fi for each else if.

  • The system probably does not provide a command -ge which you're trying to run.

  • The second and subsequent commands are not prefixed by open.

  • And in the first line, the ( is wrong as written. It looks as though you had in mind something like:

    if [ $(airport en1 -s | awk 'END {print NR}') -lt 11 ]; then
    

    This runs a command airport with two arguments and sends the output to awk which prints the number of lines it read.

  • Using awk to count lines instead of wc -l might be regarded as overkill. It will, however, work.

  • You appear to have an em-dash after the NR; that probably isn't valid at all. I'm not sure if you intended a -- (double dash) or something else. A -- would probably be syntactically valid, but functionally pointless (there's no point in post-decrementing a number if you'll never use it again).

  • Your boundary conditions are a bit suspect too.

  • The erratic groupings (10, 4, 3, 3, 3, 3, 4, 6, 5) are a little odd, too. Note that you attempt to run 24-27.mp3 for what might be expected to be 27-30.mp3, and you run 30-36.mp3 for what might be expected to be 31-36.mp3.

Maybe you really need:

num_mp3=$(airport en1 -s | awk 'END {print NR}')
if [ "$num_mp3" -le 10 ]
then open ~/Desktop/1-10.mp3
elif [ "$num_mp3" -le 14 ]
then open ~/Desktop/11-14.mp3
elif [ "$num_mp3" -le 17 ]
then open ~/Desktop/15-17.mp3
…
fi

Note that the testing won't reach the elif if the number is less than or equal to 10, so there's no need to repeat the boundary test in that condition.

Upvotes: 2

Peter Cordes
Peter Cordes

Reputation: 364039

Capture the output into a variable so you can do different tests on it

The if statements all need valid stand-alone commands

Upvotes: 0

Related Questions