Download - Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012
Shell Automation An Introduction to
Bash Shell ScriptingAnoop John
►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
An Outline
►Kernel, Shell
►Interprets commands
►Command Line Interpreter / Command Line Interface
►POSIX compliance
►Sh, Bourne Again, Brian Fox, FSF
Shell, Interpreter, POSIX
►Unix
►GNU
►GPL - Four freedoms
►Use, Modify, Distribute, Modify & Redistribute
►FSF
►GNU / Linux
Free Software
►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
Shell Operation
►Executables (ls)
►Shell commands (cd, exit, pwd)
►Return values
►Command input
►Command output
►Path
►Which
Commands
►Setting a Variable
►Environment (context)
►Script
►Eval
►Exec
►Source .
►Strings, integers, arrays
►Quoting - single, double, escaping
►Global, local
Variables & Environment
#!/bin/bash
echo “Hello World”;
name=Anoop
echo “Hello $name”
exit;
Shell Script
►Shell Scripts
►Shell Arguments
►Functions
►Function Arguments
Arguments & Functions
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
Shell Function
►Stdin
►Stdout
►Pipes
►Descriptors
Input & Output
►Assignment =
►Arithmetic +, -, *, /, **,
►Bitwise <<, >>, |, &, ~, ^
►Logical !, &&, ||
►Comparisons - Arithmetic -eq, -ne, -lt, -gt, le
►Comparisons - String =, !=, <, >, <=
►Filesystem - -e, -f, -d, -x
Expressions
if [[ expression ]] then
commands;
elif [[ expression ]] then
commands;
else
commands;
fi
If Command
case $ANIMAL in horse | dog | cat) echo n "four" ;; man | kangaroo ) echo n "two" ;; *) echo n "an unknown number of" ;;esac
Case Command
for NAME [in LIST ]; do COMMANDS; done
i=0for filename in `ls`; do i=$(( i + 1)); printf "%5s %s\n" $i “$filename”;done;
for name in Anoop John; do echo “Hello ${name}”;done;
For Loop
while [[ expression ]]; do COMMANDS; done
i=0; while [[ $i lt 10 ]]; do echo Counting $i; ((i+=1));done;
while read linedo echo $linedone < path/to/file
While Loop
►awk
►sed
►grep
►find
►xargs
►cat, less, tail, head, watch
Shell Swiss Army Knives
►ps
►top
►kill
►dmesg
►curl, wget
►chown, chmod, chgrp
►uptime, top, nice, nohup
Useful Commands
►man
►help
►command --help
►Reading scripts
►Mailing lists
►User groups
►Local community
►Search the web
Getting help
►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 :-)
How to Start
►Drupal Backups
►Asianet Autologin
►Reliance Autologin
►Secure Shared Folders
Exempli Gratia
►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
About Zyxware