Unix

Programming with the Shell.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

What is a Shell Script
A shell script is a text file containing a combination of following:

Unix commands (such as ls, cat). Shell commands (such as variable assignment)
and programming constructs (such as if statement and loops)

Since a shell script is a program, it is normally
assigned execute permission using the chmod command.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

To write effective shell scripts, you need to have
working knowledge of basic commands and utilities in Unix. These include:

regular expressions grep, cut, awk and sed utilities

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Executing a shell script

There are many ways of executing a shell script:  By passing the shell script name as an argument to the shell. For example:

sh script1.sh

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Executing a shell script

There are many ways of executing a shell script:  If the shell script has execute permission and is stored in a directory listed in PATH, it can be executed using it’s name. For example:

script1.sh -- If the shell script has execute permission and is stored in a directory listed in PATH, it can be executed using the dot command. For example:
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Tees joints
  

 

Tee uses standard input and standard output It can be placed anywhere in a pipeline It breaks up the input inito two components, one component is saved in a file and the other is connected to the standard output It is used to store the intermediate output of a pipeline Ex:ls -l | tee abc who | tee list | wc -l

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Shell Variables.
Positional Parameters.  Special Parameters.  Named variables

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Positional Parameters.

Acquire values from the position of arguments in command line.  $1, $2, $3,..$9  sh file1 10 20 30

$1

$2

$3

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Special Parameters.

Shell assigns the value for this parameter. $# Number of Command Line Arguments. $0 Command Name. $* Displays all the command line arguments. $? Exit Status. $! Process number of the last background command $@ Same as $*, except when enclosed in double quotes. $$ PID number.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Named Variables.
User-defined variable that can be assigned a value.  Used extensively in shell-scripts.
 

Used for reading data, storing and displaying it

.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Accepting Data.

read.  Accepts input from the user.  Syntax : read variable_name.  Example : read sname

Variable Name

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Display Data.

echo  Used to display a message or any data as required by the user.  echo [Message, Variable]  Example:

echo “ACS” echo $sname
Variable Name

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

test command.
Used extensively for evaluating shell script conditions.  It evaluates the condition on its right and returns a true or false exit status.  The return value is used by the construct for further execution.  In place of writing test explicitly, the user could also use [ ].

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

test command (Contd).
 Operators used with test for evaluating numeral data are:
      -eq Equal To -lt Less than -gt Greater than -ge Greater than or equal to -le Less than or equal to -ne not equal to

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

test command (Contd).
 Operators used with test for evaluating string data are:
 str1 = str2 True if both equals  str1 != str2 True if not equals  -n str1 True if str1 is not a null string  -z str1 True if str1 is a null string

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

test command (Contd).
 Operators used with test for evaluating file data are: -f file1 file. -d file1 True if file1 exists and is a regular True if file1 exists and is directory.

-s file1 True if file1 exists and has size greater than 0 -r file1 True if file1 exists and is readable. -w file1 True if file1 exists and is writable. -x file1 True if file1 exists and is executable.
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Logical Operators.
 Logical Operators used with test are:
! -o Negates the expression. logical ‘or’ operator.

-a logical ‘and’ operator.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

expr command.
Used for evaluating shell expressions.  Used for arithmetic and string operations.  Example : expr 7 + 3 Operator has to be preceded and followed by a space.

would give an output 10.

When used with variables, back quotes need to be used.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Conditional Execution.
&&  The second command is executed only when first is successful.  command1 && command2  ||  The second command is executed only when the first is unsuccessful.  command1 || command2

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Program Constructs
    

if for while until case

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

if statement.
 Syntax.
if control command then <commands> else <commands> fi

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

for statement.
 Syntax.
for variable-name in value1 value2 ....

do <commands> done

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

while statement.
 Syntax.
while control command do <commands> done

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

until statement.
 Syntax.
until control command do <commands> done

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

case statement.
The symbols ;; are used as option terminators.

 Syntax.
case value in choice1) commands ;; choice2) commands ;; .... .... esac

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Useful Shell Scripting commands.
 break  To come out of a loop.  continue  To jump to the start of loop.  exit  To prematurely terminate a program.  #  To interpret the rest of line as comments.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

export command.

export  To make a variable a part of environment and also be accessible to the child shell. export variable_name

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Customizing Your Environment

To customize the environment various built-in shell variables are available. To change the values of variables permanently , define it in .profile file. The .profile File  the Korn shell reads and runs this file whenever you log in to your system  Various environment variables can be defined in this file  Alias can be defined in .profile file

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Aliases

Alias is a synonym for a command or command string Syntax: alias new=original

Ex:alias search=grep alias cdnew=‘cd /xyz/x1/x2’ >Quotes are necessary if the string being aliased consists of more than one word >it is possible to alias an alias, aliases are recursive Ex:alias c=cdnew
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Aliases

type alias without any arguments, to get a list of all the aliases you have defined as well as several that are built-in. The command unalias name removes any alias definition for its argument

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

set command.

set command  Used for display all the environment variables.  Shows the current values of system variables.  Also allows conversion of arguments into positional parameters.  Syntax : set

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Shell Variables

Shell variables can specify everything from your prompt string to how often the shell checks for new mail built-in variables have names in all capital letters The syntax for defining variables is varname=value

 

if the value is more than one word, it must be surrounded by quotes

To delete a variable type the command unset varname
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

System Variables or Built-in Variables

PATH  Search path referred by Unix for any command.  echo $PATH

HOME  Indicates the home directory for the user.  echo $HOME

HISTFILE - Name of history file, on which the editing modes operate.
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

System Variables (Contd).

PS1  Used for displaying & changing the primary prompt.  echo $PS1

PS2  Used for changing the secondary prompt.

MAIL  Name of file to check for incoming mail (i.e., your mail file)
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

System Variables (Contd).
SHELL  Pathname of the shell you are running  PWD  Current directory  ENV  Name of file to run as environment file when shell is invoked  HISTSIZE  Number of lines kept in history file

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

System Variables (Contd)

MAILCHECK  How often, in seconds, to check for new mail (default 600 seconds, or 10 minutes)

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

The Environment File

Although environment variables will always be known to subprocesses, the shell must define which other variables, options, aliases, etc., are to communicated to subprocesses. The way to do this is to put all such definitions in a special file called the environment file instead of your .profile.

1. Decide which definitions in your .profile you want to propagate to subprocesses. Remove them from .profile and put them in a file you will designate as your environment file.
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

The Environment File

2. Put a line in your .profile that tells the shell where your environment file is: ENV=envfilename 3 . For the changes to take effect, type either . .profile or login. In either case, your environment file will be run when the shell encounters the ENV= statement.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Functions
  

A function is sort of a script-within a-script Functions improve the shell's programmability significantly To define a function, you can use either one of two forms: function functname { shell commands }

or: functname () { shell commands }
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Functions
to delete a function definition issue command unset -f functname.  To find out what functions are defined in your login session functions

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

String Operators

string operators let you do the following:  Ensure that variables exist (i.e., are defined and have nonnull values)  Set default values for variables  Catch errors that result from variables not being set  Remove portions of variables' values that match patterns

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Syntax of String Operators

Operator

Substitution

${varname:-word} If varname exists and isn't null, return its value; otherwise return word. Purpose Returning a default value if the variable is undefined. Example: ${count:-0} evaluates to 0 if count is undefined.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Syntax of String Operators
${varname:=word} If varname exists and isn't null, return its value; otherwise set it to word and then return its value Purpose: Setting a variable to a default value if it is undefined. Example: ${count:=0} sets count to 0 if it is undefined.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Syntax of String Operators
${varname:?message} If varname exists and isn't

null, return its value; otherwise print varname: followed by message, and abort the current command or script. Purpose: Example: {count:?" undefined!" } prints "count: undefined!" and exits if count is undefined.
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Catching errors that result from variables being undefined.

${varname:+word} If varname exists and isn't null, return word; otherwise return null. Purpose: Testing for the existence of a variable. Example: ${count:+1} returns 1 (which could mean "true") if count is defined.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Integer Variables and Arithmetic

The shell interprets words surrounded by $(( and )) as arithmetic expressions. Variables in arithmetic expressions do not need to be preceded by dollar signs Korn shell arithmetic expressions are equivalent to their counterparts in the C language Table shows the arithmetic operators that are supported. There is no need to backslash-escape them, because they are within the $((...)) syntax.

The assignment forms of these operators are also permitted. For example, $((x += 2)) adds 2 to x and stores the result back in x.
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Arithmetic Operators
           

Operator + * / % << >> & | ~ ^ Plus Minus

Meaning

Times Division (with truncation) Remainder Bit-shift left Bit-shift right Bitwise and Bitwise or Bitwise not Bitwise exclusive or
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Relational Operators
        

Operator < > <= >= == != && ||

Meaning Less than Greater than Less than or equal Greater than or equal Equal Not equal Logical and Logical or

Value 1 is for true and 0 for false Ex:- $((3 > 2)) has the value 1 $(( (3 > 2) || (4 <= 1) )) also has the value 1

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Arithmetic Variables and Assignment

The ((...)) construct can also be used to define integer variables and assign values to them. The statement: (( intvar=expression )) The shell provides a better equivalent: the built-in command let. let intvar=expression

there must not be any space on either side of the equal sign (=).

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

I/O Redirectors
Redirector > file < file cmd1 | cmd2 >> file >| file <> file n> file Function Direct standard output to file Take standard input from file Pipe; take standard output of cmd1 as standard input to cmd2 Direct standard output to file; append to file if it already exists Force standard output to file even if noclobber set Use file as both standard input and standard output Direct file descriptor n to file

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

I/O Redirector
2>&1 says, "send standard error (file descriptor 2) to the same place as standard output (file descriptor 1) ex:- cat abc 2>&1 standard output ex:- cat abc 2> xyz , cat abc 2>1 (where, abc is not-existing )  In other examples the error message is not displayed on

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

The redirector <> is mainly meant for use with device files (in the /dev directory), i.e., files that correspond to hardware devices such as terminals and communication lines. Low-level systems programmers can use it to test device drivers

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

read
 read command, allows you to read values into shell variables Options to read  Option Function  -p prompt it prompts for input  -n nchars it restrict no. of characters to a variable. EX:read -p "enter value for x " x read -n 5 x

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Handling Signals

Signals are sent to processes by UNIX kernel in response to certain events. Most signals cause the process receiving them to terminate abruptly. However, if you have set a ``trap'' for the signal, you can use them to recover from the emergency.

Shell recognizes a number of signals. However, not all of them can be trapped.

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Signals
A signal is a message that one process sends to another when some abnormal event takes place or when it wants the other process to do something  signal is another way of processes to communicate with each other.  To get list of all the signals on your system kill -l

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Some signals are: Value Signal Description -------- -------- ----------------------------------------------0 EXIT Exit from the shell. 1 HUP A signal used by the shell, indicating that the standard output has hung up; sending this signal logs you out. 2 INT Sent by the Control-C (intr) keystroke; sends an interrupt to the current program. 3 QUIT Sent by the Control-\ keystroke; causes the current program to abort, leaving behind a core dump (for use in program debugging).

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Value Signal ----------------9 15 KILL

Description ---------------------------------------------Cannot be trapped or ignored; forces the die. in preference to

receiving program to signal should be used the receiving program can

TERM Terminates the receiving program. This catch it and carry

Signal 9, as

out clean up tasks such as closing open 9 forces the process to terminate immediately.

files; Signal

© Affiliated Computer Services, Inc. (ACS) 2007, 2008

kill

kill is used to send a signal to any process you creatednot just the currently running job kill takes as argument the process ID, job number, or command name of the process to which you want to send the signal.

Ex:$ kill %1 $ kill -QUIT %1 $ kill -KILL %1 $ kill -QUIT 2389
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Thank You!
© Affiliated Computer Services, Inc. (ACS) 2007, 2008

Sign up to vote on this title
UsefulNot useful