Bryan
Bryan

Reputation: 23

asterisk: command not found

I'm trying to run the following script...

#!/bin/bash -x
[email protected]

X='asterisk -rx "show channels" | grep -c Zap/'
if [$X -ge 4]; then
echo "Active Calls: $X" |
mail -s "Active Calls: $X" $ADMIN
fi

and get this error "line 5: [asterisk: command not found"

I'm really new to this but understand it's probably a path problem. However from the same directory that I'm running the script from I can type out the 'asterisk -rx "show cha...' command and it works fine. So don't understand why the shell script can't do the same? Thanks!

Upvotes: 2

Views: 3134

Answers (1)

SiegeX
SiegeX

Reputation: 140257

Your problem is two-fold

  1. This line: X='asterisk -rx "show channels" | grep -c Zap/'. You want to use command substitution for this via $()
  2. This line: if [$X -ge 4]; then. The [ is actually not syntax but a call to a binary named [ which is the same as the test binary. Therefor you must put a space after the [ otherwise the shell will complain as you have seen.

.

#!/bin/sh -x
ADMIN="[email protected]" # don't forget to quote this

X=$(asterisk -rx "show channels" | grep -c Zap/)
if [ "$X" -ge 4 ]; then # don't forget the spaces
  echo "Active Calls: $X" |
  mail -s "Active Calls: $X" $ADMIN
fi

Note that if you are going to use bash you might as well use its nicer syntax:

#!/bin/bash -x
ADMIN="[email protected]" # don't forget to quote this

X=$(asterisk -rx "show channels" | grep -c Zap/)
if ((X > 4)); then # much nicer syntax
  echo "Active Calls: $X" |
  mail -s "Active Calls: $X" $ADMIN
fi

Upvotes: 2

Related Questions