0

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 ...)

3
  • 1
    What about it is "not working"? For one thing, to execute a command and use its result you want $(...) not just (...), and also watch out for "smart quotes" like it seems you might have in the first if since those don't parse right in bash, and also you need a space before the closing ], white space there is not optional. Also, look at elif instead of all those nested else if which would each need a closing fi Commented Dec 13, 2015 at 21:28
  • Among other problems, you seem to be assuming that the second and following comparisons will implicitly use the value from the first. They won't. Commented Dec 13, 2015 at 23:09
  • Ok I see what you mean. But how can I do that then? Commented Dec 14, 2015 at 15:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

WOW this is amazing! As you probably guessed I am pretty new with coding, so this is great to learn from. Now the only more thing I want is that it repeats this code automatically every 10 seconds. Online I found this: watch -n 10 num_mp3=$(airport en1 -s | awk 'END {print NR}') if [ "$num_mp3" -le 10 ] then open ~/Desktop/1.mp3 elif [ "$num_mp3" -le 14 ] then open ~/Desktop/11-14.mp3 elif [ "$num_mp3" -le 17 ] then open ~/Desktop/15-17.mp3 … fi but that does not work
I don't know what the watch command does (it isn't on my Mac). You'd probably need to put the assignment and conditional into a shell script (say ~/bin/run-mp3.sh if you have made yourself a bin subdirectory), make sure it is executable (chmod 755 ~/bin/run-mp3.sh) and then do something like watch -n 10 ~/bin/run-mp3.sh to run it. I don't have an airport command in my path either; maybe you're running the Airport Utility from the command line somehow — or maybe what you're running is wholly unrelated to the Mac's Airport networking.
@JonathanLeffler: watch(1) - execute a program periodically, showing output fullscreen. Use-cases are similar to cases where you'd use tail -f on a log file, but it shows the whole output every time.
0

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

The if statements all need valid stand-alone commands

3 Comments

How do I do that if I already have a counting command? $(airport en1 -s | awk 'END{print NR--}')
@shirin sorry for the lame answer, trying out posting from my mobile. Capture into a variable instead of the first if
No problem though, I appreciate any help. I am trying to put the scaning and counting into a variable s='airport en1 -s |awk 'END{print NR--}'' as soon as I click enter I get an error zsh: command not found: NR--}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.