Unit 2 Linux
Unit 2 Linux
In general:
• Shell is a hard protective .
In Linux:
• When you log into the system you are given a default shell.
• When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command
aliases,
and executes any commands specified in these.
• Each time you type in a line to the shell, the shell analyzes the
• The shell scans this command line and determines the name of
•This means that when the shell processes this command line:
it passes four arguments to the echo program: when, do, we, and eat?
when do we eat?
Shell Responsibilities
Variable and Filename Substitution
• Like any other programming language, the shell lets you assign
values to variables.
•Whenever you specify one of these variables on the command
line,
preceded by a dollar sign, the shell substitutes the value
assigned
to the variable at that point.
variable=value
Displaying the values
echo $variable
then the Unix shell would create three processes with two pipes
between them:
Shell Responsibilities
Interpreted Programming Language
• The shell has its own built-in programming language.
statement in the language one line at a time and then executes it.
• This done by specifying the shell before the script name as in the
following example:
$ bash script_name
• In this case, the interpreter designator line is not needed, but the
user needs to know which shell the script requires.
Creating a Script
• To create a shell script first use a text editor to create a file
containing the commands.
Script Components
• Interpreter Designator Line
• The first line of the script is the interpreter designator line.
• It tells LINUX the path to the appropriate shell interpreter.
• The designator line begins with a pound sign and a bang (#!).
• If it is omitted ,Linux uses current shell’s default interpreter.
#!/bin/bash or #!/usr/bin/bash
The Shell as a Programming Language
Comments
• Comments are documentation we add in a script to help us
understand it.
• The interpreter doesn’t use the at all; it simply skips over them.
• Comments are identified with the pound sign token (#).
Myfile.scr
Parameter Meaning
$0 Name of the current shell script
$1-$9 Positional parameters 1 through 9
$# The number of positional parameters
$* All positional parameters, “$*” is one string
$@ All positional parameters, “$@” is a set of strings
$? Return status of most recently executed command
$$ Process id of current process
Command Substitution
•When a Shell executes a command, the output is directed to
standard
output( most of the time, standard output is associated with monitor
• Shell allows the standard output of one command to be used as an
argument of another command
• Some times we need to change the output to a string that we can
store
in another string or a variable .
• Command substitutions provides the capability to convert the result
of a command to a string
• The command substitution operator that converts the output of a
command to a string is a dollar sign and a set of parentheses.
command
Without command substitution
$(command) string
Example: $ i = 10 ; j = 10
$ echo `expr $i + $j`
20
$ echo `expr $i \* $j`
100
Shell Commands
2) who | sort
• who command displays who logged onto the system and provides
an account of all users
• It displays a three column output, the first column displays the
user-ids of users currently working on the system.
• The second column displays the system name.
• The third column displays the date and time.
• Sort command sorts text files. It sorts all the lines from the
standard input.
• who | sort before displaying the who output on the screen it is
piped to sort.
• Sort receives the output of who as standard input, it the sorts the
output according to the first alphabet in each line and displays it
on screen
Shell Commands
3) ls | wc –l
Example:
$ echo “Metacharcters are : >,<,?,|,&“
Metacharcters are : >,<,?,|,&
$ echo a = 10
$ echo “a is $a and single quote is ‘b’ “
a is 10 and single quote is ‘b’
Quoting
Single Quotes
Example:
$ echo a = 10
$ echo ‘characters are < > “b” $a ? &’
characters are < > “b” $a ? &
TEST Command
• The test command is used to check file types and compare values. Test is used in
Sample Output:
Yes
TEST Command
Numeric comparison
• Numerical tests are implied when comparison between values of two
numbers is to be done.
• Different numerical operators are :
Numerical Comparison Operators Used by test
Operator Syntax Description
-s file True if file exists and has a size greater than zero
1. if
2.if –else
3.Nested if
4.case
Control Statements:
1. loops
a. for
b. While
c. Until
2. Handling of signals.
Control Structures
• THE SIMPLE IF STATEMENT
if [ condition ]; then
statements
fi
• Executes the statements only if condition is true
Control Structures
• THE IF-THEN-ELSE STATEMENT
if [ condition ]; then
statements-1
else
statements-2
fi
• executes statements-1 if condition is true
• executes statements-2 if condition is false
Control Structures
• THE NESTED IF
if [ condition ]; then
statements
elif [ condition ]; then
statement
else
statements
fi
• The word elif stands for “else if”
• It is part of the if statement and cannot be used by itself
Control Structures
THE CASE STATEMENT
• use the case statement for a decision that is based on multiple choices
Syntax:
case word in
pattern1) command-list1
;;
pattern2) command-list2
;;
patternN) command-listN
;;
esac
case pattern
• checked against word for match
may also contain:
*
?
[ … ]
[:class:]
multiple patterns can be listed via:
|
Control Structures
THE WHILE LOOP
• Purpose:
To execute commands in “command-list” as long as “expression”
evaluates to true
Syntax:
while [ expression ]
do
command-list
done
Control Structures
THE UNTIL LOOP
• Purpose:
To execute commands in “command-list” as long as “expression”
evaluates to false
Syntax:
until [ expression ]
do
command-list
done
Control Structures
THE FOR LOOP
• Purpose:
To execute commands as many times as the number of words in
the “argument-list”
Syntax:
for variable in argument-list
do
commands
done
Control Structures
Signals on Linux
% kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
^C is 2 - SIGINT
Arithmetic in Shell
Shell Arithmetic
Syntax:
expr op1 math-operator op2
Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`
Note:
expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.
expr 10 \* 3 - Multiplication use \* and not * since its wild card.
Functions
• A shell function is similar to a shell script
• stores a series of commands for execution later
• shell stores functions in memory
• shell executes a shell function in the same shell that called it
• Where to define
• In .profile
• In your script
• Or on the command line
• Remove a function
• Use unset built-in
Functions
• must be defined before they can be referenced
• usually placed at the beginning of the script
Syntax:
function-name () {
statements
}
Functions
Function parameters