How to add if ... endif to unix script?

Discussion in 'Mac Programming' started by nelly22, Jul 28, 2016.

  1. nelly22 macrumors 6502

    Joined:
    Sep 29, 2009
    #1
    I have few rows of my own code i would like to run when this script kills distnoted.

    It propably needs if ... end if type or thing.

    How i can do this?

    ps -reo '%cpu,uid,pid,command' |
    awk -v UID=$UID '
    /distnoted agent$/ && $1 > 100.0 && $2 == UID {
    system("kill -9 " $3)
    }
    '
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    I'm not entirely sure what you're trying to do there. However, "if" in UNIX works like this:

    http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

    ...so an example might be:

    Code:
    if [ 1 -eq 1 ]; then
    
      #do something
    
    fi
    

    ...hope that's at least some help!
     
  3. nelly22, Jul 29, 2016
    Last edited: Jul 29, 2016

    nelly22 thread starter macrumors 6502

    Joined:
    Sep 29, 2009
    #3
    Hi an Thanks.

    I try to do some thing like this:
    Code:
    IF ps -reo '%cpu,uid,pid,command' | awk -v UID=$UID ' /distnoted agent$/ && $1 > 100.0 && $2 == UID
        kill distnoted (system("kill -9 " $3))
        my code1 here
        my code2 here
    END IF
    So basically i want to add couple of my own rows of code after kill row and my rows are run only if kill row was run.
     
  4. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
    Code:
    command1 && command2 
    command2 only runs if command1 is successful
     
  5. nelly22 thread starter macrumors 6502

    Joined:
    Sep 29, 2009
    #5
    Thanks.

    I don't know what to do with this code.

    It would be much simpler and readable for newbie if all are in own rows.

    Actually this would be better:
    Code:
    IF ps says its over 100
        kill
        IF kill was succesfull
            my code 1
            my code 2
        else
            my code 1
            my code 2
        END IF
    END IF
     
  6. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #6
    You can do stuff like this:
    Code:
    if ps -reo '%cpu,uid,pid,command' | awk -v UID=$UID ' /distnoted agent$/ && $1 > 100.0 && $2 == UID;then
        kill distnoted (system("kill -9 " $3))
    else
        echo "Command failed"
    fi
    --- Post Merged, Jul 29, 2016 ---
    If you look up 'bash programming' online you can find many guides to help you :)
     
  7. nelly22 thread starter macrumors 6502

    Joined:
    Sep 29, 2009
    #7
    Thanks.

    I think i have now problem with $3. What ever that is.

    I tried this, because if i turn this unix script to AppleScript, then i can easily combine unix and AppleScript rows and integrate it better with my other scripts:

    Code:
    if (do shell script "ps -reo '%cpu,uid,pid,command' | awk -v UID=$UID ' /distnoted agent$/ && $1") > 100 then
      do shell script "kill distnoted (system("kill -9 " $3))"
      my AppleScript code here
    end if
     

Share This Page