OS-linux-Lab
OS-linux-Lab
read num
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i))
}
echo $fact
2.Write a shell script to print all the prime numbers between M to N (M<N).
Sol:
echo "Enter the range (M and N):"
read M N
for ((num=M; num<=N; num++)); do
is_prime=1
if (( num < 2 )); then
is_prime=0
else
for ((i=2; i*i<=num; i++)); do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
fi
if (( is_prime )); then
echo -n "$num "
fi
done
echo
3.Write a shell script to reverse a given number and check whether it is a palindrome.
Sol:
echo "Enter a number: "
read number
reverse=0
original=$number
vowel_count=0
sum_with_loop "$number"
echo -n
echo "The entered matrix is:"
for ((i = 0; i < m; i++)); do
for ((j = 0; j < n; j++)); do
echo -n "${matrix[$i,$j]} "
done
echo
done
case $choice in
1) pwd ;;
2) ls -l ;;
3) date ;;
4) who ;;
5) uptime ;;
6) df -h ;;
7) free -m ;;
8) whoami ;;
9) ps aux ;;
10) echo "Exiting..."; break ;;
*) echo "Invalid choice, please try again." ;;
esac
echo ""
done
8.Write a Shell script that displays list of all the files in the current directory to which the
user has read, write and execute permissions?
Sol:
echo "Files with read, write, and execute permissions in the current directory:"
for file in *; do
if [ -f "$file" ] && [ -r "$file" ] && [ -w "$file" ] && [ -x "$file" ]; then
echo "$file"
fi
done
if [ -f "$source_file" ]; then
cp "$source_file" "$destination_file"
echo "File copied successfully as $destination_file."
else
echo "Error: Source file does not exist."
fi
Part-B
11.Write a shell script to find whether a given year is leap year or not.
Sol:
clear
echo "Enter Year:"
read y
year=$y
y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
else
fi
14.Write a Shell script to create two data files and compare them to display unique and common
Entries.
Sol:
echo “welcome” > file1.txt
echo “to” >> file1.txt
echo “the” >> file1.txt
echo “lab” > > file1.txt
echo “”
echo “”
15.Write a shell script that compresses and decompresses a file using gzip and gunzip.
Sol:
# Check for the correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 [compress|decompress] filename"
exit 1
fi
ACTION=$1
FILE=$2
Output:
To compress a file
./programname.sh compress <filename.txt>
<filename.txt.gz> will gets create as a zip file
To decompress a file
./programname.sh decompress <filename.txt.gz> will unzip the zip file created
16.Write a shell script to find a given pattern in a list of files of current directory using grep and
fgrep commands.
Sol:
# Check for correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <pattern> <grep|fgrep>"
exit 1
fi
PATTERN=$1
METHOD=$2
done
***
Output:
After changing executable mod
./programname.sh “pattern” grep
If finds gives out which line pattern found else nothing shows output.
Before running, make sure the at command is installed and the atd service is running:
18.Write a shell script to convert uppercase characters to lowercase and vice versa.
Sol:
Method2:
./programname.sh <filename>
Shows the content of filename by changing lower/upper cases
19.Write a shell script to accept a word and perform pattern matching in a given file.
Sol:
read -p "Enter the word to search: " word
20.Write a Menu driven program to demonstrate zombie process and orphan process.
Sol:
while true; do
echo "----------------------------------"
echo " Process Demonstration Menu"
echo "----------------------------------"
echo "1. Demonstrate Zombie Process"
echo "2. Demonstrate Orphan Process"
echo "3. Exit"
echo "----------------------------------"
read -p "Enter your choice: " choice
case $choice in
1)
echo -e "\n--- Zombie Process Demo ---"
(
# Child process
echo "Child process (PID $$) exiting..."
exit 0
)&
# Parent process sleeps to keep itself alive after child dies
pid=$!
echo "Parent process waiting (PID $$), child PID: $pid"
sleep 5
echo "Use 'ps -l' to see zombie process before parent exits"
ps -l | grep "$pid"
echo "Now parent exits..."
;;
2)
echo -e "\n--- Orphan Process Demo ---"
(
sleep 5
echo "Orphan process now adopted by init (PID $$)"
)&
echo "Parent process (PID $$) exiting, orphaning the child"
exit 0
;;
3)
echo "Exiting program."
exit 0
;;
*)
echo "Invalid choice. Please enter 1, 2, or 3."
;;
esac
echo ""
read -p "Press Enter to continue..."
done