0

I am trying to pull information from a simple bash script that pulls some data from a java server. When the SSH variable is run in the script, I can't seem to run awk on it to pull out the information I need unless I write it to a local file. Here's what I am working with:

check=$(ssh user@somesystem.com "/usr/local/bin/check_mq.sh")
ret=$?
cat=/bin/cat
awk=/usr/bin/awk
output=$($cat $check | $awk -F= '{print $4}' | $awk '{print $1}')
echo $output

If I just echo $check in the script it comes up like this.

backchannel_queue messages=0 messages_ready=0 messages_unacknowledged=0 

But, if I put $check in the script with the awk commands it gives me this output:

/bin/cat: backchannel_queue: No such file or directory
/bin/cat: 'messages=0': No such file or directory
/bin/cat: 'messages_ready=0': No such file or directory
/bin/cat: 'messages_unacknowledged=0': No such file or directory

I have been able to get this working by writing $output to a text file on the system with:

 check_output="/home/user/Documents/scratch/check_output.txt"
 echo $check > $check_output
 output=$($cat $check_output | $awk -F= '{print $4}' | $awk '{print $1}')
 echo $output

I am thinking there has to be something I am doing wrong here and I shouldn't need to write $output to a file. Or, is that the only option here? Once I have that number in a variable I plan to use some IF statements to see if it's greater than x, etc.

2

You're telling it to cat a list of files, that list actually being the thing you want to it operate on, not files. You want to use echo instead of cat to do that.

  • Yup -- this worked. I swear I tried echo in there instead but maybe I didn't write or something before testing. Thanks! – saleetzo Dec 21 '18 at 21:15
2

Don't store the paths to the commands you're using in variables, it's totally unnecessary. If you are using tools under non-standard paths (which you don't do), modify $PATH instead.

You are using cat $check. Two things to note about this:

  1. $check will undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.

  2. The resulting words will be given to cat, which expects them to be filenames. This is where your error comes from.

Instead, just pipe the ssh to awk directly:

ssh user@somesystem.com "/usr/local/bin/check_mq.sh" |
awk -F '=' '{ print $NF }'

This assumes you want to output the number after the last = on the line.

If you want to test whether this number is greater than some variable $x:

ssh user@somesystem.com "/usr/local/bin/check_mq.sh" |
awk -F '=' -v x="$x" '$NF > x { printf("%d is more than %d\n", $NF, x) }'
  • Looking for the output of the number behind the first = in there. It does make sense to keep it all in one line. I like your idea, I just need to work on it to make it work for this. Thanks – saleetzo Dec 21 '18 at 22:18
0

Use echo instead of cat since your $check is a variable. Use as below

check=$(ssh user@somesystem.com "/usr/local/bin/check_mq.sh")
ret=$?
cat=/bin/cat
awk=/usr/bin/awk
output=$(echo $check | $awk -F= '{print $4}' | $awk '{print $1}')
echo $output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.