You are on page 1of 7

Q1.

Linux Scripts does not support comparison using symbols like ( < , >
etc ,) what is the alternative to do this ?

Ans: Linux script doesn’t support comparison using symbols like(<,>,etc).


These above arithmetic expressions can be used as follows:

Expression Alternative
< –lt

<= –l
> –gt
>= –l

The expr command can be used to evaluate any argument as an expression.

syntax: expr op1 operator op2

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. To print Fibonacci series.

b. Print a table of any number

c. Print the day name of the day number given by user.

(a). #!/bin/bash

a=0

b=1

i=1

echo “The Fibonacci Series is:”

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

echo “Enter any Number for table”

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

echo “Enter The day You want to see(1-7)”

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

echo “choose one day”

exit 0

Q3. Contrast the conditional control statement of Linux shell and C.

SYNTAX LINUX SHELL C


IF-ELSE if condition if(condition)
then {
statements statements;
else }
statements else
fi {
statements;
}

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
}

WHILE while condition While (test condition)


do {
Statements Body of loop
Done }

Q4. Illustrate the role of $(()), grave ascent (~) with an example.

Ans 4. The $((...)) is used to evaluate any arithmetic argument as an expression in


the linux shell scripting. $((...)) in the shell scripting helps the user to use
arithmetic operation easily as in the linux scripting every parameter is passed as a
string.

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

echo “Enter any name:”

read name

cd $ name

echo “Enter your roll no:”

read roll

echo “The list of the directory is”

ls -l

cp -name roll

echo “The $k file has been successfully copied to the 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.

Ans 6. $vi sbd

#!/bin/bash

If[$# -eq0]

Then

echo “ no argument is passed”

Else

ls $1
fi

Exit 0

You might also like