Remote call awk shell from perl script
I am trying to execute commands on a remote host from a perl script.
$check = `rsh host1 "df -k /backup/|tail -1|awk \'{print \$5}\'"`;
print $check
But awk is returning me a whole row instead of one column.
/dev/md/dsk/d10 4133838 1936684 2155816 48% /
I just need to
48%
It looks like there is a problem in escaping, but don't know what exactly is wrong. Please, help.
source to share
One option is to use qx
a separate quote as a delimiter instead, so that Perl variables won't be interpolated:
$check = qx'rsh host1 "df -k /backup/|tail -1|awk \"{print \$5}\""';
I've used double quotes around the awk command, so we still need to exit $5
to prevent it from being interpreted as a shell variable.
If you are happy to run tail
and awk
on the local computer, you can remove the quotation marks:
$check = qx'rsh host1 df -k /backup/|tail -1|awk "{print \$5}"';
This results in more data being copied from the remote machine, which may or may not be a problem.
Alternatively, you can split the output in Perl and eliminate the problem entirely:
$check = (split /\s+/, qx'rsh host1 "df -k /backup/|tail -1"')[4];
source to share