PDA

View Full Version : Trying to write a bash script




MacNewbie03
Jun 2, 2009, 04:32 PM
I am trying to write a bash script that does the following:
#!/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.



angelwatt
Jun 2, 2009, 04:45 PM
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.

MacNewbie03
Jun 2, 2009, 05:58 PM
So from Terminal I should do chmod +x PingTest.sh right??

LtRammstein
Jun 2, 2009, 08:58 PM
chmod 775 PingTest.sh


To run the script, in Terminal type:

./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.

MacNewbie03
Jun 3, 2009, 08:04 AM
Is there any way I can launch it from the gui?

iSee
Jun 3, 2009, 08:20 AM
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.)

MacNewbie03
Jun 3, 2009, 09:04 AM
Yes, .command will do it but it won't quit Terminal when its done.

Dale Cooper
Jun 3, 2009, 09:10 AM
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.

LtRammstein
Jun 3, 2009, 08:40 PM
Now why would you want to ever leave Terminal.app?

MacNewbie03
Jun 3, 2009, 10:49 PM
Now why would you want to ever leave Terminal.app?

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.

angelwatt
Jun 4, 2009, 08:14 AM
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.

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.

MacNewbie03
Jun 4, 2009, 07:02 PM
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
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.

angelwatt
Jun 4, 2009, 08:32 PM
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.
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.

MacNewbie03
Jun 5, 2009, 10:06 AM
Hey thanks angelwatt!! :o