开发者

Issue using If statments in Unix

开发者 https://www.devze.com 2023-03-25 03:59 出处:网络
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 t

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 on开发者_如何学运维e.

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


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-}" ]


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号