An Introduction to Unix Shell Scripting UNIX Shells

  • Slides: 27
Download presentation
An Introduction to Unix Shell Scripting

An Introduction to Unix Shell Scripting

UNIX Shells /bin/sh –Borne Shell /bin/csh – C Shell /bin/ksh – Korn Shell /bin/bash

UNIX Shells /bin/sh –Borne Shell /bin/csh – C Shell /bin/ksh – Korn Shell /bin/bash Borne Again Shell

Unix Shell Scripting The Bourne shell is what we will use for scripting It's

Unix Shell Scripting The Bourne shell is what we will use for scripting It's portable Virtually every Unix system has sh installed It has a rich set of programming constructs It is arguable that it is the most popular UNIX shell.

BASH Comments # is the comment symbol in the shell # can occur anywhere

BASH Comments # is the comment symbol in the shell # can occur anywhere on a line The shell ignores anything following a # until the end-of-line One special exception #! on the first line is used to tell the shell what program to use to interpret the script file Examples: #!/bin/sh -tells shell to use the Bourne shell to execute the script #!/usr/bin/perl -tells shell to use Perl to execute the script

Shell Output echo is the primary way to perform output from the shell Syntax:

Shell Output echo is the primary way to perform output from the shell Syntax: echo [-n] arguments -n - do not append a NEWLINE (suppress the enter) arguments - may be variables or explicit strings Examples echo "Hi there, I'm a Unix whiz kid!" echo "My home directory is $HOME" echo $PATH echo To suppress output from shell commands in a script, redirect the output to /dev/null

Assigning Shell Variables To store values in a shell variable, write the name of

Assigning Shell Variables To store values in a shell variable, write the name of the variable followed by an = followed by the value count=1 my_dir=/home/bobo Note that spaces are NOT allowed on either side of the = Also, the shell has no concept of data types No matter what assignment you make, the shell considers the value as a string of characters Variables don't need to be declared, they're simply assigned values when you want to use then

Referring to Variables In order to refer to the value of a variable, preface

Referring to Variables In order to refer to the value of a variable, preface the variable name with a $ echo $count echo count ¥ displays 1 ¥ displays count echo $my_dir ¥ displays /home/bobo echo my_dir ¥ displays my_dir If you want to ensure a variable is not accidentally modified, specify it as readonly. Further attempts to modify it will result in an error from the shell. my_dir=/home/bobo readonly my_dir

File Name Substitution & Variables If you define a variable as x=* ls $x

File Name Substitution & Variables If you define a variable as x=* ls $x will produce a directory list of all files Did the shell store the * in x or the list of files in your current directory?

File Name Substitution & Variables The shell stored the * in x, the shell

File Name Substitution & Variables The shell stored the * in x, the shell does not perform filename substitution when assigning values to variables What actually happens is: The shell scans ls $x, substituting * for $x It then rescans the line and substitutes all the files in the current directory for the *

Environment Create a file called firstscript that contains: #!/bin/bash x=123 echo the variable x

Environment Create a file called firstscript that contains: #!/bin/bash x=123 echo the variable x equals $x Save it and make it executable (chmod 744 firstscript) Then execute firstscript - what happens?

Variables in the Environment When variables are assigned, they are local to the current

Variables in the Environment When variables are assigned, they are local to the current shell Since scripts are executed in a sub-shell, these local variables aren't visible to the script To make them visible (inherited by) subsequent sub-shells, they must be exported export my_dir The env command lists all currently defined exported variables

Advanced echo The echo command allows for special support of control characters. These special

Advanced echo The echo command allows for special support of control characters. These special control features include such functions as new line, tab and bell To Enable the interpretation of special control(escape) characters. One must use the –e switch echo –e “This will n be on two lines”

echo escaped characters a alert (bell) b backspace c suppress trailing newline f form

echo escaped characters a alert (bell) b backspace c suppress trailing newline f form feed n new line r carriage return t horizontal tab v vertical tab \ backslash nnn The character whose ASCII code is nnn(octal)

Debugging Scripts with -x - When developing scripts, it is often difficult to debug

Debugging Scripts with -x - When developing scripts, it is often difficult to debug them - In order to get a trace of what is happening, you can invoke your script by using the shell's -x option $ $ $ sh -x case. scr This will trace(show the commands) the statements in the script as they are being executed (similar to leaving echo on in batch files)

Command Line Arguments Supposing you ran the scripted called big, using the following command.

Command Line Arguments Supposing you ran the scripted called big, using the following command. big red sea You would be providing your program with two variables. (red and sea) The program can use these variables.

Command Line Arguments When the shell invokes your script, it assigns the command line

Command Line Arguments When the shell invokes your script, it assigns the command line arguments to a set of variables $0, $1, $2, …$9 $0 is the script name $1 is the first argument, $2 the second, up through $9 You can then refer to these variables in your script.

What if you have more then 9 command line arguments? If more than 9

What if you have more then 9 command line arguments? If more than 9 arguments are used, you must access them using the shift command shift simply does a left shift on all the arguments, discarding $1 and making $1 = $2, $2 = $3, etc Note, this means that if you still need $1 you must save it in another variable BEFORE performing the shift

Other Pre-defined Variables $# - number of arguments on command line $* - collectively

Other Pre-defined Variables $# - number of arguments on command line $* - collectively references all of the positional parameters ($1, $2, $3) $0 - name of program being executed $$ - PID of current process $? - exit status of last command not executed in background

Interactive Input Aside from command line arguments input can also be provided by the

Interactive Input Aside from command line arguments input can also be provided by the user as the program in running. to accomplish this simply use the read command

The read Command Syntax: read var 1 var 2 var 3 … Example: read

The read Command Syntax: read var 1 var 2 var 3 … Example: read text This would wait for the user to input a string then the variable text would be set to it. read gets input from STDIN so it can be redirected from a file read text < data

Quoting Quotes in shell scripting have special meaning. There are four types of quoting;

Quoting Quotes in shell scripting have special meaning. There are four types of quoting; - Single Quote ‘ (next to the enter key) removes the special meaning of all enclosed characters - Double Quote " removes the special meaning of all enclosed characters EXCEPT $, `, and

Quoting - <character> removes the special meaning of the character that follows the ;

Quoting - <character> removes the special meaning of the character that follows the ; inside double quotes it removes the special meaning of $ ` " NEWLINE and but is otherwise not interpreted - Back Quote ` (matilda on the ~ key) executes command inside the quotes and inserts standard output at that point.

Examples of Back Quoting echo Your current directory is `pwd` $ Outputs "Your current

Examples of Back Quoting echo Your current directory is `pwd` $ Outputs "Your current directory is /home/bobo" echo There are `who | wc -l` users logged on $ Outputs "There are 13 users logged on"

Single Quotes - Since single quotes protect everything, the following output should make sense:

Single Quotes - Since single quotes protect everything, the following output should make sense: echo ‘`who | wc –l` tells how many users are logged on’ Outputs `who -| wc –l` tells how many users are logged on - But back quotes are interpreted inside “ echo "You have `ls | wc -l` files in your directory" Outputs You have 24 files in your directory

Shell Arithmetic - The Bourne shell has no idea how to do arithmetic -

Shell Arithmetic - The Bourne shell has no idea how to do arithmetic - For example number=2 number=$number + 4 echo $number 2+4 - That makes shell scripting pretty useless as a programming language doesn't it?

expr - Fortunately, there is a Unix command that will allow us to perform

expr - Fortunately, there is a Unix command that will allow us to perform arithmetic within a script - The expr command "evaluates" it's arguments and writes it's output on STDOUT - Example: expr 1 + 2 3 expr 6 / 2 + 5 8

expr Note, since it is evaluating arguments, they must be separated by spaces Also,

expr Note, since it is evaluating arguments, they must be separated by spaces Also, expr only works with integer arithmetic expressions a=1, b=2, c=3 expr $a / $b + $c =3