Operating System - HP-UX
1752402 Members
5638 Online
108788 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
S.Rider
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
7 REPLIES 7
Alex Lavrov.
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)
RAC_1
Honored Contributor

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

Use single quotes.
There is no substitute to HARDWORK
Alex Lavrov.
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)
Cesare Salvioni
Trusted Contributor
Solution

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
Biswajit Tripathy
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
:-)
S.Rider
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
Biswajit Tripathy
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
:-)