UNIT 2 Linux
UNIT 2 Linux
In general:
• Shell is a hard protective .
In Linux:
• A shell is a program which takes the command typed
by the user
and converts it into the machine’s understandable
format.
• Shell acts as an interpreter between user and the
operating system
• What is Shell 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.
• 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
the shell recognizes the special output redirection character > and takes the
next word on the command line as the name of the file that the output is to
be redirected to. In this case, the file is reminder. If reminder already exists
and you have write access to it, the previous contents are lost (if you don't
have write access to it, the shell gives you an error message).
Shell Responsibilities
Pipeline Hookup
• A Unix pipe provides a one-way flow of data.
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.
$ bash script_name
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
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
command
the result of a command to a string
Without command substitution
• The command substitution
$ operator that converts the
output of a command to a string is a dollar
(command sign and a set of
string
parentheses. )With command substitution
Shell Commands
• The commands used inside the shell for programming
tasks are
known as the shell commands, those are:
1)expr command
•The expr utility is used to evaluate arithmetic operations,
as shell doesn’t support arithmetic operators.
• Expression will be evaluated by expr utility and the
result is sent to
the standard output.
• Special symbols such as *,>,< must be preceded by a \,
otherwise the
shell treats it as a meta-character and yields wrong
results
Example: $ i = 10 ; j = 10
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
a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
Shell Commands
6) Aliases
• An alias provides a means of creating customized
commands by assigning a name to a command.
• An alias is created by using the alias command. Its
format is
alias name=command-definition
Example :
Using alias to rename the list command
$ alias dir=ls
$ dir
alias of command with options
$alias dir=‘ls –l’
$ dir
Listing aliases
$alias
The Environment
Environmental Variables
• Environmental variables control the user environment.
• Environmental variables are set by the environment, the
operating system, during the startup of your shell.
• Reading environment variables same as shell variables.
For example :
an environment variable named HOME, you can
access the value of this variable by using the dollar($)
sign, for example $HOME
• The environmental variables are in uppercase.
• Commonly used environmental variables are:
• CDPATH, HISTFILE,
• HOME, LOGNAME,
Quoting
• Shell uses a selected set of metacharcters in commands.
• Metacharcters are characters that have a special
interpretation.
• For example the pipe (|) .
• In order to use them as a normal text we must tell the shell
interpreter that they should be interpreted differently.
Quote
• This is called quoting the metacharcters.
• To achieve quoting sthere are three metacharcters
collectively as quotes, Double Single
Backsla
Quote Quote
sh
s s
Quoting
Backslash
• The backslash metacharcter (\) is used to change the
interpretation of the character that follows it, i.e., it converts
a literal character into a special character and vice versa.
Example:
• The character n is interpreted as a literal character by the
shell to change its interpretation as a newline character we
use backslash before it (\n).
• Similarly the use of a greater than symbol (>) in a command
is interpreted as a special character (output redirection), to
change its interpretation as a literal text we use a backslash
before it (\>)
Example:
$ echo “Metacharcters are : >,<,?,|,&“
Metacharcters are : >,<,?,|,&
$ echo a = 10
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
conditional execution. It is used for:
• Numeric comparison
• File attributes comparisons
• Perform string comparisons.
test command syntax
test condition
OR
test condition && true-command
OR
test condition || false-command
OR
test condition && true-command || false-
command
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:
5. loops
a. for
b. While
c. Until
2. Handling of signals.
Control Structures
• THE SIMPLE IF STATEMENT
if [ condition ]; then
statements
fi
if [ condition ]; then
statements-1
else
statements-2
fi
if [ condition ]; then
statements
elif [ condition ]; then
statement
else
statements
fi
• 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
$set -x
Step 2 : write below code in any editor(gedit) and name file as set.sh
x=10
while [ $x -gt 0 ]; do
x=$[ $x-1 ]
echo $x
sleep 2
done
$chmod +x set.sh