0% found this document useful (0 votes)
108 views16 pages

Assignment-3 11500116044

The document provides 15 examples of shell script programs using bash to perform tasks like printing patterns, calculating factorials and powers, sorting arrays, checking palindromes, and more. Each example includes the code to perform the task and provides an output or result. The shell scripts demonstrate basic programming concepts like loops, functions, conditionals, and arrays.

Uploaded by

Sayantan Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views16 pages

Assignment-3 11500116044

The document provides 15 examples of shell script programs using bash to perform tasks like printing patterns, calculating factorials and powers, sorting arrays, checking palindromes, and more. Each example includes the code to perform the task and provides an output or result. The shell scripts demonstrate basic programming concepts like loops, functions, conditionals, and arrays.

Uploaded by

Sayantan Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

BASIC SHELL PROGRAMMING(using bash <filename>.

sh command at terminal)
Running Environment - LinuxMint

Outputs(Along with Code)-

1. Write shell script using for loop to print the following patterns on screen

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$i"
done
echo ""
done

2. Write shell script using for loop to print the following patterns on screen
for (( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done

3. Write shell script using for loop to print the following patterns on screen

for (( i=1; i<=9; i++ ))


do
for (( j=9; j>=i; j-- ))
do
echo -n " "
done
for (( k=1; k<=i; k++ ))
do
echo -n " $i"
done
echo ""
done

4. Write a Shell Script to find factorial of a number.

n=0
on=0
fact=1

echo -n "Enter number to find factorial : "


read n

on=$n

while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done

echo "Factorial for $on is $fact"

5. Write a Shell Script to find "a" to the power "b" using Function.

read -p "Enter a: " a


read -p "Enter b: " b
pow=1
for (( i=1; i<=b; i=i+1 ))
do
pow=$(( $pow * $a ))
done
echo $pow

6. Write a Shell Script to find Armstrong number between 1 to 500.


i=1
while((i<=500))
do
c=$i
d=$i
b=0
a=0

while((c>0))
do

a=$((c%10))

b=$((b + a*a*a))
c=$((c/10))
done
if((b==d)); then
echo "$i"
fi
i=$((i+1))
done

7. Write a Shell Script to Display numbers Using Array.

for((i=0;i<5;i++))
do
echo "enter `expr $i + 1` number"
read arr[$i]
done
echo "the numbers you have entered are"
for((i=0;i<5;i++))
do
echo ${arr[$i]}
done
8. Write a Shell Script to add two numbers using Function.

add()
{
x=$1
y=$2
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}

echo "enter first number"


read first
echo "enter second number"
read second

add $first $second

9. Write a Shell Script to implement Bubble Sort.

echo "enter maximum number"


read n
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done

echo "Numbers in an array are:"


for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done

for (( i = 0; i < $n ; i++ ))


do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done

echo "\nSorted Numbers "


for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
10. Write a Shell Script to swap two numbers without using third variable.

echo "enter first number"


read a
echo "enter second number"
read b
echo "a before swapping is $a and b is $b"

a=$((a+b))
b=$((a-b))
a=$((a-b))
echo "a after swapping is $a and b is $b"

11. Write a Shell Script to Sort Number in Descending Order.

echo "enter maximum number"


read n

echo "enter Numbers in array:"


for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done

echo " Numbers in an array are:"


for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done

for (( i = 0; i < $n ; i++ ))


do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -lt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done

echo "\nSorted Numbers "


for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
12. Write a Shell Script to implement Insertion Sort.

echo "enter the max limit"


read n
echo "enter number in an array"
for((i=0;i<n;i++))
do
read arr[$i]
done

for((i=1;i<n;i++))
do
j=$i-1
temp=${arr[$i]}
while((j>=0 && arr[j]>temp))
do
arr[$j+1]=${arr[$j]}
j=$j-1
done
arr[j+1]=$temp
done

echo "printing sorted array"


for((i=0;i<n;i++))
do
echo ${arr[$i]}
done
14. Write a Shell Script to Print Fibonacci Series.

c=0
a=1
b=1
read -p "Enter limit of fibonacci Series:" n
echo -n "$a "
echo -n "$b "
while((c<n))
do
c=$((a+b))
echo -n "$c "
a=$b
b=$c
done

15. Write a Shell Script to check whether a given String is Palindrome or not.

echo -n "Enter the String:"


read n
len=${#n}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${n:$i:1}"
c2="${n:$len-$i-1:1}"

if [ $c1 != $c2 ];then


flag=0
echo "String is not palindrome"
break
fi
done
if(( $flag==1)); then
echo "Input String is Palindrom"
fi

You might also like