0% found this document useful (0 votes)
31 views

Shell Script Examples

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

Shell Script Examples

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

Shell Script to demonstrate use of "for loop"

#!/bin/bash

for (( counter=10; counter>0; counter-- ))

do

echo -n "$counter "

done

printf "\n"

Shell Script to demonstrate use of "if statement"

#!/bin/bash

n=10

if [ $n -lt 10 ];

then

echo "This is one digit number"

else

echo "This is two digit number"

fi

Shell Script to demonstrate use of "for loop"

#!/bin/bash

for (( counter=10; counter>0; counter-- ))

do

echo -n "$counter "

done

printf "\n"

Shell Script to demonstrate use of "While loop"


#!/bin/bash

a=true

count=1

while [ $a ]

do

echo $count

if [ $count -eq 10 ];

then

break

fi

((count++))

done

Program to calculate profit or loss

echo
"input the cost price of an item"

read cp

echo "input the selling price of


the item"

read sp

if [ $cp -eq $sp ]

then

echo "no profit or no gain"

fi

if [ $cp -gt $sp ]

then

s=$((cp - sp))
echo "loss of rs:$s"

else

s=$((sp - cp))

echo "profit of rs:$s"

fi

Shell script to print a Rectangle

#!/bin/bash
echo "enter value of n"

read rows

echo "enter value of m"

read column

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

do

for((j=1; j<=column; j++))

do

echo -n "* "

done

echo

done

shell script to print a square

#!/bin/bash
echo "enter value of n"

read rows

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

do

for((j=1; j<=rows; j++))

do

echo -n "* "

done

echo

done

shell script to print a triangle

echo "enter value of n"

read rows

#-n -> allows not to append to next line

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

do

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

do

echo -n "* "

done

echo

done

Shell script to print Diamond pattern

echo "enter value of n "

read num
for (( i=1;i<=$num ;i++))

do

for (( j=$num;j>=i;j-- ))

do

echo -n " "

done

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

do

echo -n " *"

sum=`expr $sum + 1`

done

echo ""

done

d_max=`expr $num - 1`

for (( i=$d_max;i>=1;i--))

do

for (( j=i;j<=$d_max;j++ ))

do

if [ $j -eq $d_max ]

then

echo -n " "

fi

echo -n " "

done

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

do

echo -n " *"

sum=`expr $sum + 1`
done

echo ""

done

Write a shell script to find a number is Armstrong or not.

Steps

1. Start

2. read number

3. set sum=0 and temp=number

4. reminder=number%10

5. sum=sum+(reminder*reminder*reminder)

6. number=number/10

7. repeat steps 4 to 6 until number > 0

8. if sum = temp

9. display number is armstrong

10. else

11. display number is not armstrong

12. stop

echo "Enter the number"

read n
function ams

t=$n

s=0

b=0

c=10

while [ $n -gt $b ]

do

r=$((n % c))

i=$((r * r * r))

s=$((s + i))

n=$((n / c))

done

echo $s

if [ $s == $t ]

then

echo "Amstrong number"

else

echo "Not an Armstrong number"

fi

result=`ams $n`

echo "$result"

shell script to print a triangle


echo "enter value of n"

read rows
#-n -> allows not to append to next line

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

do

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

do

echo -n "* "

done

echo

done

program to find Greatest among three numbers

# take a numbers from the user

echo "Enter a number: "

read a

read b

read c

# -gt is a greater sign here

if [ $a -gt $b -a $a -gt $c ]

then

echo "It's a."

elif [ $b -gt $a -a $b -gt $c ]

then

echo "It's b."

else

echo "It's c"

fi
shell script to make a basic calculator which performs
addition, subtraction, Multiplication, division

echo "1. Addition"

echo "2.
Subtraction"

echo "3. Multiplication"

echo "4. Division"

read c
echo "give value of a and b"

read a
read b

case $c in

1)echo "sum $((a + b))" ;;

2)echo "subtraction $((a - b ))" ;;

3)echo "multiplication $((a * b))" ;;

4)echo "division $((a / b))" ;;

*) echo "enter valid operation"

esac

shell script to print sum of all even numbers from 1 to 10.

echo "enter the


limit"

read n

i=2

while [ $i -lt $n ]

do

sum=$((sum+i))

i=$((i+2))
done

echo "sum:"$sum

Write a program for array initialization using shell script:

#!/bin/bash
arr[0]="zero"
arr[1]="one"
arr[2]="two"

arr[3]="three"
arr[4]="four"
arr[5]="five"
echo "array size = ${#arr[*]}"
echo "array items"
for item in ${arr[*]}
do
echo $item
done
echo "array items"
for item in ${arr[@]}
do
echo $item
done
echo "array indexes"
for index in ${!arr[*]}
do
echo $index
done
echo "index: array"
for index in ${!arr[*]}
do
echo $index ${arr[$index]}
done

Write a Program to declare an array

#!/bin/bash

arr1=("zero" "one" "two" "three" "four")


arr2=([4]="four" [0]="zero" [1]="one" [2]="two" [3]="three")

echo "-- without double-quot"


for item in ${arr1[@]}
do
echo $item
done
echo

for item in ${arr1[*]}


do
echo $item
done

echo
echo "-- with double-quot"
for item in "${arr2[@]}"
do
echo $item
done

echo

for item in "${arr2[*]}"


do
echo $item
done

Write shell script to Generate ramdom number array with 10 elements and then sort:

#!/bin/bash

#arr=(one two three four)


arr=($(for i in {0..9};do echo $((RANDOM%100));done))
echo "initial array:"
echo "${arr[@]}"
echo "sorted array:"
echo ${arr[@]} | tr " " "\n"| sort -n | tr "\n" " "
echo

Write a Shell script to read and print elements of array.

#!/bin/bash
echo "Enter the size of array"
read size
echo "Enter $size elements"
for((i=0;i<size;i++))
do
read arr[$i]
done
echo "Contents of array:"
for((i=0;i<size;i++))
do
echo ${arr[$i]}
done

You might also like