在 Unix 中使用 If 语句的问题
我是 shell 脚本新手,需要一些帮助。我正在尝试编写一个脚本来反弹某些服务器,但我的 if 语句有一些问题。下面的第一个和第二个给了我一个参数太多
错误。
对于第一个,我的变量 $jmsProcess
是一个 ps -ef | grep
命令,我只想进入 if 语句
(如果它返回一些结果)。这与第二个问题相同。
在第三个 if-statement
中,我希望它检查这些变量中的任何一个是否具有值 true
但这给了我一个
if[ [ false || false ] == true ]: command not found
错误。
#Check the JMS process has been killed
if [ $jmsProcess != null ] # SHOULD THIS BE NULL???
then
echo "JMS Process is still running"
$jmsRunning = "true"
fi
#Check the Bone process has been killed
if [ $boneProcess != null ] # SHOULD THIS BE NULL???
then
echo "B-One Process is still Running"
$boneRunning = "true"
fi
if[ [ $jmsRunning || $boneRunning ] == true ] # CHECK THIS FOR QUOTES
then
# $killProcess
fi
I'm new to shell scripting and need some help. I am trying to write a script to bounce some servers and I am having a few issues with my if statements. The First and Second one below is giving me a too many arguments
error.
For the first one, I am the variable $jmsProcess
is a ps -ef | grep
command and I only want to go into the if-statement
, if this returns some results. This is the same issue for the second one.
In the Third if-statement
I want it to check if either of those variables have the value true
but this gives me a
if[ [ false || false ] == true ]: command not found
Error.
#Check the JMS process has been killed
if [ $jmsProcess != null ] # SHOULD THIS BE NULL???
then
echo "JMS Process is still running"
$jmsRunning = "true"
fi
#Check the Bone process has been killed
if [ $boneProcess != null ] # SHOULD THIS BE NULL???
then
echo "B-One Process is still Running"
$boneRunning = "true"
fi
if[ [ $jmsRunning || $boneRunning ] == true ] # CHECK THIS FOR QUOTES
then
# $killProcess
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
null
不是 Bash 关键字。如果要检查变量是否已定义,可以执行以下操作:检查变量是否为空或未定义:
null
is not a Bash keyword. If you want to check whether a variable is defined, you can do the following:To check whether it's empty or undefined:
我们不知道您如何设置任何变量值(jmsProcess、boneProcess)。
尝试包围所有 var 值,例如“$var”(带双引号),然后查看错误消息是否发生变化。
上面可见的代码中还存在许多语法问题。很难判断它是否是在这里发布的工件(代码块功能非常强大),所以我将对我看到的错误进行评论。
这里有很多问题:
false
是一个 shell 命令。尝试在命令行中输入它,然后执行echo $?
。您应该看到1
。真; echo $?
将返回 0。但是 if 语句根据最后的返回代码继续或转移到 else 块(有一些特殊情况例外)。另外,我猜您正在尝试使用
[ false || 制作某种 reg exp false] == true
。行不通。见下文。您可以将状态变量设置为
false
(或true
)值,shell 将正确评估该值。另外,
if[
将给出“命令未找到”消息。因此,通过使用值为 false 的变量,您可以尝试将
两者更改为
false
以查看消息更改。另外,null 只是 shell 脚本中的一个字符串,您可能指的是“”。
最后,var 赋值不能在“=”符号周围有空格,并且当它位于语句的左侧时,不要在前面使用“$”字符,即
我希望这会有所帮助。
We don't know how you're setting any of the variable values, (jmsProcess, boneProcess).
Try surrounding all var values like "$var" (with dbl-quotes) and see if the error messages change.
Also there are numerous syntax issues in code visible above. Hard to tell if it is an artifact of posting here, (The code block feature is pretty robust), so I'm going to comment on what I see wrong.
There are a lot of issues here:
false
is an shell command. Try typing it on the command line and then doecho $?
. you should see1
.true; echo $?
will return 0. But the if statements continue or fall-over to the else block based on the last return code (with some special case exceptions).Also, I'm guessing you're trying to make some sort of reg exp with
[ false || false ] == true
. Won't work. see below.You can set status variables to have the value of
false
(ortrue
) which will be evaluated correctly by the shell.Also,
if[
will give the 'command not found' msg. So by using vars that have the value false, you can doTry
Change both to
false
to see the message change.Also, null is just a string in a shell script, you probably mean "".
Finally, var assignments cannot have spaces surrounding the '=' sign AND do not use the '$' char at the front when it is on the left hand side of the statment, i.e.
I hope this helps.