Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

unix - problem in a shell command

i am trying the following command on the command line

ps -u `id | cut -f2 -d"=" | cut -f1 -d"("`  -f | grep ppLSN | awk '{print $9}' | awk '{FS="=";print $2}' | grep KLMN | wc -l

the value of teh command is returned as 7.

but when i am putting the same command inside a script abc_sh like below

ps -u `id | cut -f2 -d"=" | cut -f1 -d"("`  -f | grep ppLSN | awk '{print $9}' | awk '{FS="=";print $2}' | grep $XYZ | wc -l

and i am calling the script on the command line as abc_sh XYZ=KLMN and it does not work and returns 0 the problem is with the grep in the command grep $XYZ could anybody please tell why this is not working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because your $1 variable (first argument to the script) is set to XYZ=KLMN.

Just use abc_sh KLMN and grep $1 instead of grep $XYZ.

(Assuming we are talking about bash here)

The other alternative is defining a temporary environment variable in which case you would have to call it like this: XYZ=KLMN abc_sh

EDIT:

Found what you were using, you have to use set -k (see SHELL BUILTIN COMMANDS in the BASH manual)

          -k      All arguments in the form of assignment  statements  are
                  placed  in the environment for a command, not just those
                  that precede the command name.

So

vinko@parrot:~$ more abc
#!/bin/bash
echo $XYZ
vinko@parrot:~$ set -k
vinko@parrot:~$ ./abc XYZ=KLMN
KLMN
vinko@parrot:~$ set +k
vinko@parrot:~$ ./abc XYZ=KLMN

vinko@parrot:~$

So, the place where this was working probably has set -k in one of the startup scripts (bashrc or profile.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...