[go: up one dir, main page]

0% found this document useful (0 votes)
2 views12 pages

OS-linux-Lab

The document contains a series of shell script solutions for various programming tasks, including calculating factorials, finding prime numbers, checking for palindromes, counting vowels, and more. Each solution is presented with a brief description and the corresponding shell script code. The tasks also cover file operations, user input handling, and process demonstrations.

Uploaded by

plusabcplus786
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)
2 views12 pages

OS-linux-Lab

The document contains a series of shell script solutions for various programming tasks, including calculating factorials, finding prime numbers, checking for palindromes, counting vowels, and more. Each solution is presented with a brief description and the corresponding shell script code. The tasks also cover file operations, user input handling, and process demonstrations.

Uploaded by

plusabcplus786
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/ 12

PART-A

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


Sol1:

echo "Enter a number"

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

while [ $number -ne 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

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


Sol:
echo "Enter a string:"
read input_string

vowel_count=0

for (( i=0; i<${#input_string}; i++ )); do


char=${input_string:$i:1}
case $char in
( [aeiou]) ((vowel_count++)) ;;
esac
done

echo "Number of vowels in the given string: $vowel_count"


5.Write a shell script to find the sum of digits of a given number using loops
Sol:
sum_with_loop() {
num=$1
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits (using loop): $sum"
}

echo "Enter a number:"


read number

sum_with_loop "$number"

6.Write a shell script to read and print a matrix of order m x n.


Sol:
echo "Enter number of rows (m):"
read m
echo "Enter number of columns (n):"
read n

declare -A matrix​ ​ ​ ​ ​
echo "Enter the elements of the matrix:"
for ((i = 0; i < m; i++)); do
for ((j = 0; j < n; j++)); do
read matrix[$i,$j]
done
done

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

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


Sol:
while true; do
echo "Choose a command to execute:"
echo "1. Show current directory"
echo "2. List files"
echo "3. Show date and time"
echo "4. Show logged-in users"
echo "5. Show system uptime"
echo "6. Show disk usage"
echo "7. Show memory usage"
echo "8. Show current user"
echo "9. Show running processes"
echo "10. Exit"
read -p "Enter your choice (1-10): " choice

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

9.Write a shell script to copy a file within current directory


Sol:
echo "Enter the name of the file to copy:"
read source_file

echo "Enter the destination file name:"


read destination_file

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

10.Write a shell script to copy file between two directories.


Sol:
echo "Enter the source directory:"
read source_dir

echo "Enter the file name to copy:"


read file_name

echo "Enter the destination directory:"


read destination_dir

# Check if source file exists


if [ -f "$source_dir/$file_name" ]; then
# Check if destination directory exists, create if not
if [ ! -d "$destination_dir" ]; then
echo "Destination directory does not exist. Creating it..."
mkdir -p "$destination_dir"
fi

# Copy the file


cp "$source_dir/$file_name" "$destination_dir/"
echo "File copied successfully to $destination_dir."
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

echo "$year is Leap Year!"

else

echo "$year is not a Leap Year!"

fi

12.Write a shell script to check whether a given number is even or odd.


Sol:
clear
echo "Enter a number : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

13.Write a shell script to generate Fibonacci series.


Sol:
clear
echo "Enter the value of N: "
read N
a=0
b=1
echo -e "the Fibonacci series is :\n "
for((i=0;i<N;i++))
do
echo "$a"
fn=$((a+b))
a=$b
b=$fn
done

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 “welcome” > file2.txt


echo “to” >> file2.txt
echo “the” > > file2.txt
echo “college” >>
file2.txt

Sort file1.txt -o file1.txt


Sort file2.txt -o file2.txt

echo “”

echo “common entries:”


comm -12 file1.txt file2.txt

echo “”

echo “unique entries in file1:”


comm -23 file1.txt file2.txt
echo “”
echo “unique entries in file2:”
comm -13 file1.txt file2.txt

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

# Perform the appropriate action


case $ACTION in
compress)
if [ -f "$FILE" ]; then
gzip "$FILE"
echo "File compressed to $FILE.gz"
else
echo "Error: File '$FILE' does not exist."
exit 2
fi
;;
decompress)
if [ -f "$FILE" ]; then
gunzip "$FILE"
echo "File decompressed from $FILE"
else
echo "Error: File '$FILE' does not exist."
exit 2
fi
;;
*)
echo "Invalid action. Use 'compress' or 'decompress'."
exit 3
;;
esac
​ ​ ****

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

# Loop through all files in the current directory


for FILE in *; do
if [ -f "$FILE" ]; then
case $METHOD in
grep)
grep "$PATTERN" "$FILE" && echo "Pattern found in: $FILE"
;;
fgrep)
fgrep "$PATTERN" "$FILE" && echo "Pattern found in: $FILE"
;;
*)
echo "Invalid method. Use 'grep' or 'fgrep'."
exit 2
;;
esac
fi

done
​ ​ ***
Output:
After changing executable mod
./programname.sh “pattern” grep
If finds gives out which line pattern found else nothing shows output.

17.Write a shell script to execute a command at scheduled time.


Sol:

Before running, make sure the at command is installed and the atd service is running:

# Install 'at' (if not already installed)


sudo apt install at # Debian/Ubuntu

# Start the at daemon


sudo service atd start

18.Write a shell script to convert uppercase characters to lowercase and vice versa.
Sol:

if [ "$#" -eq 1 ] && [ -f "$1" ]; then

tr 'a-zA-Z' 'A-Za-z' < "$1"​ ​ ​


​ ​ #translate command
else
echo "Usage: $0 <filename>"
echo "Or type input manually and press Ctrl+D to finish:"
# Read from standard input
tr 'a-zA-Z' 'A-Za-z'
fi
​ ***
Output:
method1:
After executable mode
./programname.sh
Welcome tO LAB give any put string
wELCOME To lab shows the output

Press ctrl+D to come out to the command prompt

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

# Prompt the user to enter the file name


read -p "Enter the file name: " filename

# Check if the file exists


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

# Perform pattern matching using grep


echo "Searching for '$word' in file '$filename'..."
grep --color=always -n "$word" "$filename"

# Check if grep found a match


if [[ $? -ne 0 ]]; then
echo "No matches found for '$word' in '$filename'."
fi

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

You might also like