Expr Variable Help

I am attempting to build some if variables into my dialplan and am having trouble with two statements that I need to add on. I am sure it is escape keys, but I am not sure what exactly is missing. Is anyone able to lend a hand? The script below grabs a JSON result with multiple values, then returns one selected value.

On the Linux command line:
curl -s ‘https://some.JSON.output’ | python -c "import sys, json; print json.load(sys.stdin)[‘AUX’]"
It works. It returns a value of 10.

when I try:
expr curl -s ‘https://some.JSON.output’ | python -c “import sys, json; print json.load(sys.stdin)[‘AUX’]”

or

expr 1 + curl -s ‘https://some.JSON.output’ | python -c “import sys, json; print json.load(sys.stdin)[‘AUX’]”

I get:
expr: syntax error
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python2.6/json/init.py”, line 267, in load
parse_constant=parse_constant, **kw)
File “/usr/lib/python2.6/json/init.py”, line 307, in loads
return defaultdecoder.decode(s)
File “/usr/lib/python2.6/json/decoder.py”, line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.6/json/decoder.py”, line 338, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

Any ideas? Much appreciated!

I also had one more in a similar situation. The script counts the total number of processes with callback in the path.

On the Linux command line:
ps axo pid,etime,cmd|grep -v queue|egrep ‘callbac[k]|PI[D]’|wc -l
It works. It returns a value of 4.

when I try:
expr ps axo pid,etime,cmd|grep -v queue|egrep ‘callbac[k]|PI[D]’|wc -l

or

expr 1 + ps axo pid,etime,cmd|grep -v queue|egrep ‘callbac[k]|PI[D]’|wc -l

I get:
expr: syntax error
0

and

expr: non-numeric argument
0

respectively.

you need to wrap your bashisms in $(. . .)

eg

expr 1 + $(ps axo pid,etime,cmd|grep -v queue|egrep ‘callbac[k]|PI[D]’|wc -l)

also

echo $(( $(ps axo pid,etime,cmd|grep -v queue|egrep ‘callbac[k]|PI[D]’|wc -l) + 1 ))

might give a better spin on bash math than expr

(you also need to find a good bash scripting tutorial :wink:

1 Like