Shell Scripting - Part 1

 

A UNIX shell script is a series of commands strung together to automate tasks.  The commands are executed by the UNIX interrupter.

 

The first line of every shell script is:

 

#!/bin/sh         the shebang (#!) Is a special operator and if it appears on the first line of a file, it indicates which interrupter should be invoked.  In this case /bin/sh    (http://en.wikipedia.org/wiki/Shebang_(Unix))

 

Other common invocations are:

 

#!/bin/perl

#!/usr/local/bin/expect    (another scripting language)

#!/bin/bash

#!/bin/tcsh        

#!/bin/ksh      

 

 

 

As a rule of thumb, I generally used #!/bin/sh ; since ksh and bash were derived from /bin/sh, they have additional features that are not found in /bin/sh; thus if I use /bin/sh I know it will work on the other two shells

 

 

Tcsh and any of the csh derivatives have a different syntax and most people don't generally use them for shell scripting

 

Here is a brief history and some great notes on UNIX shell interrupters:

http://en.wikipedia.org/wiki/Unix_shell

 

 

 

 

so, lets start with our first shell program; we'll put the following into a file called test.sh:

 

 

#!/bin/sh                    <- this has to be the very top line in the file

echo "hello world"

 

 

 

 

after we save it, we need to make it executable:

chmod 755 test.sh

 

now we can run it:

./test.sh

 

UNIX shell scripts can use any combination of pipes, redirection and commands in combination with one another.  Here are a few examples:

 

 

#!/bin/sh

 

echo "the quick brown fox .. " | mail -s "test stuff" apayne@mlp.com

exit 0

 

 

the exit 0 give it a successful return code.

 

#!/bin/sh

 

ME=`hostname`

echo "All IP addresses for $ME"

/sbin/ifconfig -a | grep "inet "

exit 0

 

This shows me all the ipv4 ip addresses.

Notice that I have the line:

ME=`hostname`

 

This is called a variable.  Throughout my script when I refer to $ME it will be a substitute for the hostname of the machine.

 

The syntax for setting a variable is:

VARNAME=SOMETHING   <-  notice that there are no spaces!

 

Lets try a few now:

 

#!/bin/sh

ME=`hostname`     <-I can substitute the output of a command with backticks

IP=` /sbin/ifconfig eth0 | grep "inet addr:" | awk -F ":" '{print $2}' | awk -F " " '{print $1}'`

 

STRING="this is a string"

 

echo "$IP"

echo "$ME"

echo "$STRING"

 

NS=` cat /etc/resolv.conf | tail -2 | head -1 | awk -F " " '{print $2}'`

 

ping -c 1 $NS

exit 0

 

 

HOMEWORK 1:

read:http://steve-parker.org/sh/first.shtml and http://steve-parker.org/sh/variables1.shtml

1- watch http://www.youtube.com/watch?v=IEheCUsf7lA and http://www.youtube.com/watch?v=uq8Z9aYdGpE&feature=related

 

I love this guy!

 

2- write a shell script that prints (to screen) the last line of the /etc/passwd file 

 

3- write a shell script that prints ONLY the usernames of all the users in the /etc/passwd file

 

 

UNIX COMMANDS:

ifconfig

 

     this is used to show how a network card is configured.  It can also

     be used to configure a network card.  The important switches are:

     -a show all interfaces and ethx  shows just the info for ethx

 

      /sbin/ifconfig eth0

 

netstat

     

      This is used to show network information.  Netstat -nr shows the

       Routing table.  Netstat -a shows me all current network connections

     

       Review the man page for both of these please, then

HOMEWORK 2:

  

Write s shell script that prints all the current established network connections