Shell Automation
 An Introduction to
Bash Shell Scripting
      Anoop John
An Outline
►Shell, interpreter, POSIX
►Shell script operation
►Commands, paths, returns
►Variables, environments
►Input, output, pipes, descriptors
►Expressions, conditions, loops
►Awk, grep, sed, find, xargs
►System utils
►Examples
Shell, Interpreter, POSIX
►Kernel, Shell
►Interprets commands
►Command Line Interpreter / Command Line
Interface
►POSIX compliance
►Sh, Bourne Again, Brian Fox, FSF
Free Software
►Unix
►GNU
►GPL - Four freedoms
 ►Use, Modify, Distribute, Modify &
  Redistribute
►FSF
►GNU / Linux
Shell Operation
►Reads input from file, string (-c), or terminal
►Breaks the input into words and operators
►Parses the tokens into simple and compound
commands
►Performs the various shell expansions
►Performs any necessary redirections
►Executes the command
►Optionally waits for the command to complete
and collects its exit status
Commands
►Executables (ls)
►Shell commands (cd, exit, pwd)
►Return values
►Command input
►Command output
►Path
►Which
Variables & Environment
►Setting a Variable
►Environment (context)
►Script
►Eval
►Exec
►Source .
►Strings, integers, arrays
►Quoting - single, double, escaping
►Global, local
Shell Script
#!/bin/bash
echo “Hello World”;
name=Anoop
echo “Hello $name”
exit;
Arguments & Functions
►Shell Scripts
►Shell Arguments
►Functions
►Function Arguments
Shell Function
function log {
  if [ $# ­gt 0 ]; then
    echo "[$(date +"%D %T")] $@" >> $LOG_FILE
    db "$@"
  else 
    while read data
    do
      echo "[$(date +"%D %T")] $data" >> 
$LOG_FILE 
      db "$data"
    done
  fi
}
log “Hello World!”
echo “Hello World!” | log
Input & Output
►Stdin
►Stdout
►Pipes
►Descriptors
Expressions
►Assignment =
►Arithmetic +, -, *, /, **,
►Bitwise <<, >>, |, &, ~, ^
►Logical !, &&, ||
►Comparisons - Arithmetic -eq, -ne, -lt, -gt, le
►Comparisons - String =, !=, <, >, <=
►Filesystem - -e, -f, -d, -x
If Command
if [[ expression ]] then
  commands;
elif [[ expression ]] then
  commands;
else
  commands;
fi
Case Command
case $ANIMAL in
  horse | dog | cat) 
    echo ­n "four"
    ;;
  man | kangaroo ) 
   echo ­n "two"
   ;;
  *) 
   echo ­n "an unknown number of"
   ;;
esac
For Loop
for NAME [in LIST ]; do 
  COMMANDS; 
done

i=0
for filename in `ls`; do 
  i=$(( i + 1));
  printf "%­5s ­ %sn" $i “$filename”;
done;

for name in Anoop John; do 
  echo “Hello ${name}”;
done;
While Loop
while [[ expression ]]; do 
  COMMANDS; 
done

i=0; while [[ $i ­lt 10 ]]; do
  echo Counting $i;
  ((i+=1));
done;

while read line
do
  echo $line
done < path/to/file
Shell Swiss Army Knives
►awk
►sed
►grep
►find
►xargs
►cat, less, tail, head, watch
Useful Commands
►ps
►top
►kill
►dmesg
►curl, wget
►chown, chmod, chgrp
►uptime, top, nice, nohup
Getting help
►man
►help
►command --help
►Reading scripts
►Mailing lists
►User groups
►Local community
►Search the web
How to Start
►Get GNU / Linux installed on your systems
►Start using shell
►Identify pain points in your daily operations
►Automate through scripts
►Join a mailing list
►Ask & answer questions
►Show off :-)
Exempli Gratia
►Drupal Backups
►Asianet Autologin
►Reliance Autologin
►Secure Shared Folders
About Zyxware
►Free Software Company
►Software Development - Drupal
►Leading Drupal Contributor from India
►FSF Contributing Member
►Free Software Support in the local market
►IT Training and FOSS Enabling
►Websites & Email Services
►IT Consultancy for Enterprises
Thank You!

   www.zyxware.com
  info@zyxware.com
     9446-06-9446

Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012