Trying to write a bash script

Discussion in 'Mac Programming' started by MacNewbie03, Jun 2, 2009.

  1. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #1
    I am trying to write a bash script that does the following:
    Code:
    #!/bin/bash
    ping -c 4 192.168.2.27
    ping -c 4 192.168.2.1
    ping -c 4 72.11.28.215
    ping -c 4 72.11.28.129
    ping -c 4 72.11.31.21
    ping -c 4 72.11.0.21
    sleep 5s
    exit 
    
    I want to be able to double click it from my home folder and then it runs and then quits Terminal. How do I go about this? The above code is what I used on my Ubuntu box and I was able to get it to work as I wanted.
     
  2. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Messages:
    7,842
    Location:
    USA
    #2
    Put the code in a text file and named it something like script.sh. Then, from Terminal change the permissions to add the execute attribute for yourself. I believe it'll work to double click on it after that. It's been a while since I've done it, but know it's possible.
     
  3. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #3
    So from Terminal I should do
    Code:
    chmod +x PingTest.sh
    right??
     
  4. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Messages:
    570
    Location:
    Denver, CO
    #4
    Code:
    chmod 775 PingTest.sh
    
    To run the script, in Terminal type:
    Code:
    ./PingTest.sh
    
    Make sure you are in the directory where the file is located! You could even make an alias in your .profile file so that you can run it no matter what, or put it in sbin.
     
  5. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #5
    Is there any way I can launch it from the gui?
     
  6. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    Messages:
    3,325
    #6
    I think if you give the file a .command extension it will execute when you double-click it. e.g., pingTest.command. (Hopefully I am remembering the extension correctly.)
     
  7. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #7
    Yes, .command will do it but it won't quit Terminal when its done.
     
  8. macrumors regular

    Dale Cooper

    Joined:
    Sep 20, 2005
    Messages:
    218
    #8
    You can do the whole thing in Automator I believe, and save it as a doubleclickable application. It has a "execute shell script" action, and a quit application action.
     
  9. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Messages:
    570
    Location:
    Denver, CO
    #9
    Now why would you want to ever leave Terminal.app?
     
  10. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #10
    Because I have other stuff to do. :D The only time I use this script is to test for Internet connectivity

    How do I go about using Automator? I tried to use it and nothing happened. It acted like something was going on but I didn't see anything.
     
  11. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Messages:
    7,842
    Location:
    USA
    #11
    Within Automator there's a work flow option for command line stuff. I'm not at my Mac to get the specific name, but once you add that you can add your bash script and save the work flow as a .app.
     
  12. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #12
    Sorry for asking so many questions but how do I get Automator to show me that the script is running? When I run the script from Terminal myself I get something like this
    Code:
    PING 192.168.2.27 (192.168.2.27): 56 data bytes
    64 bytes from 192.168.2.27: icmp_seq=0 ttl=64 time=0.047 ms
    64 bytes from 192.168.2.27: icmp_seq=1 ttl=64 time=0.058 ms
    64 bytes from 192.168.2.27: icmp_seq=2 ttl=64 time=0.057 ms
    64 bytes from 192.168.2.27: icmp_seq=3 ttl=64 time=0.056 ms
    
    --- 192.168.2.27 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 0.047/0.054/0.058/0.004 ms
    PING 192.168.2.1 (192.168.2.1): 56 data bytes
    64 bytes from 192.168.2.1: icmp_seq=0 ttl=64 time=3.103 ms
    64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=2.885 ms
    64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=3.063 ms
    64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=3.232 ms
    
    --- 192.168.2.1 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 2.885/3.071/3.232/0.124 ms
    PING 72.11.28.215 (72.11.28.215): 56 data bytes
    64 bytes from 72.11.28.215: icmp_seq=0 ttl=64 time=2.954 ms
    64 bytes from 72.11.28.215: icmp_seq=1 ttl=64 time=3.046 ms
    64 bytes from 72.11.28.215: icmp_seq=2 ttl=64 time=3.137 ms
    64 bytes from 72.11.28.215: icmp_seq=3 ttl=64 time=3.077 ms
    
    --- 72.11.28.215 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 2.954/3.054/3.137/0.066 ms
    PING 72.11.28.129 (72.11.28.129): 56 data bytes
    64 bytes from 72.11.28.129: icmp_seq=0 ttl=254 time=82.527 ms
    64 bytes from 72.11.28.129: icmp_seq=1 ttl=254 time=57.910 ms
    64 bytes from 72.11.28.129: icmp_seq=2 ttl=254 time=79.953 ms
    64 bytes from 72.11.28.129: icmp_seq=3 ttl=254 time=98.633 ms
    
    --- 72.11.28.129 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 57.910/79.756/98.633/14.503 ms
    PING 72.11.31.21 (72.11.31.21): 56 data bytes
    64 bytes from 72.11.31.21: icmp_seq=0 ttl=61 time=108.897 ms
    64 bytes from 72.11.31.21: icmp_seq=1 ttl=61 time=11.599 ms
    64 bytes from 72.11.31.21: icmp_seq=2 ttl=61 time=20.390 ms
    64 bytes from 72.11.31.21: icmp_seq=3 ttl=61 time=88.204 ms
    
    --- 72.11.31.21 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 11.599/57.273/108.897/42.036 ms
    PING 72.11.0.21 (72.11.0.21): 56 data bytes
    64 bytes from 72.11.0.21: icmp_seq=0 ttl=60 time=74.475 ms
    64 bytes from 72.11.0.21: icmp_seq=1 ttl=60 time=80.896 ms
    64 bytes from 72.11.0.21: icmp_seq=2 ttl=60 time=81.626 ms
    64 bytes from 72.11.0.21: icmp_seq=3 ttl=60 time=52.231 ms
    
    --- 72.11.0.21 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 52.231/72.307/81.626/11.920 ms
    
    The above is what I want to do with Automator.
     
  13. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Messages:
    7,842
    Location:
    USA
    #13
    Automator doesn't seem to have anything great for displaying the results. I wrote up a quick AppleScript instead that display the results in a pop-up.
    Code:
    display dialog (do shell script "
    ping -c 1 -q 192.168.2.27
    ping -c 1 -q 192.168.2.1
    ping -c 1 -q 72.11.28.215
    ping -c 1 -q 72.11.28.129
    ping -c 1 -q 72.11.31.21;
    ping -c 1 -q 72.11.0.21;
    ")
    You can open Script Editor and place it in there. It can be saved as a .app as well. I modified the commands slightly to better ensure the output would fit inside the pop-up.
     
  14. macrumors regular

    Joined:
    Jul 3, 2007
    Messages:
    106
    Location:
    Hicksville, OH (Northwest Ohio)
    #14
    Hey thanks angelwatt!! :eek:
     

Share This Page