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

LabManual

The document contains a series of shell scripts that perform various tasks, including printing prime numbers, reversing numbers, summing digits, implementing Linux commands, copying files, comparing data files, counting vowels, and handling processes. Each script is designed to demonstrate specific programming concepts and functionalities in bash. The scripts are well-structured and include user prompts for input.

Uploaded by

dhanushchitra579
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)
4 views

LabManual

The document contains a series of shell scripts that perform various tasks, including printing prime numbers, reversing numbers, summing digits, implementing Linux commands, copying files, comparing data files, counting vowels, and handling processes. Each script is designed to demonstrate specific programming concepts and functionalities in bash. The scripts are well-structured and include user prompts for input.

Uploaded by

dhanushchitra579
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/ 14

1. Write a shell script to print all the prime numbers between M to N (M < N).

#!/bin/bash

echo "Enter M : "


read m
echo "Enter N : "
read n

flag_val=0;

for ((i=m; i<=n; i++)); do


if((i==1)); then
echo "1 is not prime nor composite"
continue
fi
#for((j=2; j<i; j++)); do
for((j=2; j*j<=i; j++)); do # To reduce the computation we would check till
sqrt(j)

if((i%j == 0)); then


flag_val=1
break
else
flag_val=0
fi
done
if((flag_val == 1)); then
echo "$i is not prime"
else
echo "$i is prime"
fi

done
2. Write a shell script to reverse a given number and check whether it is a
palindrome.

#!/bin/bash

echo "Enter the number "


read num

copy_val=$num
rev_val=0

while((copy_val>0)); do
#echo "$copy_val"
mod_val=$((copy_val%10))
rev_val=$((rev_val*10 + mod_val));
copy_val=$((copy_val/10))
done

#echo "rev val is : $rev_val"


if ((rev_val == num)); then
echo "$num is palindrome"
else
echo "$num is not palindrome"
fi
3. Write a shell script to find the sum of digits of a given number using loops
and without using loops.

#!/bin/bash

sum_of_digits(){
local n=$1
if (( n == 0 )); then
echo 0
else
local digit=$(( n % 10))
local rest=$(( n/10 ))
local rec_sum=$(sum_of_digits $rest)
echo $(( digit + rec_sum ))
fi
}

echo "Enter number"


read num

echo "sum of digits without loop is "


sum_of_digits "$num"

copy_val=num
sum=0

while ((copy_val > 0)); do


mod_val=$((copy_val % 10))
sum=$((sum + mod_val))
copy_val=$((copy_val / 10))
done

echo "sum of the digits is $sum"


4. Write a shell script to implement 10 Linux commands using case.

#!/bin/bash

echo "1. List files"


echo "2. Present working directory"
echo "3. OS information "
echo "4. User"
echo "5. Date"
echo "6. Process details"
echo "7. Uptime"
echo "8. Network interfaces"
echo "9. Memory"
echo "10.Who"

read choice

case $choice in
1) ls -la;;
2) pwd;;
3) uname -a;;
4) whoami;;
5) date;;
6) ps aux;;
7) uptime;;
8) ifconfig;;
9) df -h;;
10) who;;
*)echo "Invalid choice"
Esac
5. Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.

#!/bin/bash

echo "list all the files displaying permissions"

echo " "


ls -la | awk '$1 ~ /^-rwx/ {print $9}'

echo " "


find . -maxdepth 1 -type f -executable
6. Write a shell script to copy a file within the current directory.

#!/bin/bash

echo "Enter the source file name"


read src

echo "Enter destination file name"


read des

echo "Enter the content to put in the first file"


read content

echo "$content" > "$src"

cp "$src" "$des"
7. Write a shell script to copy a file between two directories.

#!/bin/bash

echo "Enter the directory name"


read dir_name

mkdir "$dir_name"

echo "Enter the source file name"


read src

echo "Enter content to enter into the file"


read content

echo "Enter the destination file name"


read des

echo "$content" > "$src"

cp "$src" ./"$dir_name/$des"
8. Write a shell script to create two data files and compare them to display
unique and common entries.

#!/bin/bash

echo "Enter first file name"


read file_first

echo "Enter second file name"


read file_second

echo "Enter the content for 1st file"


cat > "$file_first"

echo "Enter the content for 2nd file"


cat > "$file_second"

echo -e "\n\n\n"

sort "$file_first" > sorted_first


sort "$file_second" > sorted_second

echo -e "common entries : \n $(comm -12 sorted_first sorted_second)"

echo -e "unique entry in $file_first: \n $(comm -23 sorted_first sorted_second)"

echo -e "unique entry in $file_second:\n $(comm -13 sorted_first


sorted_second)"

rm "$file_first" "$file_second" sorted_first sorted_second


9. Write a shell script to count the number of vowels in a string.

#!/bin/bash

echo "Enter the string"


read string
vowel_count=$(echo "$string" | grep -o -i "[aeiou]" | wc -l)

echo "number of vowels in $string is : $vowel_count"


10. Write a shell script to convert uppercase characters to lowercase and
vice versa.

#!/bin/bash

echo "Enter a string"


read string

new_string=$(echo "$string" | tr '[:lower:][:upper:]' '[:upper:][:lower:]')

echo "$new_string"
11. Write a shell script to accept a word and perform pattern matching in a
given file.

#!/bin/bash

echo "Enter a word to search"


read word

echo "Enter filename"


read file_name

if [[ ! -f "$file_name" ]]; then


echo "no such file"
exit 1
fi

grep --color=always -n "$word" "$file_name"


12. Write a shell script to find the factorial of a number.

#!/bin/bash

factorial(){
if [[ $1 -le 1 ]]; then
echo 1
else
local prev=$(factorial $(( $1 - 1 )))
echo $(( $1 * prev ))
fi

echo "Enter a non negative number: "


read num

if ! [[ "$num" =~ ^[0-9]+$ ]]; then


echo "invalid output"
exit 1
fi

result=$(factorial $num)

echo "factorial of $num is : $result"


13. Write a menu-driven program to demonstrate the zombie process and
orphan process.

#!/bin/bash

show_menu(){
echo -e "\n------------------------\n"
echo "1. Create zombie process"
echo "2. Create orphan process"
echo "3. exit"
echo -e "\n------------------------\n"
}

create_zombie(){
echo -e "\ncreating zombie process\n"
{
echo "child (PID $$) running"
exit 0
}&

child_pid=$!
echo "Parent (PID $$) sleeping for 10 seconds without waiting for child"
sleep 10

create_orphan(){
echo -e "\n creating orphan process\n"
{
sleep 5
echo "orphan child (PID $$) running with new parent (PID: $(ps -o
ppid= -p $$))"
}&

echo "parent (PID $$) exiting before child finishes"


exit 0
}

while true; do
show_menu
echo "Enter choice"
read choice

case $choice in
1) create_zombie ;;
2) create_orphan ;;
3) echo "Exiting .."; break ;;
*) echo "Invalid choice" ;;
esac
done

You might also like