在shell命令问题 [英] 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
德命令的返回值是为 7
。
但是当我把同样的命令的脚本里面abc_sh像下面
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
和我打电话的命令行脚本,如 abc_sh XYZ = KLMN
,它不工作,返回 0
问题是在命令中的grep 的grep $ XYZ
可能有人请告诉这是为什么不工作?
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?
推荐答案
由于您的$ 1个变量(第一个参数脚本)设置为XYZ = KLMN。
Because your $1 variable (first argument to the script) is set to XYZ=KLMN.
只要使用 abc_sh KLMN
和的grep $ 1
而不是的grep $ XYZ
。
(假设我们在这里谈论的bash)
(Assuming we are talking about bash here)
另一种方法是定义在这种情况下,你将不得不这样称呼它一个临时的环境变量: XYZ = KLMN abc_sh
The other alternative is defining a temporary environment variable in which case you would have to call it like this: XYZ=KLMN abc_sh
编辑:
找到你使用的是什么,你必须使用设置-k
(见BASH手册SHELL内建命令)
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.
所以
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:~$
所以,凡这可能是工作的地方有设置-k
在启动脚本中的一个(或.bashrc的配置文件。)
So, the place where this was working probably has set -k
in one of the startup scripts (bashrc or profile.)
这篇关于在shell命令问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!