views:

82

answers:

4

Hi, 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?

+3  A: 

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

Vinko Vrsalovic
There is a constraint that i should not call the script like u said abc_sh KLMN.
Vijay Sarathi
You should have said so, but anyway, the way to do it is answered twice already
Vinko Vrsalovic
this a an already existing script where it will be called like that only abc_sh XYZ=KLMN and now i am using this variable XYZ.but this is not working .even if i do echo $XYZ before the command inside the script it does not show anything.and this variable is already used some other places in the script already where it is working fine.
Vijay Sarathi
did you do "XYZ=KLMN abc_sh" instead of "abc_sh XYZ=KLMN"?
Vinko Vrsalovic
why don't you paste the original script as well? Are you using BASH?
Vinko Vrsalovic
FYI, i am using ksh.
Vijay Sarathi
@vinko thanks a lot for ur suggestion of using set -k.it has solved the problem:)
Vijay Sarathi
+2  A: 

Try any of these to set a temporary environment variable:

XYZ=KLMN abc_sh
env XYZ=KLMN abc_sh
(export XYZ=KLMN; abc_sh)
John Kugelman
Dysoistia :( http://stackoverflow.com/questions/1386824/hired-as-a-developer-to-maintain-and-update-current-code-base-no-docs/1386842#1386842
Vinko Vrsalovic
+1  A: 

you are using so many commands chained together....

ps -u `id -u` -f |  awk -v x="$XYZ" -v p="ppLSN" '$0~p{
 m=split($9,a,"=")
 if(a[2]~x){count++} 
}
END{print count}'
ghostdog74
A: 

Call this script:

#!/bin/ksh
ps -u $(id -u) -o args | grep $XYZ | cut -f2- -d " "

Like this:

XYZ=KLMN abc_sh
Dennis Williamson