Shell Scripting 2
Shell Scripting 2
Variables
System variables
System Variables
System Variables
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
<Enter>
PS2 variable
LOGNAME variable
Or
echo $LOGNAME
SHLVL variable
1
$sh
$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.
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="
Syntax:
$variablename
Linux
echo
Syntax:-
-n
-e
escaped \a \b \c \n \r
\t
\\
horizontal tab
backslash
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
Back quote
Shell Arithmetic
\* / %
Examples
and remainder is 2)
$ expr 10 \* 3
$ echo `expr 6 + 3`
Exit Status
Arithmetic expression
Syntax
$((expression))
expr command
Example
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
If constructs
Test command
test expression
[expression]
test command
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
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
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
Example
else
echo "$1 number is negative" fi
if condition
then
condition is zero (true - 0)
elif condition1
condition1 is zero (true - 0)
elif condition2
condition2 is zero (true - 0)
else
None of the above condtion,condtion1,condtion2
fi
if [ $a ge 60 -a $a -lt 80 ] then
Echo distinction
fi
exit command
Syntax
exit
case.....esac
construct
Syntax:
case $variable_name in value1) command.
;;
value2) command. ;; . ..
*) command;;
esac
Example
case $rental in
Linux
Loops
and done)
done
Example
for i in 1 2 3 4 5 do echo "Welcome $i times"
done
While loop
while [ condition ] do
command1 command2 command3 .. ....
done
done