-1

I have a command which uses awk that I am trying to run remotely via SSH.

This is the command and a sample output when it is run locally:

df -h -P | awk '{print $1,$2,$3,$5,$6}'

Filesystem Size Used Use% Mounted
/dev/mapper/volgroup00-lv_root 3.9G 2.7G 74% /
tmpfs 2.9G 0 0% /dev/shm
/dev/mapper/volgroup00-lv_seemis 4.4G 3.6G 87% /STORE
/dev/sda1 477M 195M 44% /boot
/dev/sdb1 30G 26G 90% /data

The output of that command is then processed by the rest of my script. However, when I try to run the command remotely it produces an error. Here is an example of the remote command and the error it produces (where $servername is the FQDN of the server and $user is the SSH user):

timeout 10s ssh -n $user@$servername "df -h -P | awk '{print $1,$2,$3,$5,$6}'"


awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:        ^ syntax error
awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:         ^ syntax error
awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:          ^ syntax error
awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:           ^ syntax error
awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:            ^ syntax error
awk: cmd. line:1: {print ,,,,}
awk: cmd. line:1:             ^ unexpected newline or end of string

I feel that this has something to do with the use of $1, $2 etc. when running the command remotely. How can I get around this error that awk is producing?

0

1 Answer 1

0

Adding a back slash before each $NUM variable fixed the problem:

timeout 10s ssh -n $user@$servername "df -h -P | awk '{print \$1,\$2,\$3,\$5,\$6}'"

The command returned what was expected.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.