Mac IF Statement *Mess*

gpchess2k

macrumors member
Original poster
Oct 12, 2015
39
0
Hey Guys,

In a pickle with a simple IF statement. I am assigning the output of a command to a variable. But this command can hang for some time and so I figured out how to simply kill it. This command is checking for a connection to a specific server. The problem is there are multiple lines and I'm trying to search for keywords but no luck. Attempted some variations of GREP too. What am I missing?

Here is the script simplified:

Code:
#!/bin/bash

jss_comm_chk=$(jamf checkJSSConnection& sleep 2; kill $! > /dev/null; echo $?)

if [[ "$jss_comm_chk" == *"JSS is available"* ]]; then
echo "JSS binary installed. Attempting to reinstall......"

if [[ "$jss_comm_chk" == *"command not found"* ]]; then
echo "JSS binary not currently installed. Attempting to install....."

else
echo "No Internet Connection"
fi
fi
One output of the command would be:

Code:
[1] 77856
Checking availability of https://jss.corp.mycompany.com:8223/...
The JSS is available.
[1]+  Done                    jamf checkJSSConnection
-bash: kill: (77856) - No such process
1
 

Nermal

Moderator
Staff member
Dec 7, 2002
18,888
1,361
New Zealand
I'll just prefix this by saying that I know next to nothing about bash, but is there supposed to be a ; before the "then"? That seems very strange, coming from a C background.
 

Senor Cuete

macrumors 6502
Nov 9, 2011
303
12
It looks to me like you have too many endifs. The else matches the second if, so you should only need one endif at the end.
 

MC6800

macrumors 6502
Jun 29, 2016
368
126
Do you want to be executing your command twice? Would it not be better to execute it once, put the result in a variable, and then do the sequential IFs to dispatch on the result? Assume there's more code to be added here though.
 

gpchess2k

macrumors member
Original poster
Oct 12, 2015
39
0
@Senor Cuete Yes your right. I had it right the first time. Seems like the issue is in my expression. Its only searching the first line of the variable output. If i search for '1' then it works based on my above output example.

Updated code:

Code:
#!/bin/bash

jss_comm_chk=$(jamf checkJSSConnection& sleep 2; kill $! > /dev/null; echo $?)

if [[ "$jss_comm_chk" == *"JSS is available"* ]]
then
echo "JSS binary installed. Attempting to reinstall......"


else if [[ "$jss_comm_chk" == *"command not found"* ]]
then
echo "JSS binary not currently installed. Attempting to install....."

else
echo "No Internet Connection"
fi
fi
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.