Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Simple scripting issue that's got me stumped right now

SOLVED
Go to Solution
Regular Advisor Regular Advisor

Simple scripting issue that's got me stumped right now

/usr/sbin/swlist | grep CPB | sort | tail -1
works fine on my local server

But if I set a variable equal to the command
export CMD1="/usr/sbin/swlist | grep | sort | tail -1"
and then "$CMD1", it's not recognizing the pipe symbols and I get errors about "software | not found" and "sofware grep not found" etc.

But a "remsh xxxxxxx $CMD1" works fine.

I'm trying to get a script that will issue the same command on both the local server and a list of remote servers and I'm stumped why it's being treated differently on the local server.

Ride Boldly Ride, but watch out for El Dorado's
1 ACCEPTED SOLUTIONS
Highlighted
Trusted Contributor Trusted Contributor

Re: Simple scripting issue that's got me stumped right now

hi

try this:
CMD1="/usr/sbin/swlist | grep | sort | tail -1"
eval $CMD1

the problem is that you must force the shell to eval the line twice, the first one to substitute the variable, second one to exec the pipe. Is like:

a="echo b=test"
eval $a

or

eval $(resize) in a xterm

hope it helps
7 REPLIES
Honored Contributor Honored Contributor

Re: Simple scripting issue that's got me stumped right now

Interesting ....

try:
export CMD1='/usr/sbin/swlist | grep | sort | tail -1'


(' instead of ")
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Honored Contributor Honored Contributor

Re: Simple scripting issue that's got me stumped right now

Use single quotes.
There is no substitute to HARDWORK
Honored Contributor Honored Contributor

Re: Simple scripting issue that's got me stumped right now

Hey, just tried it on linux (don't have hp here) it's acting the same.

Single quote didn't help, but:

export CMD1="/usr/sbin/swlist | grep | sort | tail -1"
sh $CMD1

worked fine.


that's why it works with "remsh", it's just like running it with "sh".
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Highlighted
Trusted Contributor Trusted Contributor

Re: Simple scripting issue that's got me stumped right now

hi

try this:
CMD1="/usr/sbin/swlist | grep | sort | tail -1"
eval $CMD1

the problem is that you must force the shell to eval the line twice, the first one to substitute the variable, second one to exec the pipe. Is like:

a="echo b=test"
eval $a

or

eval $(resize) in a xterm

hope it helps
Honored Contributor Honored Contributor

Re: Simple scripting issue that's got me stumped right now

Interestingly, the "problem" is with pipe. You could use
set CMD="ls -al" and run $CMD and it works fine.

- Biswajit
:-)
Regular Advisor Regular Advisor

Re: Simple scripting issue that's got me stumped right now

Thanks guys, fixed in minutes for me.
Single quotes didn't help, "sh $CMD1" kept giving me "execute permission denied" but "eval $CMD1" worked great.
Ride Boldly Ride, but watch out for El Dorado's
Honored Contributor Honored Contributor

Re: Simple scripting issue that's got me stumped right now

Another option is

alias CMD1="/usr/sbin/swlist | grep | sort | tail -1"
CMD1

- Biswajit
:-)