Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    12

    Effect of using eval to execute a command as opposed to writing it on the commandline

    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 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?

    Works correctly using eval though! How?

  2. #2
    Just 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:

    Code:
    cmd=`date | wc`
    $cmd
    So, "date" will output something like:

    Code:
    Sat Jun 19 01:59:59 BST 2010
    When this is passed down the pipe to "wc", the output, which will be assigned to the $cmd variable, will be:

    Code:
    1 6 29
    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.

    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!

  3. #3
    Just 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 :
    Code:
    cmd="date | wc"
    My question is, after doing the above, if I do this in a shell script :

    Code:
    $cmd
    It doesn't work when the script is executed, but when I write this :
    Code:
    eval $cmd
    it works. Why ?

  4. #4
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    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.

  5. #5
    Just 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.
    Code:
    cmd="date | wc"
    cmd
    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:
    eval 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.

    For my sake, I hope I am right !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...