Results 1 to 5 of 5
Code:
cmd='date | wc'
$cmd
If this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is ...
- 06-18-2010 #1Just Joined!
- Join Date
- May 2010
- Posts
- 12
Effect of using eval to execute a command as opposed to writing it on the commandline
If this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is passed to date as an argument". What is meant by expansion of pipe. When we execute date | wc on the command line, it goes fine. then | is not treated as an argument. Why?Code:cmd='date | wc' $cmd
Works correctly using eval though! How?
- 06-19-2010 #2Just Joined!
- Join Date
- Feb 2006
- Location
- Cambridge, UK
- Posts
- 7
Well, for a start, you're using single-quotes instead of back-quotes (also known as back-ticks, and often found on the key to the left of the (number) '1' key).
Secondly, attempting to execute $cmd won't work, as $cmd will contain three numbers: the numbers of lines, words and characters, respectively, in the date output.
The fixed version of your code is:
So, "date" will output something like:Code:cmd=`date | wc` $cmd
When this is passed down the pipe to "wc", the output, which will be assigned to the $cmd variable, will be:Code:Sat Jun 19 01:59:59 BST 2010
Then you attempt to execute $cmd, which will try to run the "1" command with two arguments ("6" and "29"). Since there is no "1" command on most Linux boxes, this will fail.Code:1 6 29
It's not obvious what you're trying to achieve in this example, since the date command always outputs 29 characters (including the newline) in six "words" on one line, unless you give it command arguments. See the date(1) man page for more details.
It looks to me as if you're new to writing shell scripts. I'd strongly recommend reading a good primer on "bash", or internet tutorials, and work through the examples. Otherwise, you're going to be fighting a lot of needless battles getting to grips with scripting!
- 06-19-2010 #3Just Joined!
- Join Date
- May 2010
- Posts
- 12
Thanks, but I did not use backquotes for a purpose. The use of back quotes is to give the result of the command contained inside them. I wanted to assign the string date | wc to cmd and then execute cmd. I could have done this :
My question is, after doing the above, if I do this in a shell script :Code:cmd="date | wc"
It doesn't work when the script is executed, but when I write this :Code:$cmd
it works. Why ?Code:eval $cmd
- 06-23-2010 #4
The best way I can describe the concept is that Bash only interpolates variables on the commandline. If Bash were to interpolate and then execute variables, then how would you ever simply acquire the value of the variable? I know this answer is a bit obtuse, but its the best I can do to explain a bit of a confusing concept.
--- rod.Stuff happens. Then stays happened.
- 06-23-2010 #5Just Joined!
- Join Date
- May 2010
- Posts
- 12
I understand that substituting variables with their values occurs after the special meaning of symbols like |,$$,etc. has been explained to the shell.
When cmd is executed, the shell doesn't find any special characters (to interpret them as special) but only a variable cmd which has to be substituted. Therefore, it goes about with the substitution, and foregoes assigning special meaning to the special symbols. Now, | is treated literally and assumed to be an argument to date, and hence error is generated.Code:cmd="date | wc" cmd
eval first parses the argument to eval without executing it on the command line, then again parses it, but this time executes it too. So, for the first parse, the same thing happens as above. When the second parse starts, the shell again looks for special characters. This time, since cmd has already been substituted by "date | wc", it does find | as a special character and assigns it its special status (by probably inserting some code which treats it specially). Thereafter, its executed.Code:eval cmd
For my sake, I hope I am right !


Reply With Quote