I'm writing a bash script and I have a function that gets 3 arguments, a hostname, a command, and a file, it should excute the command on that hostname and redirect the output to the filename. This is the fucntion:
execmd ()
{
P_SERV="$1"
P_CMD="$2"
P_OUT="$3"
X_SERV=`hostname`
if [ "$P_SERV" = "$X_SERV" ] ; then
$P_CMD >> $P_OUT
else
ssh $P_SERV $P_CMD >> $P_OUT
fi
}
When I execute:
execmd venus "cat /proc/meminfo" /tmp/meminfo
I get the error
cat /proc/meminfo: no such file or directory
any idea why its not working? same behavior either if call it with local hostname or remote hostname.
cat /proc/meminfo
with no arguments, notcat
with the argument/proc/meminfo
. – Satō Katsura Sep 13 '16 at 19:55$P_CMD
isn't quoted, as in the code in the question, it should get word-split when the script runs the command. That should make your examplecat
command work as expected, and indeed the function works for me withexecmd $myhostname "cat /proc/meminfo" output
. – ilkkachu Sep 14 '16 at 08:06cat
on the terminal, since stderr isn't redirected, so withexecmd $myhostname "cat /proc/meminfox" output
I getcat: /proc/meminfox: No such file or directory
. Looking closely at your error message, I think it might come fromcat
, but I suspect it's not copied correctly here: usually theNo
would be with an uppercaseN
. – ilkkachu Sep 14 '16 at 08:10bash: catxx: command not found
. Though an older bash (3.1.17 tested) might scramble that if the command name contained a carriage return, but the error is stillcommand not found
. – ilkkachu Sep 14 '16 at 08:13/proc/meminfo
actually exists on the host that you are connecting to. I'm voting to close this as off-topic (typo/problem went away) until such time that the question is clarified about the existence of the file. – Kusalananda Mar 07 '19 at 14:08