0% found this document useful (0 votes)
10 views4 pages

Shell Code Examples and Outputs

The document contains code snippets and outputs for various Bash shell programming tasks including generating cubes of even numbers from 1 to 50, creating a multiplication table for 6, determining if a number is positive, negative or zero, developing a menu driven program to check leap years, odd/even numbers, and find the greatest of a set of numbers, and writing a function to copy multiple files to a directory.

Uploaded by

Jacob Abraham
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)
10 views4 pages

Shell Code Examples and Outputs

The document contains code snippets and outputs for various Bash shell programming tasks including generating cubes of even numbers from 1 to 50, creating a multiplication table for 6, determining if a number is positive, negative or zero, developing a menu driven program to check leap years, odd/even numbers, and find the greatest of a set of numbers, and writing a function to copy multiple files to a directory.

Uploaded by

Jacob Abraham
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

GOA COLLEGE OF ENGINEERING

ASSIGNMENT Date: 07/12/2022

[A] Generate cubes of even no. from 1 to 50.


Code:
echo "Cubes of Even No. from 1 to 50 are:-"
i=1
n=50
while [ $i -le $n ]
do
if [ $(($i%2)) -eq 0 ]
then
echo "($i)^3 = $(($i*$i*$i))"
fi
i=$(($i+1))
done

Output:
Cubes of Even No. from 1 to 50 are:-
(2)^3 = 8
(4)^3 = 64
(6)^3 = 216
(8)^3 = 512
(10)^3 = 1000
(12)^3 = 1728
(14)^3 = 2744
(16)^3 = 4096
(18)^3 = 5832
(20)^3 = 8000
(22)^3 = 10648
(24)^3 = 13824
(26)^3 = 17576
(28)^3 = 21952
(30)^3 = 27000
(32)^3 = 32768
(34)^3 = 39304
(36)^3 = 46656
(38)^3 = 54872
(40)^3 = 64000
(42)^3 = 74088
(44)^3 = 85184
(46)^3 = 97336
(48)^3 = 110592
(50)^3 = 125000

[B] Multiplication table of 6


Code:
echo "Multiplication Table of 6 is:"
i=1
n=10
number=6
while [ $i -le $n ]
do
echo "$number * $i = $(($number*$i))"
i=$(($i+1))
done

Output:
Multiplication Table of 6 is:
6*1=6
6 * 2 = 12

Name: JACOB ABRAHAM Roll No: 201105020 Page No:


GOA COLLEGE OF ENGINEERING

6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60

[C] To find whether a no is positive, negative or zero


Code:
echo "Enter a Number:"
read n
if [ $n -eq 0 ]
then
echo "Entered Number is Zero"
fi
if [ $n -gt 0 ]
then
echo "Entered Number is Positive No."
fi
if [ $n -lt 0 ]
then
echo "Entered Number is Negative No."
Fi

Output:
Enter a Number:
-13
Entered Number is Negative No.

[D] Menu driven program :


a) Leap year or not
b) Odd or Even number
c) Find greatest of n numbers
Code:
choice=1
while [ $choice -ne 4 ]
do
echo "Menu"
echo "[Link] Leap Year"
echo "[Link] or Even"
echo "[Link] of n Numbers"
echo "[Link]"
read choice
case $choice in
1)echo "Enter the year: "
read year
if [ $(($year%4)) -eq 0 ]
then
echo "$year is a Leap Year."
else
echo "$year is not a Leap Year."
fi
;;
2)echo "Enter a number: "
read n
if [ $(($n%2)) -eq 0 ]
then
echo "$n is Even."

Name: JACOB ABRAHAM Roll No: 201105020 Page No:


GOA COLLEGE OF ENGINEERING

else
echo "$n is Odd."
fi
;;
3)echo "Enter the size of array:"
read N
i=1
max=0
echo "Enter Numbers:"
while [ $i -le $N ]
do
read num
if [ $i -eq 1 ]
then
max=$num
else
if [ $num -gt $max ]
then
max=$num
fi
fi
i=$((i + 1))
done
echo "The Greatest of Entered numbers is $max"
;;
4);;
*)echo "Invalid Choice!"
esac
Done

Output:
Menu
[Link] Leap Year
[Link] or Even
[Link] of n Numbers
[Link]
1
Enter the year:
2012
2012 is a Leap Year.
Menu
[Link] Leap Year
[Link] or Even
[Link] of n Numbers
[Link]
2
Enter a number:
13
13 is Odd.
Menu
[Link] Leap Year
[Link] or Even
[Link] of n Numbers
[Link]
3
Enter the size of array:
4
Enter Numbers:
6
3

Name: JACOB ABRAHAM Roll No: 201105020 Page No:


GOA COLLEGE OF ENGINEERING

12
1
The Greatest of Entered numbers is 12
Menu
[Link] Leap Year
[Link] or Even
[Link] of n Numbers
[Link]
4

[E] Copy multiple files to a directory


Code:
copyfile() {
cp $1 $2
}
echo "The files are:"
echo | ls
ch=1
while [ $ch -ne 0 ]
do
echo "Enter the file location to be copied:"
read sourcefile
echo "Enter destination:"
read destination
copyfile "$sourcefile $destination"
echo "Do you want to continue?(0/1):"
read ch
Done

Output:
The files are:
folder1 [Link] [Link] text3 text6
Enter the file location to be copied:
text3
Enter destination:
folder1
Do you want to continue?(0/1):
1
Enter the file location to be copied:
text6
Enter destination:
folder1
Do you want to continue?(0/1):
0

Name: JACOB ABRAHAM Roll No: 201105020 Page No:

You might also like