0

I am trying to fetch only column using awk in OS commands like uname -a , uptime, using pssh multiple commands, But it's not giving correct output. Using "uname -a" is just an example i have shown, I meant to say awk is not working even any of the command, I tried only particular field of uptime via awk but no luck. Please let me know, if there is any possibility..

My need is to place the commands in same line and Below is the command, I'm trying, which gives correct output. But Not with awk

pssh -h /tmp/hosts -i 'echo "$(echo -n);$(date);$(uname -a)"'
[1] 13:41:35 [SUCCESS] server1
;Sun Jan 27 12:41:35 UTC 2019;Linux server1 3.12.74-60.64.99-default #1 SMP Tue Aug 14 07:11:35 UTC 2018 (d28148a) x86_64 x86_64 x86_64 GNU/Linux
[2] 13:41:35 [SUCCESS] server2
;Sun Jan 27 12:41:35 UTC 2019;Linux server2 3.12.74-60.64.99-default #1 SMP Tue Aug 14 07:11:35 UTC 2018 (d28148a) x86_64 x86_64 x86_64 GNU/Linux

Below is the commands using with awk command is Not working

pssh -h /tmp/hosts -i 'echo "$(echo -n);$(date);$(uname -a|awk '{print \$3}')"'

[1] 13:54:54 [SUCCESS] server1
;Sun Jan 27 12:54:54 UTC 2019;
Stderr: awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string
[2] 13:54:54 [SUCCESS] server2
;Sun Jan 27 12:54:54 UTC 2019;
Stderr: awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string
1

You can see what's going wrong if you turn on bash shell debugging

$ set -x
$ parallel-ssh -h hostsfile -i 'echo "$(uname -a | awk '{print $3}')"'
+ parallel-ssh -h hostsfile -i 'echo "$(uname -a | awk {print' '})"'

So the invoking shell is splitting the command into two arguments:

echo "$(uname -a | awk {print

and

$3})"

(with $3 being expanded to an empty value). This is one of the (rare) cases where the usual "if in doubt, add quotes" doesn't help you - the double quotes around the command substitution in the echo aren't really necessary, and if you remove them you can get rid of one level of quote nesting. Then you can just do

$ parallel-ssh -h hostsfile -i 'echo $(date) $(uname -a|awk "{print \$3}")'
[1] 09:13:36 [SUCCESS] vm
Sun Jan 27 09:13:36 EST 2019 4.4.0-141-generic
[2] 09:13:37 [SUCCESS] localhost
Sun Jan 27 09:13:37 EST 2019 4.15.0-43-generic

where

  • single quotes around the whole pssh argument prevent the invoking shell from expanding and running the command substitutions locally

  • double quotes around the awk expression DON'T prevent $3 frombeing expanded by the remote shell, so backslash escape it

Alternatively, you can add literal single quotes around the awk expression by opening and closing the single quotes:

'echo $(date) $(uname -a|awk '\''{print $3}'\'')'
0

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.