在 Unix 中使用 If 语句的问题

发布于 2024-11-27 17:14:19 字数 866 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼波传意 2024-12-04 17:14:19

null 不是 Bash 关键字。如果要检查变量是否已定义,可以执行以下操作:

if [ "${var+defined}" = defined ]

检查变量是否为空或未定义:

if [ -z "${var-}" ]

null is not a Bash keyword. If you want to check whether a variable is defined, you can do the following:

if [ "${var+defined}" = defined ]

To check whether it's empty or undefined:

if [ -z "${var-}" ]
软糯酥胸 2024-12-04 17:14:19

我们不知道您如何设置任何变量值(jmsProcess、boneProcess)。

尝试包围所有 var 值,例如“$var”(带双引号),然后查看错误消息是否发生变化。

上面可见的代码中还存在许多语法问题。很难判断它是否是在这里发布的工件(代码块功能非常强大),所以我将对我看到的错误进行评论。

if[ [ false || false ] == true ]: command not found

这里有很多问题: false 是一个 shell 命令。尝试在命令行中输入它,然后执行 echo $?。您应该看到 1真; echo $? 将返回 0。但是 if 语句根据最后的返回代码继续或转移到 else 块(有一些特殊情况例外)。

另外,我猜您正在尝试使用 [ false || 制作某种 reg exp false] == true。行不通。见下文。

您可以将状态变量设置为 false(或 true)值,shell 将正确评估该值。

另外,if[ 将给出“命令未找到”消息。因此,通过使用值为 false 的变量,您可以尝试

jmsRunning=false ; boneRunning=true
if [[ ${jmsRunning} || ${boneRunning} ]] ; then           
  echo both NOT running
else
  echo at least 1 is running
fi

两者更改为 false 以查看消息更改。

另外,null 只是 shell 脚本中的一个字符串,您可能指的是“”。

最后,var 赋值不能在“=”符号周围有空格,并且当它位于语句的左侧时,不要在前面使用“$”字符,即

boneRunning=true 

我希望这会有所帮助。

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.

if[ [ false || false ] == true ]: command not found

There are a lot of issues here: false is an shell command. Try typing it on the command line and then do echo $?. you should see 1. 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 (or true) 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 do

Try

jmsRunning=false ; boneRunning=true
if [[ ${jmsRunning} || ${boneRunning} ]] ; then           
  echo both NOT running
else
  echo at least 1 is running
fi

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.

boneRunning=true 

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文