A Very Short Introduction to Shell Scripts CSCI

  • Slides: 30
Download presentation
(A Very Short) Introduction to Shell Scripts CSCI N 321 – System and Network

(A Very Short) Introduction to Shell Scripts CSCI N 321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana University

Section Overview Comments and Script Headers Variables Flow Control Loops

Section Overview Comments and Script Headers Variables Flow Control Loops

References Linux System Administration Chapter 14 CQU COIT 13146 System Administration Course Textbook Lectures

References Linux System Administration Chapter 14 CQU COIT 13146 System Administration Course Textbook Lectures w. Chapter w 2002 9 #8

Why Learn Scripting? /bin/sh always there Understand/Modify Existing System Scripts n n RC Startup

Why Learn Scripting? /bin/sh always there Understand/Modify Existing System Scripts n n RC Startup Scripts Daemon Startup Scripts Automate repetitive tasks Great for small tools

Writing/Debugging Scripts Know how to do job manually first Write in small (logical) parts

Writing/Debugging Scripts Know how to do job manually first Write in small (logical) parts Test each part before continuing n n n Print modified variables Print command lines to be executed Never work on production data Comment code!!!

Starting Scripts Passed as input to a shell n Example: /bin/bash mysript Run directly

Starting Scripts Passed as input to a shell n Example: /bin/bash mysript Run directly from the command line n n n First line of script (i. e: #!/bin/bash) Script must have execute bits set $PATH considerations

Commenting code Anything following ‘#’ is considered a comment and ignored Script Header n

Commenting code Anything following ‘#’ is considered a comment and ignored Script Header n n Tells what the script does Common Entries: • • Author Date written Program name Command line usage • • Purpose Files used Notes/features Revision History

Argument Variables Variable $0 $1 - $9 Purpose Name of the program Command line

Argument Variables Variable $0 $1 - $9 Purpose Name of the program Command line arguments $# Number of Arguments $* All arguments as 1 word $@ Each argument is a word

Other Special Variables Variable $$ $! $? Purpose PID number of script PID number

Other Special Variables Variable $$ $! $? Purpose PID number of script PID number of parent process Exit status of script (Checked by parent)

Basic Input read variable [ < filename ] Examples: n n read ANS read

Basic Input read variable [ < filename ] Examples: n n read ANS read LINE < file (only reads first line)

Basic output echo [ -n ] [ -e ] “string” -n : No new

Basic output echo [ -n ] [ -e ] “string” -n : No new line -e : Allow backslash options Example: echo –n “Enter your name: “

Backslash Options Character a b c Purpose Alert (bell) Backspace Don’t display trailing newline

Backslash Options Character a b c Purpose Alert (bell) Backspace Don’t display trailing newline n r t v \ ### Newline Carriage return Horizontal Tab Vertical Tab Backslash ###: ascii value of character

Expression Evaluation expr command n n n Math expressions ( +, -, *, /,

Expression Evaluation expr command n n n Math expressions ( +, -, *, /, % ) String expressions (match, substring, etc. ) Logic expressions ( &, |, =, etc. ) Example: n COUNT=`expr $COUNT + 1`

Flow Control Need to determine which commands to run based on one or more

Flow Control Need to determine which commands to run based on one or more conditions Useful to catch errors Provides flexibility in code Classic if-then-else model

If-Then condition Basic decision construct if condition then commands fi

If-Then condition Basic decision construct if condition then commands fi

String Tests Expression True if… -z string Length of string is 0 -n string

String Tests Expression True if… -z string Length of string is 0 -n string Length of string is not 0 string 1 = string 2 If the 2 strings are identical string 1 != string 2 If the 2 strings are different string If string is not NULL

Numeric Tests Expression int 1 –eq int 2 True if… int 1 equals int

Numeric Tests Expression int 1 –eq int 2 True if… int 1 equals int 2 int 1 –ne int 2 int 1 and int 2 not equal int 1 –gt int 2 int 1 > int 2 int 1 –ge int 2 int 1 >= int 2 int 1 –lt int 2 int 1 < int 2 int 1 –le int 2 int 1 <= int 2

File Tests Expression -r file -w file -x file -f file -d file -h

File Tests Expression -r file -w file -x file -f file -d file -h file -p file File File exists exists True if… and readable and writable and executable and is a regular file and is a directory and is a symbolic link and is a named pipe

File Tests (Con’t. ) Expression True if… -c file File exists and is a

File Tests (Con’t. ) Expression True if… -c file File exists and is a character device -b file File exists and is a block device -u file File exists and is SUID -g file File exists and is SGID -k file File exists and sticky bit set -s file File exists and size > 0 bytes

Logic Operators Expression ! Purpose Negates the expression -a AND -o OR ( expression

Logic Operators Expression ! Purpose Negates the expression -a AND -o OR ( expression ) Groups the expression

If-Then-Else condition if condition then commands else commands fi

If-Then-Else condition if condition then commands else commands fi

If-Then-Else If condition if condition then commands elif condition then commands fi

If-Then-Else If condition if condition then commands elif condition then commands fi

Case condition Perform a range of tests on a variable case variable in pattern

Case condition Perform a range of tests on a variable case variable in pattern 1) commands ; ; pattern 2) commands ; ; esac *) matches all other cases

Loops Need to run block of code more than once Modes n n n

Loops Need to run block of code more than once Modes n n n Repeat a set number of times Repeat while a condition is true Repeat until a condition is true

for loop Repeat for each variable in list for variable in variable_list do commands

for loop Repeat for each variable in list for variable in variable_list do commands done

while loop Repeat until condition is false while [ condition ] do commands done

while loop Repeat until condition is false while [ condition ] do commands done

Reading Files Read a file one line at a time while read LINE do

Reading Files Read a file one line at a time while read LINE do echo $LINE done < $FILENAME

do-until loop Repeat until condition is true until [ condition ] do commands done

do-until loop Repeat until condition is true until [ condition ] do commands done

Escaping out of loops Sometimes you need to exit loop early n n Error

Escaping out of loops Sometimes you need to exit loop early n n Error conditions Special conditions break command Example: while [ do echo read if [ then 1 ] –n “Done? “ ANS $ANS = “y” ] break fi done

Other scripting languages Perl n n Very popular among sysadmins Many modules available TCL/TK

Other scripting languages Perl n n Very popular among sysadmins Many modules available TCL/TK Expect Python