0% found this document useful (0 votes)
44 views7 pages

Bash Scripts for Number Operations

The document contains several Bash scripts that perform various tasks including finding the largest number among three inputs, calculating the sum of digits of a number, generating a multiplication table, checking if a number is a palindrome, removing zero-byte files from a specified directory, and displaying current date, user, and file listings based on user choice. Each script includes user prompts and outputs demonstrating their functionality. The scripts are designed for basic command-line operations in a Linux environment.
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)
44 views7 pages

Bash Scripts for Number Operations

The document contains several Bash scripts that perform various tasks including finding the largest number among three inputs, calculating the sum of digits of a number, generating a multiplication table, checking if a number is a palindrome, removing zero-byte files from a specified directory, and displaying current date, user, and file listings based on user choice. Each script includes user prompts and outputs demonstrating their functionality. The scripts are designed for basic command-line operations in a Linux environment.
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

LARGEST NUMBER

$ #!/bin/bash
echo "enter num1"
read num1
echo "enter num2"
read num2
echo "enter num3"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo "the lagest number is" $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo "the largest number is" $num2
else
echo "the largest number is" $num3
fi

OUTPUT
enter num1
5
enter num2
15
enter num3
50
the largest number is 50

SUM OF DIGITS
$ #!/bin/bash
echo "enter the number"
read n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(($n%10))
n=$(($n/10))
sum=$(($sum+sd))
done
echo "SUM OF ALL DIGITS IS $sum"

OUTPUT
enter the number
156
SUM OF ALL DIGITS IS 12

MULTIPLICATION TABLE

$ #!/bin/bash

# Input from user


echo "Enter the number -"
read n
# initializing i with 1
i=1

# Looping i, i should be less than


# or equal to 10
while [ $i -le 10 ]
do
res=`expr $i \* $n`

# printing on console
echo "$n * $i = $res"

# incrementing i by one
((++i))

# end of while loop


Done

OUTPUT

Enter the number -


7
7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

PALINDROME CHECKING

$ num=545

# Storing the remainder


s=0

# Store number in reverse


# order
rev=""

# Store original number


# in another variable
temp=$num

while [ $num -gt 0 ]


do
# Get Remainder
s=$(( $num % 10 ))

# Get next digit


num=$(( $num / 10 ))

# Store previous number and


# current digit in reverse
rev=$( echo ${rev}${s} )
done
if [ $temp -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi

OUTPUT
545
Number is palindrome
420

Number is NOT palindrome

LARGEST NUMBER
$ #!/bin/bash
echo "Enter Size(N)"
read N

i=1
max=0

echo "Enter Numbers"


while [ $i -le $N ]
do
read num
if [ $i -eq 1 ] #set first number as max
then
max=$num
else #from number 2 update max if the num > max.
if [ $num -gt $max ]
then
max=$num
fi
fi
i=$((i + 1)) #increment i by 1
done

echo $max

OUTPUT
Enter Size(N)
5
Enter Numbers
50
70
40
30
20
70

ZERO BYTE FILE REMOVE


$ #!/bin/bash

# Taking directory name as input from user


echo -n "Enter name of the directory : "
read directory_name
# If directory exist it will print
# Directory exits
# and remove the zero-sized files.
# Or if directory doesn't exist it will print
# Directory does not exist.
if [ -d "$directory_name" ];
then
echo "$directory_name Directory exist"
for i in `find $directory_name -size 0 -delete`
do
echo ""
done
echo "Zero-sized files are Successfully deleted"
else
echo "$directory_name does not exist"

fi

OUTPUT
Enter name of the directory : /d/demo
/d/demo Directory exist
Zero-sized files are Successfully deleted

OUTPUT
Enter name of the directory : /d/demo
/d/demo Directory exist
Zero-sized files are Successfully deleted
[Link] a Shell script for displaying current date, user name, file listing and
directories by getting user choice.

#!/bin/bash

echo "Menu driven"

echo "[Link] date"

echo "[Link] user"

echo "[Link] files and folders"

echo "Enter the choice"

read choice

case $choice in

1)

echo "display a current date"

date
echo "display a current month"

cal

;;

2)

echo "display current user"

who am i

;;

3)

echo "List files and folders"

ls

;;

Esac

OUTPUT

Menu driven
[Link] date
[Link] user
[Link] files and folders
Enter the choice
1
display a current date
Sat Sep 28 [Link] IST 2024
display a current month

Menu driven
[Link] date
[Link] user
[Link] files and folders
Enter the choice
2
display current user
Prabha@Prabha-PC MINGW64 /

Menu driven
[Link] date
[Link] user
[Link] files and folders
Enter the choice
3
List files and folders
[Link] [Link] bin/ cmd/ dev/ etc/ [Link]* git-
[Link]* mingw64/ proc/ tmp/ [Link] [Link]* [Link]
usr/

You might also like