Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...