LINUX
Variables
process our data/information
kept in computers RAM memory
two types of variables
System variables
Created and maintained by Linux itself and is defined in CAPITAL LETTERS.
User defined variables (UDV)
Created and maintained by user and is defined in
LOWER LETTERS
System Variables
System Variables
$ echo $USERNAME $ echo $HOME
Caution: Do not modify System variable this can some time create problems.
System variables
PS1 variable
Prompt string 1 variable Contains the shell prompt, $ symbol
To change the shell prompt
$PS1=Hello>
<Enter>
Now your prompt becomes
Hello>
PS2 variable
Specifies the value for the secondary prompt Secondary prompt
Displays when an incomplete command is entered
on the command line
Default value is > symbol
Example
$PS2=^
Example - PS2 variable
$ echo This is incomplete ^ close the quotes ^
LOGNAME variable
Users login name echo ${LOGNAME}
Or
echo $LOGNAME
SHLVL variable
Contains the shell level currently working in $echo $SHLVL
1
$sh
this is the login shell
$echo $SHLVL 2 $exit working in the new shell
$echo $SHLVL
1 login shell
env command
View a list of all the exported environment variables and their respective values
$env
<enter>
Rules for Naming variable name (Both UDV and System Variable)
Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character
Don't put spaces on either side of the equal sign when assigning value to variable.
Variables are case-sensitive
Rules for Naming variable name (Both UDV and System Variable)
You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition)
For e.g.
$ vech= $ vech="
Do not use ?,* etc, to name your variable names.
How to print or access value of UDV (User defined variables)
Syntax:
$variablename
For eg. To print contains of variable 'vech'
$ echo $vech
Linux
echo
Displays text or variables value on screen.
Syntax:-
echo [options] [string, variables...]
Options of echo command
-n
Do not output the trailing new line.
-e
escaped \a \b \c \n \r
Enable interpretation of the following backslash
characters in the strings: alert (bell) backspace suppress trailing new line new line carriage return
\t
\\
horizontal tab
backslash
Example of echo command
$ echo -e "An apple a day keeps away \a\t\tdoctor\n"
More about Quotes
There are three types of quotes
'
Double Quotes Single quotes
Back quote
Double Quotes
Anything enclose in double quotes removed meaning of that characters (except \ and $).
echo Hello
$ echo "Today is date"
Can't print message with today's date.
Single quotes
Enclosed in single quotes remains unchanged.
Back quote
To execute command. Example
$ echo "Today is `date`".
it will print today's date
Shell Arithmetic
Use to perform arithmetic operations expr command is used to execute expressions +
\* / %
Examples
$ expr 2 - 1 $ expr 10 / 2 $ expr 20 % 3 # remainder read as 20 mod 3
and remainder is 2)
$ expr 10 \* 3
# Multiplication use \* not * since its wild card)
$ echo `expr 6 + 3`
Exit Status
$? variable of shell is used $ ls $ echo $?
It will print 0 to indicate command is successfu
Arithmetic expression
Calculate the value of an expression Enclose the expansion in $(( )).
Syntax
$((expression))
expr command
echo `expr 10 + 2` Var=`expr 10 + 2`
Example
$ a=25 & b=20 echo $((a+b))
Expression
combination of terms and operators Result of an expression can be an arithmetic or a logical result
Arithmetic result is represented as a string of decimal
digits
Logical result is represented as a true condition or a
false condition
Conditional execution
Test and [ ] command
If constructs
Test command
Specify a condition that can be either true or false
test command or [ expr ] is used to see if an expression is true
Use square brackets [], instead of test command Returns true or false if it is true it return zero(0), otherwise returns nonzero(>0) for false. Syntax:
test expression
[expression]
test command
test $user_name= rani [ $user_name= rani ]
For Mathematics use following operator in Shell Script
For string Comparisons use
Shell also test for file and directory types
Logical Operators
Logical operators are used to combine two or more condition at a time
Simple if construct
used for decision making in shell script If given condition is true then command1 is executed. condition is nothing but comparison between two values, for compression we can use test or [expr ] statements or even
exist status can be also used.
An expression is nothing but combination of values, relational operator (such as >,<, <> etc) and mathematical operators (such as +, -, / etc ).
Simple If
Syntax:
if condition then
command1 if condition is true or if exit status
of condition is 0 (zero) ... fi
if...else...fi
If given condition is true then command1 is executed otherwise command2 is executed. Syntax: if condition then command1 if condition is true or if exit status
of condition is 0(zero)
... else command2 if condition is false or if exit status
of condition is >0 (nonzero)
... fi
Example
if test $1 -gt 0 then echo "$1 number is positive"
else
echo "$1 number is negative" fi
Multilevel if-then-else -Syntax
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
condition2 is zero (true - 0)
execute all commands up to elif statement
Multilevel if-then-else -Syntax
else
None of the above condtion,condtion1,condtion2
are true (i.e. all of the above nonzero or false)
execute all commands up to fi
fi
Logical operators example -a AND -o OR
if [ $a ge 60 -a $a -lt 80 ] then
Echo distinction
fi
exit command
Terminate the execution of a shell script and return to the $prompt
Syntax
exit
case.....esac
construct
Check multiple values of a variable
Syntax:
case $variable_name in value1) command.
;;
value2) command. ;; . ..
*) command;;
esac
Example
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, invalid data";; esac
Linux
Loops
for loop While loop Until loop
for loop Syntax:
for { variable name } in { list } do
execute one for each item in the list until the list is
not finished (And repeat all statement between do
and done)
done
Example
for i in 1 2 3 4 5 do echo "Welcome $i times"
done
While loop
Loop is executed as long as given condition is true
While loop Syntax:
while [ condition ] do
command1 command2 command3 .. ....
done
Example multiplication table of a given number
n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1`
done