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

update unix program bca6th sem

The document contains a series of shell script examples demonstrating various programming concepts such as variable assignment, arithmetic operations, user input, conditional statements, and loops. Each example includes a brief description and the corresponding shell script code. The scripts cover basic tasks like printing names, performing calculations, and controlling program flow using loops and conditionals.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

update unix program bca6th sem

The document contains a series of shell script examples demonstrating various programming concepts such as variable assignment, arithmetic operations, user input, conditional statements, and loops. Each example includes a brief description and the corresponding shell script code. The scripts cover basic tasks like printing names, performing calculations, and controlling program flow using loops and conditionals.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

#Wap to print your name


echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

2. # Linux shell script program to create and print the value of variables.
country="India"
year=2021
echo "Country name: $country"
echo "Year: $year"

3.# Shell script program to add two numbers.


num1=10
num2=20
num3=`expr $num1 + $num2`
echo "Sum is: $num3"

4. # shell script program to swap two numbers.


num1=10
num2=20
echo "Before Swapping"
echo "Num1: $num1"
echo "Num2: $num2"
num3=$num1
num1=$num2
num2=$num3
echo "After Swapping"
echo "Num1: $num1"
echo "Num2: $num2"

5.# print the subtraction of both variables.


echo "Enter num1: "
read num1
echo "Enter num2: "
read num2
result=`expr $num1 - $num2`
echo "Subtraction is: $result"

6.# Linux shell script program to multiply two numbers.


echo "Enter num1: "
read num1
echo "Enter num2: "
read num2
multiply=`expr $num1 \* $num2`
echo "Multiplication is: $multiply"

7.# shell script program to add two numbers using command line arguments.
sum=`expr $1 + $2`
echo "Sum is: $sum"

8.# shell script program to execute a "ls" command.


cd /
ls

9.# Linux shell script program to demonstrate the "$#" variable.


echo "Total command line arguments are: $#"
10. # Linux shell script program to print the current process id.
echo "Current process identifier: $$"

11.# Take input from user and calculate sum.


read -p "Enter first number: " num1
read -p "Enter second number: " num2
sum=$(( $num1 + $num2 ))
echo "Sum is: $sum"

12. # Shell Program to find Average of Three Numbers


clear
echo "Enter first number: "
read a
echo "Enter second number: "
read b
echo "Enter third number: "
read c
sum=`expr $a + $b + $c`
avg=`expr $sum / 3`
echo "Sum = $sum"
echo "Average = $avg"

12. # Shell Program to print swap two Numbers using 3rd variable

clear
echo "Enter first number: "
read a
echo "Enter second number: "
read b

echo "Before Swapping"


echo "a: $a"
echo "b: $b"

c=$a
a=$b
b=$c

echo "After Swapping"


echo "Num1: $a"
echo "Num2: $b"

13. # Shell Program to print area of square,rectangle,circle

echo “Enter the side of the square:”


read s
echo “Enter the length and breadth of rectangle:”
read leng
read brea
echo “Enter the radius of the circle:”
read radius
echo “Area of the square is:` expr $s \* $s ` ”
echo “Area of the rectangle is: `expr $leng \* $brea`”
echo “Area of the circle is: `expr 3.14 \* radius \* radius`”
14. # shell program no is equal or not equal
a=10
b=20

if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

15 #If a number is divisible by 2 and gives no remainder then it is an even number.

echo "Enter a number: "


read a
if [ $a == 0 ]
then
echo "It's zero."
elif [ `expr $a % 2` == 0 ]
then
echo "It's even."
else
echo "It's odd."
fi

16 #Write a Shell Script to take integer value from user and print some message based on a match
echo "Enter number:"
read num

case $num in
1)
echo "It's one!"
;;

2)
echo "It's two!"
;;

3)
echo "It's three!"
;;

*)
echo "It's something else!"
;;
esac

17 #In the following example we will take user name and time of the day as input and display
some greetings message.

echo "Enter your name:"


read name

echo "Enter time of the day [Morning/Afternoon/Evening/Night]:"


read daytime
case $daytime in
"Morning")
echo "Good Morning $name"
;;

"Afternoon")
echo "Good Afternoon $name"
;;

"Evening")
echo "Good Evening $name"
;;

"Night")
echo "Good Night $name"
;;
esac

18 #In the following example we will take two integer numbers from the user and divide them and
show 5 decimal place.

printf "Enter first integer number: "


read a
# get the second number
printf "Enter second integer number: "
read b
# if b is 0
if [ $b == 0 ]
then
printf "Division by 0 not allowed.\n"
exit # exit the script
fi
# perform division
result=`expr "$a / $b" | bc -l`
printf "%d / %d = %.5f\n" "$a" "$b" "$result"

LOOP and NESTED LOOP IN UNIX


1.for i in 1 2 3 4 5
do
echo "Looping ... number $i"
done

2. for i in hello 1 * 2 goodbye


do
echo "Looping ... i is set to $i"
done

3. INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
4. while :
do
echo "Please type something in (^C to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done

5. while read input_text


do
case $input_text in
hello) echo English ;;
howdy) echo American ;;
gday) echo Australian ;;
bonjour) echo French ;;
"guten tag") echo German ;;
*) echo Unknown Language: $input_text
;;
esac
done

6. for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done

7. a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done

8. a=10

until [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

9. a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
10.
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done

11. for ((i = 0 ; i < 3 ; i++)); do


for((j = 0 ; j < 2 ; j++)); do
echo "hello world"
done
done

You might also like