Professional Documents
Culture Documents
Linux Scripts does not support comparison using symbols like ( < , >
etc ,) what is the alternative to do this ?
Expression Alternative
< –lt
<= –l
> –gt
>= –l
where op1 and op2 may be any integer no(without any decimal point)and
operator can be :
+ Addition
- Subtraction
/ division
% Modular
\* Multiplication
< Less than
>Greater than
<=Less than or equal to
>= Greater than or equal to
In newer scripts the use of expr is normally replaced with the more efficient $((..))
syntax.
Q2. Write a shell script
(a). #!/bin/bash
a=0
b=1
i=1
echo $a
echo $b
for i in (1 2 3 4 5 6)
do
c=$(($a+$b))
echo $c
a=$b
b=$c
done
exit 0
b) $ vi table.sh
#!/bin/bash
read n
for i in 1 2 3 4 5 6 7 8 9 10
do
t=$(($n*$i))
echo “$t”
done
exit 0
(c). #!/bin/bash
Case 1
Print “Monday”
Case 2
Print “Tuesday”
Case 3
Print “Wednesday”
Case4
print “Thursday”
case 5
print “Friday”
case 6”
print “Saturday
case 7
print “Sunday”
do
break
done
exit 0
switch(var_name)
CASE case var_name in case 1: statement;
condition1 ) break;
statement case 2: statement;
;; break;
condition2 ) default: statement;
FOR For i in [1………10] For(initialization; test
Do condition; increment)
Statement {
Done Body of loop
}
Q4. Illustrate the role of $(()), grave ascent (~) with an example.
Syntax:-
$((expression))
Grave ascent (~) generally represents the home directory in linux shell. It
represents input mode.
Q5. Write a script having same name as your roll number. It should include
following things. It should take two inputs, source and destination. Copy
source directory to destination directory. If it is not a subdirectory it will not
be copied and display an error message for this.
Ans 5. $ vi C2801B53
#!/bin/bash
read name
cd $ name
read roll
ls -l
cp -name roll
exit 0
Q6. Write a program to display the contents of all the files in a directory.
After displaying the content of a file, the program should take input from the
user whether to output the content or not .Name of the directory should be
passed as shell parameter.
#!/bin/bash
If[$# -eq0]
Then
Else
ls $1
fi
Exit 0