For Loop: -
Q) Write a shell script to print 1
to 10 in for loop ?
Ans –
1st Way -
#! /bin/sh
for i in 1 2 3 4 5 6 7 8 9 10
do
echo “$i”
done
OR
2nd Way –
#! /bin/sh
for i in {1..10}
do
echo “$i”
done
OR
3rd Way –
#! /bin/sh
for ((i=1; i<=10; i++))
{
echo “$i”
}
Until Loop: -
Q) What is until loop ?
Ans –
1. This
loop is just opposite of while loop.
2. The
statement written in until block condition if false, then while block get
executed otherwise not executed.
Q) Print 1 to 10 using until loop ?
Ans –
#! /bin/sh
i=1
until [ $i –gt 10 ]
do
echo “$i”
i=`expr $i + 1`
done
Break Statement: -
Q) What is break statement ?
Ans –
This keyword is used to terminate
the loop and come out from the loop even though the condition is still
satisfying.
Example – 1: -
#! /bin/sh
i=1
while [ $i –le 10 ]
do
echo “$i”
if [ $i –eq 5 ]
then
break
fi
i=`expr $i + 1`
done
Output – 1 2 3 4 5
Example – 2: -
#! /bin/sh
for ((i=1; i<=10;i++))
{
for
((j=1;j<=10;j++))
{
echo
–n “$j”
if
[ $j –eq 5 ]
then
break
2
fi
}
}
Output – 1 2 3 4 5
Continue Statement: -
Q) What is continue statement ?
Ans –
This keyword is used to skip the
remaining statements and start the next iteration of the loop.
Example –
#! /bin/sh
i=0
while [ $i –le 10 ]
do
i=`expr
$i + 1`
if
[ $i –eq 5 ]
then
continue
fi
echo
“$i”
done
Select Loop: -
Q) Write an example of select loop to
understand it ?
Ans –
$ vi SelectExample.sh
#! /bin/sh
select drink in tea coffee juice
water all none
do
case
$drink in
tea
| coffee | water)
echo
“Go to caferteria”
;;
Juice)
echo
“Available at juice counter in cafeteria”
;;
all)
echo
“Available at home or outside”
;;
None)
Break
;;
*)
echo
“Invalid Option”
;;
esac
done
ESC + :wq<Enter – Key>
$ chmod 755 SelectExample.sh
$./SelectExample.sh
1. Tea
2. Coffee
3. Water
4. Juice
5. All
6. None
Enter Value: 1
Go to cafeteria
Enter Value: 8
Invalid Option
Enter Value: 6
$
Fibonacci Series: -
Q) Write a script to print Fibonacci
series for first 20 elements.
0 1 1 2 3 5 8 13 …
Ans –
#! /bin/sh
i=0
j=1
count=3
echo “Fibonacci Series: $i $j “
while [ $count –le 20 ]
do
c=`expr $i + $j`
echo “$c”
i=$j
$j=$c
count=`expr $count + 1`
done
Command Substitution: -
Q) Write examples of command
substitution ?
Ans –
Example – 1:
#! /bin/sh
x=`ls –lrt`
echo “Listing of current
directories: “
echo $x
Example – 2:
#! /bin/sh
x=`date`
y=`who`
z=`ls`
m=`ps`
echo “Current date is $x”
echo “Logged in users are: “
echo $y
echo “listing will be: “
echo $z
echo “Processes will be: “
echo $m
Array: -
Q) Write a script to read all
elements of array and print it ?
Ans –
#! /bin/sh
a=(1 ‘mm’ ’23-Apr-2019’)
len=${#a[@]}
##length of array
for ((i=0;i<=len;i++))
{
echo “${a[i]}”
}
Q) Write a script to read input of
array in run time and print it ?
Ans –
#! /bin/sh
echo “Enter the length of the
array: “
read leng
echo “Enter Array Elements: “
for ((i=0;i<leng;i++))
{
echo “Enter array `expr $i + 1`
element: “
read ${a[i]}
}
echo “The array elements are: “
for ((i=0;i<leng;i++))
{
echo “${a[i]}”
}
Assigning a string to array: -
Q) Assign the string 1234 5678
1223456 to an array and print it.
Ans –
#! /bin/sh
str=”1234 5678 1223456”
IFS=’ ‘read –a arr <<<
${str}
###IFS = Internal Field Separator
len=${#arr[@]}
###length of array
echo “Array Elements are: “
for ((i=0;i<=len;i++))
{
echo ${arr[i]}
}
Reading from a file:
Q) Take the filename from command
prompt and read it line by line.
Ans –
$ vi script10.sh
#! /bin/sh
While read line
do
echo $line
done < $1
ESC + :wq<Enter – Key>
$ chmod 755 script10.sh
$ ./script10.sh test.dat<Enter –
Key>
Q) Take the user input as file and
show the lines of the file with line number will be the first character present
for all lines.
Ans –
#! /bin/sh
echo “Enter the file name: “
read fn
i=1
while read line
do
echo “$i. $line”
i=`expr $i + 1`
done < $fn
No comments:
Post a Comment