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

Linux Lab Programs

The document contains a series of Linux shell script exercises, including tasks such as printing prime numbers, reversing a number, summing digits, implementing Linux commands, checking file permissions, copying files, comparing data files, counting vowels, converting case, performing pattern matching, and calculating factorials. Each task includes a brief description followed by the corresponding shell script code. These exercises aim to enhance understanding and proficiency in shell scripting.

Uploaded by

reachspchaitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Linux Lab Programs

The document contains a series of Linux shell script exercises, including tasks such as printing prime numbers, reversing a number, summing digits, implementing Linux commands, checking file permissions, copying files, comparing data files, counting vowels, converting case, performing pattern matching, and calculating factorials. Each task includes a brief description followed by the corresponding shell script code. These exercises aim to enhance understanding and proficiency in shell scripting.

Uploaded by

reachspchaitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

LINUX LAB PROGRAMS

1) Write a shell script to print all the prime numbers between M


to N(M<N)
echo "Enter the value of M:"
read M
echo "Enter the value of N:"
read N

if [ $M -lt 2 ]; then
M=2 # Start from the first prime number if M is less than 2
fi

echo "Prime numbers between $M and $N are:"

for (( num=M; num<=N; num++ )); do


is_prime=1 # Assume the number is prime

for (( i=2; i*i<=num; i++ )); do


if (( num % i == 0 )); then
is_prime=0
break
fi
done

if (( is_prime == 1 )); then


echo $num
fi
done
2)Write a shell script to reverse a given number and check
whether it is palindrome
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

3) Write a shell script to find the sum of digits of a given


number using loops and without using loops

echo "Enter a number: "


read number

sum=0

while [ $number -gt 0 ]; do


digit=$(( number % 10 ))
sum=$(( sum + digit ))
number=$(( number / 10 ))

echo "Sum of digits (using loops) is: $sum"


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

display_menu() {
echo "Select a command to execute:"
echo "1) Display current date and time"
echo "2) List files in current directory"
echo "3) Show current working directory"
echo "4) Display disk usage"
echo "5) Show system information"
echo "6) Display logged-in users"
echo "7) Show memory usage"
echo "8) Display network configuration"
echo "9) Show running processes"
echo "10) Exit"
}

while true; do
display_menu
read -p "Enter your choice (1-10): " choice
case $choice in
1)
echo "Current date and time: $(date)"
;;
2)
echo "Files in current directory:"
ls -l
;;
3)
echo "Current working directory: $(pwd)"
;;
4)
echo "Disk usage:"
df -h
;;
5)
echo "System information:"
uname -a
;;
6)
echo "Logged-in users:"
who
;;
7)
echo "Memory usage:"
free -h
;;
8)
echo "Network configuration:"
ip link show
;;
9)
echo "Running processes:"
ps aux
;;
10)
echo "Exiting..."
break
;;
*)
echo "Invalid option, please select again."
;;
esac

echo ""

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
echo "Files with read, write, and execute permissions:"
for file in *; do

if [ -f "$file" ] && [ -r "$file" ] && [ -w "$file" ] && [ -x "$file" ]; then


echo "$file"
fi
done

6) Write a shell script to copy a file with in the current directory

echo "Enter the path of the file to copy:"


read source_file

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

echo "Enter the destination directory:"


read destination_dir
if [ ! -d "$destination_dir" ]; then
echo "Error: Directory '$destination_dir' does not exist."
exit 1
fi

cp "$source_file" "$destination_dir"

if [ $? -eq 0 ]; then
echo "File '$source_file' has been copied to '$destination_dir'."
else
echo "Failed to copy '$source_file'."
fi

7) Write a shell script to create two data files and compare them to
display unique and common entries

echo "Creating data files..."


cat <<EOL > file1.txt
apple
banana
cherry
date
fig
grape
EOL

cat <<EOL > file2.txt


banana
cherry
date
kiwi
lemon
mango
EOL

echo "Data files created: file1.txt and file2.txt"

sort file1.txt -o file1.txt


sort file2.txt -o file2.txt
echo "Comparing files for unique and common entries..."

comm -3 file1.txt file2.txt

8) Write a shell script to count the number of vowels in a string

read -p "Enter a string: " input_string


vowel_count=0
for (( i=0; i<${#input_string}; i++ )); do
char="${input_string:i:1}" # Extract each character
if [[ "$char" =~ [aeiouAEIOU] ]]; then
((vowel_count++)) # Increment the vowel count
fi
done
echo "Number of vowels in the string: $vowel_count"

9) Write a shell script to convert uppercase characters to lowercase


and vice versa
read -p "Enter a string: " input_string
converted_string=$(echo "$input_string" | tr '[:upper:][:lower:]'
'[:lower:][:upper:]')
echo "Original string: $input_string"
echo "Converted string: $converted_string"

10)Write a shell script to accept a word and perform pattern


matching in a given file

read -p "Enter a word to search for: " search_word


read -p "Enter the filename to search in: " filename
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' does not exist."
exit 1
fi

echo "Searching for '$search_word' in '$filename':"


grep -n "$search_word" "$filename"
if [ $? -eq 0 ]; then
echo "Search completed."
else
echo "No matches found for '$search_word'."
fi

11) Write a shell script to find the factorial of a number


read -p "Enter a number: " num
fact=1
for ((i=1; i<=num; i++)); do
fact=$((fact * i)) # Multiply fact by i
done
echo "Factorial of $num is: $fact"

You might also like