Main
CouchMan
Flac-Jacket
Downloads
 -mostly scripts
Script Bits
Docs
Misc Notes
Tasks
Personal
hash bang slash
      bin slash bash

Snips of Scripts


Here is a link to some Working Examples of Shell Scripts that you can View or Download.
This page, and the above link, will be most useful as quick references and are not of much value unless you are already familiar with shell scripting.


Quick Reference
  • exec -replaces the running process with a new one
  • exit n -quits the shell with exit status n
  • export -makes variables available to spawned shells
File Test Options
-b file True if file exists and is a block special file
-c file True if file exists and is a character special file
-d file True if file exists and is a directory
-e file True if file exists
-f file True if file exists and is a regular file
-G file True if file exists and is owned by the effective group ID
-g file True if file exists and has SGID set
-k file True if file exists and has its "sticky" bit set
-l file True if file exists and is a symbolic link
-O file True if file exists and is owned by effective ID
-p file True if file exists and is a named pipe
-r file True if file exists and is a readable
-S file True if file exists and is a socket
-s file True if file exists and has a size greater than zero
-u file True if file exists and has SUID set
-w file True if file exists and is writable
-x file True if file exists and is executable
file1 -ef file2 True if both files have the same device and inode numbers
file1 -nt file2 True if file1 is newer (modification date) than file2
file1 -ot file2 True if file1 is older (modification date) than file2

String Test Options
string1 -a string2 True if both expressions are true
string1 -o string2 True if either expressions are true
-z string True if the length of string is zero
-n string True if the length of string is non-zero
string1 = string2 strings are equal
string1 != string2 strings not are equal

Integer Test Options
integer1 -eq integer2 True if integers are equal
integer1 -ge integer2 True if integer1 is greater than or equal to integer2
integer1 -gt integer2 True if integer1 is greater than integer2
integer1 -lt integer2 True if integer1 is less than integer2
integer1 -le integer2 True if integer1 is less than or equal to integer2
integer1 -ne integer2 True if integer1 is not equal to integer2

Script Snipits
  • IF, WHILE, DO
    if [ -z "$" ]; then
      while [ -z "$" ]
      do
      ....(some code)...
      done 
    fi
    
  • FOR, DO, DONE
    for i in  word1 word2 ...wordn
    do
      action involving $i (which = word)
    done
    

  • AWK & SED
    NEW_VARIABLE="`/sbin/ifconfig -a|grep -A 2 eth0|awk '/inet/ { print $2 }'|sed -e s/addr://`"
    

  • MAIL Notification
    # build the Message & Subject variables
       echo "No major canges in disk use on $HOSTNAME" > $MESSAGE
       echo "  " >> $MESSAGE
       echo "`cat $DISKUSE`" >> $MESSAGE
       echo "$HOSTNAME - Disk Space Report" > $SUBJECT
    
    # send the message to address $EMAIL
    echo "`cat $MESSAGE`" | mail -s "`cat $SUBJECT`" $EMAIL
    

  • Test if File Exists with Non-Zero Size
    if [ -s $DIFFTEST ] ;then
    

  • Test for Exact Equal (of number value)
    if [ $SCHEDULED == $ACTUAL ] ;then
    

  • Get IP Address
    # get external address from by greping ifconfig 
    EXIP="`ifconfig -a|grep -A 2 $EXIF|awk '/inet/ { print $2 }'\
       |sed -e s/addr://`"
    

  • Get Broadcast Address
    EXBCAST="`ifconfig -a|grep -A 2 $EXIF|awk '/inet/ { print $3 }'\
      |sed -e s/Bcast://`"
    

  • Get DNS Server IP's
    # get dns servers from resolv.conf file
    # first line is path, next 3 are dns servers
    NAMED1="`cat /etc/resolv.conf|grep -n nameserver|grep 2:\
     |sed -e s/nameserver//|sed -e s/2://`" 
    
    NAMED2="`cat /etc/resolv.conf|grep -n nameserver|grep 3:\
     |sed -e s/nameserver//|sed -e s/3://`"
    
    NAMED3="`cat /etc/resolv.conf|grep -n nameserver|grep 4:\
     |sed -e s/nameserver//|sed -e s/4://`"
    

  • Remove Spaces from Filenames
    for x in `ls -1|sed -e s/"\ "/_/g`;do mv "`echo $x|sed -e s/_/"\ "/g`" ${x};done
    


Back to Main Page
Site maintained by: Pete Nesbitt RHCE
Date: Thursday, 02-Sep-2010 16:07:06 PDT