0

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!

2 Answers 2

1

BASH FAQ entry #50:

I'm trying to put a command in a variable, but the complex cases always fail!

Variables hold data. Functions hold code. Don't put code inside variables! There are many situations in which people try to shove commands, or command arguments, into variables and then run them. Each case needs to be handled separately.

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

6 Comments

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?
along the lines of what you have said... this works: a="nc" b="shldvgfas005" c="6153" d="temp.6153"; $a $b $c < $d
The big thing that's confusing it is the redirection. Take that out of the variable.
Ah you are right, this works: cmd="nc shldvgfas005 6157" ; $cmd < temp.6153 Can 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?
You can run it with bash -x to see what it's doing at each step.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.