Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple commands in a variable

Status
Not open for further replies.

ayjaycee

Technical User
Mar 5, 2001
291
0
0
GB
OK, this one is doing my head in X-)

I want to pass (possible) multiple commands to a script, for it to execute. I'm using an option/value syntax :- "-c "command ; command ; command" to set a variable which I then try to execute later on.

Unfortunately, only the 1st command in the string is run & the rest are ignored. I've tried the following test to see what's happening :-

Code:
# cat test
#!/bin/sh
cmd="pwd ; uname -a"
pwd ; uname -a
$cmd
`$cmd`
which results in :-
Code:
# sh -vx ./test
#!/bin/sh
cmd="pwd ; uname -a"
cmd=pwd ; uname -a

pwd ; uname -a
+ pwd
/usr/local/bin/sysadm
+ uname -a
SunOS ncwcs36 5.5.1 Generic_103640-31 sun4d sparc SUNW,SPARCserver-1000

$cmd
+ pwd ; uname -a
/usr/local/bin/sysadm

`$cmd`
+ pwd ; uname -a
+ /usr/local/bin/sysadm
./test: /usr/local/bin/sysadm: cannot execute
so, issuing command ; command works OK,
issuing $cmd just runs the 1st command &
issuing `$cmd` runs the 1st command & generates an error msg :-0

Any ideas/hint/tips on this? TandA

Day by day, the penguins steal my sanity.
 
It's amazing - I post the question & then solve it before getting an answer - must be picking up some psychic vibes :)

Doing eval $cmd seems to do the trick. TandA

Day by day, the penguins steal my sanity.
 
Also, you could put your commands into a little function and then just call the function.

COMMAND() {
pwd
uname -a
}

Then just call COMMAND from the command line.

crowe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top