[HPADM] SUMMARY: Run command set in a variable

From: Edi CAHYADI (edi.cahyadi@st.com)
Date: Tue Aug 29 2006 - 21:04:58 EDT


Thanks to Andrew J Hoffman, Dan Jonahsson, Eef Hartman, David Lodge, Shyam Hazari, Bill Thompson, Bill Hassell, Jeff Waugh, Illgen Steve, Corne Beerse and Vidal Cazarez for the replies.

"eval" does the trick. So, instead of running "$cmd" directly, you run "eval $cmd"
# cmd="mount | grep /app"
# eval $cmd
will get expected result.

Here's an excerpt from "Learning the Korn Shell' from the O'Reilly bookshelf (sent by Bill Thomson):

-----------------------------------------------------
The eval statement tells the shell to take eval's arguments and run them through the command-line processing steps all over again. To help you understand the implications of eval, we'll start with a trivial example and work our way up to a situation in which we're constructing and running commands on the fly.

eval ls passes the string ls to the shell to execute; the shell prints list of files in the current directory. Very simple; there is nothing about the string ls that needs to be sent through the command-processing steps twice. But consider this:

      listpage="ls | more"
      $listpage

Instead of producing a paginated file listing, the shell will treat | and more as arguments to ls, and ls will complain that no files of those names exist. Why? Because the pipe character "appears" in step 5 when the shell evaluates the variable, after it has actually looked for pipe characters (in step 2). The variable's expansion isn't even parsed until step 8. As a result, the shell will treat | and more as arguments to ls, so that ls will try to find files called | and more in the current directory!

Now consider eval $listpage instead of just $listpage. When the shell gets to the last step, it will run the command eval with arguments ls, |, and more. This causes the shell to go back to Step 1 with a line that consists of these arguments. It finds | in Step 2 and splits the line into two commands, ls and more. Each command is processed in the normal (and in both cases trivial) way. The result is a paginated list of the files in your current directory.
-----------------------------------------------------

Regards,
Edi

-----Original Message-----
From: hpux-admin-owner@DutchWorks.nl [mailto:hpux-admin-owner@DutchWorks.nl] On Behalf Of Edi CAHYADI
Sent: Tuesday, August 29, 2006 12:53 PM
To: hpux-admin@DutchWorks.nl
Subject: [HPADM] Run command set in a variable

Hi All,

We can set a command in a variable, for example:
# cmd=mount
Or
# cmd="ps -ef | grep root"
And
Run it:
# $cmd

But don’t know why the following variable setting has error when run:
# cmd="mount | grep /app"
# $cmd
usage: mount [-l][-v|-p]
        mount [-F FStype][-eQ] -a
        mount [-F FStype][-eQrV][-o specific_options]
                { special | directory }
        mount [-F FStype][-eQrV][-o specific_options]
                special directory

# cmd="for x in 1 2 3; do echo $x; done"
# $cmd
sh: for: not found.

Can anybody help?

Regards,
Edi

--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)
--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:54 EDT