Note: You are currently viewing my old web site. There is a new version with most of this content at OJB.NZ. |
Previous (Up to OJB's Tips Page) A Simple Shell ScriptShell commands can be combined into programs (or "scripts") which can be run at any time. All that you need to do is save all the commands you want to run in a text file, with a "shebang" at the top (the characters "#!") followed by the shell you are using, and set the file to be executable by using (you guessed it) the shell command chmod +x script.sh (assuming your script was called "script.sh"). You can then run it by typing the path of the file, for example /Users/owen/Documents/script.sh. Here's an example script, which lists all of the devices on a small IP network, followed by an explanation of how it works... #!/bin/sh. This line tells the system which shell to use (in this case the standard shell "sh"). The shebang (#!) is just a syntax element identifying this command. s="192.168.1.". On this line, s is a variable and I assign the first 3 bytes of the addresses I want to search through. Note the final dot, inside the double quotes, which becomes part of the address when a final number is appended (see later).
for i in {1..254}. This sets up a loop which uses a variable i to vary from
1 to 254 (the possible values of the final byte of an IP address) in the following instructions,
and echo ".\c". Echo is a command which displays something on the screen, in this case a single dot. The \c just means don't start a new line after this is displayed. In general, the backslash character means interpet the next letter as in a special way. p=`ping -c 1 -W 500 $s$i | grep '1 packets received'`. This is a bit more complex. The key is that there are two commands, separated with a pipe (|) which sends the output form the first command to the second. So here's an explanation... The first part p= just means to take the output of all the rest (which is inside back ticks (`) meaning it should be evaluated as a value) and put it in a variable M. The second part is the ping command which sends a small message to another computer and waits for a reply. This is used to check a computer at that address is running. the -c 1 means send only one ping, the -W 500 means wait 500 milliseconds for a reply, and the $s$i means insert the values of the two variables s and i. Remember that s contains "192.168.1." and i changes from 1 to 254 as it loops. The third part of the line is a grep command. This is the "global regular expression parser" which is a utility used for searching for strings. In this case it looks for the string "1 packets received" inside the output for the grep and only passes on results where this is found. So only successful pings will be processed and if that string isn't found the result (which is stored in the variable p) will be empty. if [ "$p" != "" ]; then. This is an if statement which checks if the variable p is empty. The commands from here up to the end of the if (fi) will only be run if p doesn't equal (!=) nothing ("").
echo. Display a blank line, and
fi. The end of the if block, and
echo. Another blank line, and So we have created a useful little utility to identify which computers are running on a home network. Note that almost all home networks use IP addresses where the first 3 bytes of the address stay the same and each device has a unique byte 4. The 3 bytes 192.168.1 are reserved for this use but other common sequences are 10.0.0, 172.16.0, and 192.168.0. Just look at the address of any computer to find what these are on your network and change the value next to s= appropriately. And yes, I know I could have passed that as a parameter to the script! |