AWKing an SSH command in bash script gives bad output

Clash Royale CLAN TAG#URR8PPP
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.
bash shell-script awk
add a comment |
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.
bash shell-script awk
add a comment |
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.
bash shell-script awk
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.
bash shell-script awk
bash shell-script awk
edited Dec 28 '18 at 10:48
msp9011
3,78343863
3,78343863
asked Dec 21 '18 at 20:29
saleetzo
196114
196114
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 triedechoin there instead but maybe I didn't write or something before testing. Thanks!
– saleetzo
Dec 21 '18 at 21:15
add a comment |
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:
$checkwill undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.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 %dn", $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
add a comment |
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
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490394%2fawking-an-ssh-command-in-bash-script-gives-bad-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 triedechoin there instead but maybe I didn't write or something before testing. Thanks!
– saleetzo
Dec 21 '18 at 21:15
add a comment |
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 triedechoin there instead but maybe I didn't write or something before testing. Thanks!
– saleetzo
Dec 21 '18 at 21:15
add a comment |
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.
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.
answered Dec 21 '18 at 20:58
John
11.5k11730
11.5k11730
Yup -- this worked. I swear I triedechoin there instead but maybe I didn't write or something before testing. Thanks!
– saleetzo
Dec 21 '18 at 21:15
add a comment |
Yup -- this worked. I swear I triedechoin there instead but maybe I didn't write or something before testing. Thanks!
– saleetzo
Dec 21 '18 at 21:15
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
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
add a comment |
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:
$checkwill undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.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 %dn", $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
add a comment |
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:
$checkwill undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.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 %dn", $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
add a comment |
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:
$checkwill undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.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 %dn", $NF, x) '
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:
$checkwill undergo field splitting on whitespace, turning it into a number of words. These will then undergo filename globbing if they contain filename globbing characters.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 %dn", $NF, x) '
edited Dec 21 '18 at 21:06
answered Dec 21 '18 at 20:59
Kusalananda
122k16229374
122k16229374
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Dec 28 '18 at 10:48
SouravGhosh
493311
493311
answered Dec 22 '18 at 11:08
Praveen Kumar BS
1,212138
1,212138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490394%2fawking-an-ssh-command-in-bash-script-gives-bad-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown