23
Shell Automation An Introduction to Bash Shell Scripting Anoop John

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

Embed Size (px)

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-kollam

Citation preview

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

Shell Automation An Introduction to

Bash Shell ScriptingAnoop John

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

►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

►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

►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

►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

►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

►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

#!/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

►Shell Scripts

►Shell Arguments

►Functions

►Function Arguments

Arguments & Functions

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

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

►Stdin

►Stdout

►Pipes

►Descriptors

Input & Output

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

►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

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

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

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

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

►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

►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

►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

►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

►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

►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

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

[email protected]

9446-06-9446

Thank You!