Part – 2:
Q) How we get the PID of current
shell ?
Ans - $ echo “$$”<Enter –
Key>
Q) How we get the exit status of
last command ?
Ans - $ echo “$?”<Enter –
Key>
Q) How we get the process number of
last back ground process ?
Ans - $ echo “$!”<Enter –
Key>
Q) What are different types of Unix
Special Variable ?
Ans –
$0 – Return Current Script File
Name
$1 – Return 1st Command
line argument
$2 – Return 2nd Command
line argument
$n – Return nth Command line argument
$# - Return total number of Command
line arguments
$@ or $* - Return all command line
arguments within double quotes.
Example –
vi script6.sh
#! /bin/sh
echo “The PID of current Shell: $$”
echo “The total number of command
line arguments: $#”
echo “The 1st command
line argument: $1“
echo “The 2nd command
line argument: $2”
echo “The name of your script: $0”
ESC + :wq<Enter – Key>
$ sh script6.sh 23 12<Enter –
Key>
Output –
The PID of current shell: 2354
The total number of command line
arguments: 2
The 1st command line
argument: 23
The 2nd command line
argument: 12
The name of your script: script6.sh
Q) What are the operators in Shell
Script ?
Ans – There are 5 operators present
in shell script. Such as –
1. Arithmetic Operator
2. Relational Operator
3. Boolean Operator
4. String Operator
5. File Test Operator
Q) What are arithmetic operators ?
Ans –
+ -> `expr $a + $b` Addition
Operator
- -> `expr $a - $b` Subtraction
Operator
* -> `expr $a \* $b` Multiplication
Operator
/ -> `expr $a / $b` Division
Operator
% -> `expr $a % $b` Modulus
Operator
= -> c=`expr $a + $b` Assignment
Operator
== -> if [ $a == $b ] Comparison
Operator – if both string or character are equal return true otherwise false
!= -> if [ $a != $b ] Comparison
Operator - if both string or character are not equal return true otherwise
false
Where a and b are operands.
Example –
#! /bin/sh
a=45
b=23
s=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
d=`expr $a / $b`
mod=`expr $a % $b`
echo “Sum = $s”
echo “Subtract = $sub”
echo “Multiplication = $mul”
echo “Division = $d”
echo “Modulus = $mod”
Q) What are Relational operators ?
Ans –
For numbers, it operates.
-eq -> equal operator
-ne -> Not equal operator
-gt -> greater than operator
-ge -> greater than equal to
-lt -> less than
-le -> less than equal to
Example
a=34
b=21
if [ $a –eq $b ]
Q) What are Boolean operators ?
Ans –
-o (logical OR) – It takes multiple
conditions, any one condition is true, then total expression is true.
-a (logical AND) – It takes
multiple conditions and returns true if all the conditions are true.
Q) What are String Operators ?
Ans –
= -> check if the value of two
string operands are equal or not
!= -> check if the value of
string operands are not equal if yes return true else false
-z -> Check if the size of
operand is zero if yes return true else false.
-n -> check if the size of operand
is non – zero if yes returns true else false.
Q) What are File Test Operators ?
Ans –
If [ -r $FileName ] -> checks if
the file is readable or not
If [ -w $FileName ] -> checks if
the file is writable or not
If [ -x $FileName ] -> checks if
the file is executable or not
If [ -f $FileName ] -> checks if
the file is ordinary file or not
If [ -d $FileName ] -> checks if
the file is directory file or not
If [ -e $FileName ] -> checks if
the file name any other files or directories exists or not
If [ -s $FileName ] -> checks if
the size of file is non – zero or not
No comments:
Post a Comment