0% found this document useful (0 votes)
32 views15 pages

CN Lab1 202211099.2

Uploaded by

pehep10730
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)
32 views15 pages

CN Lab1 202211099.2

Uploaded by

pehep10730
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/ 15

COMPUTER NETWORKS LAB-2 (CS361)

IIITV-ICD
BASIC SHELL SCRIPTING
By: 202211099

1) Write a shell scripting for printing “Hello World !!”.

echo "Hello World !!"

Output-

2) Write a shell script to add two numbers using as command-line arguments.

iecho "Enter Two numbers : "


read a
read b
res=`echo $a + $b | bc`
echo "Result : $res"

Output-

3) Write a shell script to check whether a number is even or odd.

echo "Enter a number : "


read a
if(($a % 2 == 0)); then
echo "$a is an even number."
else
echo "$a is an odd number."
fi
Output:

4) Write a shell script to display multiplication table of given number.

read -p "Enter a number: " num

echo "Multiplication table of $num:"

for ((i=1; i<=10; i++)); do


result=$((num*i))
echo "$num * $i = $result"
done

Output-

5) Write a shell script to find the factorial of a given number.

fact() {
local num=$1
local result=1
for ((i=num; i>=1; i--)); do
result=$((result * i))
done
echo $result
}
read -p "Enter a number: " num
fact $num
Output:
6) Write a shell script to check whether a given character is vowel or consonant.

echo "Enter a charater : "


read ch

if [[ $ch == [AEIOUaeiou] ]]
then
echo "$ch is a vowel."
else
echo "$ch is a consonant."
fi

Output:

7) Write a shell script to find the largest of three numbers.

echo "Enter Number1"


read num1
echo "Enter Number2"
read num2
echo "Enter Number3"
read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]


then
echo "$num1 is greator"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo "$num2 is greator"
else
echo "$num3 is greator"
fi

Output-
8) Write a shell script to check whether a given string is a palindrome.

echo "Enter a number: "


read number
reverse=0
original=$number
while [ $number -gt 0 ]
do
remainder=$(( $number % 10 ))
reverse=$(( $reverse * 10 + $remainder ))
number=$(( $number / 10 ))
done
if [ $original -eq $reverse ]
then
echo "$original is a palindrome."
else
echo "$original is not a palindrome."
fi

Output-

9) Write a shell script to print the Fibonacci series up to n terms.

read -p "Enter the number of terms: " n


a=0
b=1
echo "Fibonacci series:"

for ((i=1; i<=$n; i++)); do


echo -n "$a "
fib=$((a+b))
a=$b
b=$fib
done
echo

Output-
10) Write a shell script to reverse a string.

read -p "Enter string:" string


len=${#string}
reversed=" "
for ((i = $len - 1; i >= 0; i--))
do
reversed="$reversed${string:$i:1}"
done
echo "$reversed"

Output:

11) Write a shell script to reverse a number.

echo "Enter a number: "


read number

reverse=0
original=$number

while [ $number -gt 0 ]


do
remainder=$(( $number % 10 ))
reverse=$(( $reverse * 10 + $remainder ))
number=$(( $number / 10 ))
done

echo "$reverse is the reverse of $original."

Output:

12) Write a shell script to check whether a file exists and is readable.
if [ -r x.txt ]
then
echo "File exists and is readable"
else
echo "File doesn't exists."

fi

Output:

13) Write a shell script to count the number of lines, words, and characters in a file.

line_count=$(wc -l < x.txt)


echo "Number of lines in document.txt: $line_count"
word_count=$(wc -w < x.txt)
echo "Number of words in document.txt: $word_count"
char_count=$(wc -m < x.txt)
echo "Number of characters in document.txt: $char_count"

Output:

14) Write a shell script to calculate the sum of digits of a number.

read -p "Enter a number: " num


sum=0
while [ $num -gt 0 ]
do
digit=$(( $num % 10 ))
sum=$(( $sum + $digit ))
num=$(($num / 10))
done
echo "Sum of digits: $sum"

Output:

15) Write a shell script to find all prime numbers between two numbers.
echo "Enter a number:"
read number
i=2

if [ $number -lt 2 ]
then
echo "$number is not a prime number."
exit
fi

max=`echo "sqrt($number)" | bc`


while [ $i -le $max ]
do
if [ `expr $number % $i` -eq 0 ]
then
echo "$number is not a prime number."
exit
fi
i=`expr $i + 1`
done

echo "$number is a prime number."

Output:

16) Write a shell script to find whether an input year is leap year or not.

read -p "Enter a year: " year

if (( $year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0 )); then


echo "$year is a leap year."
else
echo "$year is not a leap year."
fi

Output:
17) Write a shell script to delete files older than 7 days from a specific directory.

read -p "Enter the directory path to clean up: " DIRECTORY

if [ -d "$DIRECTORY" ]; then
find "$DIRECTORY" -type f -mtime +7 -exec rm -f {} \;
echo "Files older than 7 days have been deleted from $DIRECTORY."
else
echo "Error: Directory $DIRECTORY does not exist."
fi

Output:

18) Write a shell script to convert a string to uppercase.

echo "Enter a string: "


read input
uppercase_string=$(echo $input| tr '[a-z]' '[A-Z]')
echo "Uppercase string: $uppercase_string"

Output:

19) Write a shell script to display all the files and directories in the current directory.

echo "Files and directories in the current directory:"


ls -l

Output:
20) Write a shell script to find the sum of all even numbers from 1 to n.

read -p "Enter the value of n: " n


sum=0
for ((i=2; i<=n; i+=2))
do
sum=$((sum+i))
done
echo "The sum of all even numbers from 1 to $n is: $sum"

Output:
21) Write a shell script to implement a basic calculator (addition, subtraction, multiplication,
division) using case statements.

echo "Enter Two numbers : "


read a
read b

echo "Enter Choice :"


echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read op

case $op in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a - $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo "scale=2; $a / $b" | bc`
;;
esac
echo "Result : $res"

Output:

22) Write a shell script to find the number of months between two dates.
calculate_months_between() {
local start_date=$1
local end_date=$2

start_month=$(date -d "$start_date" "+%Y%m")


end_month=$(date -d "$end_date" "+%Y%m")

start_year=$(date -d "$start_date" "+%Y")


start_month_only=$(date -d "$start_date" "+%m")
end_year=$(date -d "$end_date" "+%Y")
end_month_only=$(date -d "$end_date" "+%m")

month_diff=$(( (end_year - start_year) * 12 + (end_month_only - start_month_only) ))

echo $month_diff
}

read -p "Enter the start date (YYYY-MM-DD): " start_date


read -p "Enter the end date (YYYY-MM-DD): " end_date

if ! date -d "$start_date" &>/dev/null; then


echo "Error: Invalid start date."
exit 1
fi

if ! date -d "$end_date" &>/dev/null; then


echo "Error: Invalid end date."
exit 1
fi

months_diff=$(calculate_months_between "$start_date" "$end_date")


echo "The number of months between $start_date and $end_date is: $months_diff months."

Output:

23) Write a shellscript to Swapping Two Numbers Without Using a Third Variable.
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
echo "Before swapping: num1 = $num1, num2 = $num2"
num1=$((num1 + num2))
num2=$((num1 - num2))
num1=$((num1 - num2))
echo "After swapping: num1 = $num1, num2 = $num2"

Output:

24) Write a shell script to Solve a Linear Equation (ax + b = 0).

echo "Enter the value of a:"


read a
echo "Enter the value of b:"
read b
x=$(echo "scale=2; $b / $a" | bc -l)
echo "The value of x is: $x"

Output:

25) Write a shell script to concatenate two strings input by the user.

read -p "Enter the first string: " string1


read -p "Enter the second string: " string2
result=$string1$string2
echo "The concatenated string is: $result"

Output:

26) Write a shell script to display all the files and directories in the current directory.
echo "Files and directories in the current directory:"
ls -l

Output:

27) Write a shell script to read a file line by line and print each line with its line number.
read -p "Enter the file name: " filename

if [ ! -f "$filename" ]; then
echo "Error: File $filename not found."
exit 1
fi

line_number=1

while IFS= read -r line; do


echo "$line_number: $line"
line_number=$((line_number + 1))
done < "$filename"

Output:

28) Write a shell script to create a backup of a directory by copying it to another location.

read -p "Enter the directory to backup: " source_dir


read -p "Enter the backup destination: " backup_dest

if [ ! -d "$source_dir" ]; then
echo "Error: Source directory $source_dir does not exist."
exit 1
fi

if [ ! -d "$backup_dest" ]; then
echo "Error: Backup destination $backup_dest does not exist. Creating it now..."
mkdir -p "$backup_dest"
fi

backup_dir="$backup_dest/$(basename "$source_dir")_backup_$(date +%Y%m%d_%H%M%S)"

cp -r "$source_dir" "$backup_dir"

echo "Backup of $source_dir created at $backup_dir"

Output:
29) Write a shell script to check the disk usage and send an alert if it exceeds a certain threshold.

THRESHOLD=80
ALERT_EMAIL="[email protected]"

disk_usage=$(df / | grep / | awk '{print $5}' | sed 's/%//')

if [ "$disk_usage" -gt "$THRESHOLD" ]; then


echo "Disk usage is at ${disk_usage}%, which is above the threshold of ${THRESHOLD}%."
echo "Disk usage is above ${THRESHOLD}%. Currently at ${disk_usage}%." | mail -s "Disk
Usage Alert" $ALERT_EMAIL
else
echo "Disk usage is under control: ${disk_usage}%."
fi

Output:

30) Write a shell script to perform linear search operation.

read -p "Enter the elements of the array (space-separated): " -a array


read -p "Enter the element to search for: " search_element

found=0

for i in "${!array[@]}"; do
if [ "${array[$i]}" -eq "$search_element" ]; then
echo "Element $search_element found at index $i."
found=1
break
fi
done

if [ $found -eq 0 ]; then


echo "Element $search_element not found in the array."
fi

Output:

You might also like