RECOMMENDATIONS Storing a CLI output to a variable

Stewart

Guru
Joined
Sep 16, 2009
Messages
603
Reaction score
6
I'm trying to write some scripts that allow greater logging and notification but for some reason can't get my commands to store the results in a variable. Running:
Code:
asterisk -rx "core show uptime"
results in:
System uptime: 37 minutes, 23 seconds
Last reload: 37 minutes, 23 seconds

But if I run either
Code:
VAR1=$(/usr/sbin/asterisk -rx "core show uptime") | echo $VAR1
or
Code:
VAR1=$(/usr/sbin/asterisk -rx "core show uptime" | grep uptime) | echo $VAR1
I get what seems to be a blank line.

Any ideas as to what I'm doing wrong?
 
Replace the pipe into "echo" with a semi-colon.

Code:
VAR1=$(/usr/sbin/asterisk -rx "core show uptime") ; echo $VAR1
 
My guess is that the programming scope of this is likely as this:

Statement 1 | Statement 2

Statement 1 may use variables but Statement 2 would not "see" them as such. I did not know about ;

I'm just throwing my two-cents around.
 
OK. So now I've come to the point that I have the scripts all running, but not when called from the web interface. So:
I have a button on a page that edits a text file. - This works.
I have a cron job that runs every minute and checks the file. - This works.
This cron job then launches a script to restart asterisk services. - This works BUT the line
Code:
  /usr/sbin/amportal stop gracefully
doesn't do anything.

If I run the script manually it works so I'm thinking either permissions or pathing issues. It's being called by cron so maybe it doesn't have the permissions? Any ideas? Should be simple I guess. Thanks.
 

Members online

Forum statistics

Threads
26,687
Messages
174,409
Members
20,257
Latest member
Dempan
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Back
Top