tags:

views:

28

answers:

2

this works:

nc shldvgfas005 6155 < temp.6153 

this hangs:

cmd="nc shldvgfas005 6153 < temp.6153"; $cmd

this gives an error:

cmd="nc shldvgfas005 6153 < temp.6153"; "$cmd"

-bash: nc shldvgfas005 6153 <  temp.6153: command not found

HELP!

+1  A: 

BASH FAQ entry #50

Ignacio Vazquez-Abrams
i need a little more info. I get what you are saying that there is some subtle difference between the command executing from the variable and it is due to the expansions that bash does. So what is being expanded that is confusing bash?
johnnycrash
along the lines of what you have said... this works:a="nc" b="shldvgfas005" c="6153" d="temp.6153"; $a $b $c < $d
johnnycrash
The big thing that's confusing it is the redirection. Take that out of the variable.
Ignacio Vazquez-Abrams
Ah you are right, this works: cmd="nc shldvgfas005 6157" ; $cmd < temp.6153Can I ask...what is bash seeing when it gets the variable. I didn't get that from the #50 you linked me to. It just said the word breaks were confusing it. Is there any way to see the expanded text that bash is attempting to execute?
johnnycrash
You can run it with `bash -x` to see what it's doing at each step.
Ignacio Vazquez-Abrams
bash processes redirects before expanding variables; this means that when it sees `<` in the expansion of `$cmd`, it doesn't see it as a redirect, just another parameter to `nc`. Essentially, `$cmd` is equivalent to the command `nc 'shldvgfas005' '6153' '<' 'temp.6153'`
Gordon Davisson
+1  A: 

this works:

nc shldvgfas005 6155 < temp.6153

this hangs:

cmd="nc shldvgfas005 6153 < temp.6153"; $cmd

The difference you see here is due to the shell parsing redirection operators before parameter expansions.

this gives an error:

cmd="nc shldvgfas005 6153 < temp.6153"; "$cmd"

-bash: nc shldvgfas005 6153 <  temp.6153: command not found

This command fails because the quotes around the parameter expansion prevent it from being split into multiple fields. The shell tries to find a single executable named nc shldvgfas005 6153 < temp.6153 (i.e. there are embedded spaces in the filename), but fails to find any such file. Also, the redirection does not happen due to the same reason as the first failure.

For the details of your shell’s parsing, consult its manual (or that of a related shell; possibly the POSIX Shell Command Language specification). In general, all Bourne-like shells parse redirections before expansions. So the redirection operator can not be part of a variable (though the redirection source/target (filename) can be a variable). Some folks like to use eval to make stuff like this work, but it is a bad idea unless you are fully aware of the security implications.

Chris Johnsen