IF Statement *Mess*

Discussion in 'Mac Programming' started by gpchess2k, May 26, 2017.

  1. gpchess2k macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #1
    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
     
  2. Nermal Moderator

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    New Zealand
    #2
    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.
     
  3. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
  4. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #4
    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.
     
  5. MC6800 macrumors 6502

    Joined:
    Jun 29, 2016
    #5
    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.
     
  6. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #6
    @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
     

Share This Page