introduction to bash scripting, zyxware technologies, csi students convention, sep 15, 2012
DESCRIPTION
A workshop on "An introduction to BASH shell scripting". Conducted at CSI Students Convention at TKM College of Engineering, Kollam, Kerala on 15th September, 2012 http://www.zyxware.com/articles/3080/zyxware-conducts-workshop-on-bash-scripting-at-tkm-college-of-engineering-kollamTRANSCRIPT
![Page 1: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/1.jpg)
Shell Automation An Introduction to
Bash Shell ScriptingAnoop John
![Page 2: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/2.jpg)
►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
![Page 3: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/3.jpg)
►Kernel, Shell
►Interprets commands
►Command Line Interpreter / Command Line Interface
►POSIX compliance
►Sh, Bourne Again, Brian Fox, FSF
Shell, Interpreter, POSIX
![Page 4: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/4.jpg)
►Unix
►GNU
►GPL - Four freedoms
►Use, Modify, Distribute, Modify & Redistribute
►FSF
►GNU / Linux
Free Software
![Page 5: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/5.jpg)
►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
![Page 6: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/6.jpg)
►Executables (ls)
►Shell commands (cd, exit, pwd)
►Return values
►Command input
►Command output
►Path
►Which
Commands
![Page 7: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/7.jpg)
►Setting a Variable
►Environment (context)
►Script
►Eval
►Exec
►Source .
►Strings, integers, arrays
►Quoting - single, double, escaping
►Global, local
Variables & Environment
![Page 8: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/8.jpg)
#!/bin/bash
echo “Hello World”;
name=Anoop
echo “Hello $name”
exit;
Shell Script
![Page 9: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/9.jpg)
►Shell Scripts
►Shell Arguments
►Functions
►Function Arguments
Arguments & Functions
![Page 10: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/10.jpg)
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
![Page 11: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/11.jpg)
►Stdin
►Stdout
►Pipes
►Descriptors
Input & Output
![Page 12: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/12.jpg)
►Assignment =
►Arithmetic +, -, *, /, **,
►Bitwise <<, >>, |, &, ~, ^
►Logical !, &&, ||
►Comparisons - Arithmetic -eq, -ne, -lt, -gt, le
►Comparisons - String =, !=, <, >, <=
►Filesystem - -e, -f, -d, -x
Expressions
![Page 13: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/13.jpg)
if [[ expression ]] then
commands;
elif [[ expression ]] then
commands;
else
commands;
fi
If Command
![Page 14: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/14.jpg)
case $ANIMAL in horse | dog | cat) echo n "four" ;; man | kangaroo ) echo n "two" ;; *) echo n "an unknown number of" ;;esac
Case Command
![Page 15: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/15.jpg)
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
![Page 16: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/16.jpg)
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
![Page 17: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/17.jpg)
►awk
►sed
►grep
►find
►xargs
►cat, less, tail, head, watch
Shell Swiss Army Knives
![Page 18: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/18.jpg)
►ps
►top
►kill
►dmesg
►curl, wget
►chown, chmod, chgrp
►uptime, top, nice, nohup
Useful Commands
![Page 19: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/19.jpg)
►man
►help
►command --help
►Reading scripts
►Mailing lists
►User groups
►Local community
►Search the web
Getting help
![Page 20: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/20.jpg)
►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
![Page 21: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/21.jpg)
►Drupal Backups
►Asianet Autologin
►Reliance Autologin
►Secure Shared Folders
Exempli Gratia
![Page 22: Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention, Sep 15, 2012](https://reader034.vdocuments.mx/reader034/viewer/2022052523/556106e2d8b42a7f138b45ad/html5/thumbnails/22.jpg)
►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