unix
1.Arithmatic opration using swith case in UNIX
#!/bin/bash
echo "Enter first number: "
read num1
echo "Enter second number: "
read num2
echo "Enter the operation (+, -, '*', '/'): " # Enclose '*' and '/' in single quotes
read operation
case $operation in
+) result=$((num1 + num2));;
-) result=$((num1 - num2));;
'*') result=$((num1 * num2));; # Use single quotes to specify '*'
'/') result=$((num1 / num2));; # Use single quotes to specify '/'
*) echo "Invalid operation"; exit 1;;
esac
echo "Result: $result"
2.print Febonacci series or prime number
#!/bin/bash
echo "Enter 'F' for Fibonacci series or 'P' for prime numbers: "
read choice
if [ "$choice" = "F" ]; then
echo "Enter the number of terms in Fibonacci series: "
read n
a=0
b=1
echo "Fibonacci Series: "
for ((i=0; i<n; i++)); do
echo -n "$a "
temp=$((a + b))
a=$b
b=$temp
done
echo
elif [ "$choice" = "P" ]; then
echo "Enter the range for prime numbers: "
read range
echo "Prime Numbers: "
for ((num=2; num<=range; num++)); do
is_prime=1
for ((i=2; i<=num/2; i++)); do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
done
if [ $is_prime -eq 1 ]; then
echo -n "$num "
fi
done
echo
else
echo "Invalid choice"
exit 1
fi
3.Input string vowels , constents , special chrecter , digits , spaces , symbol
#!/bin/bash
echo "Enter a string: "
read input_string
vowels=$(echo $input_string | tr -cd 'AEIOUaeiou' | wc -c)
consonants=$(echo $input_string | tr -cd 'BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz' | wc -c)
digits=$(echo $input_string | tr -cd '0-9' | wc -c)
spaces=$(echo $input_string | tr -cd ' ' | wc -c)
symbols=$(echo $input_string | tr -cd '!@#$%^&*()_+' | wc -c)
echo "Vowels: $vowels"
echo "Consonants: $consonants"
echo "Digits: $digits"
echo "Spaces: $spaces"
echo "Symbols: $symbols"
4.input file name count the lines word and chrecters
#!/bin/bash
echo "Enter the filename: "
read filename
if [ -f "$filename" ]; then
lines=$(wc -l < "$filename")
words=$(wc -w < "$filename")
characters=$(wc -m < "$filename")
echo "Lines: $lines"
echo "Words: $words"
echo "Characters: $characters"
else
echo "File not found"
fi
5.check 2 file and if its same containt then delete any on
#!/bin/bash
echo "Enter the path to the first file: "
read file1
echo "Enter the path to the second file: "
read file2
if cmp -s "$file1" "$file2"; then
echo "The files have the same content."
echo "Do you want to delete one of them? (y/n): "
read choice
if [ "$choice" = "y" ]; then
echo "Which file do you want to delete? Enter '1' or '2': "
read file_to_delete
if [ "$file_to_delete" = "1" ]; then
rm "$file1"
echo "File 1 deleted."
elif [ "$file_to_delete" = "2" ]; then
rm "$file2"
echo "File 2 deleted."
else
echo "Invalid choice. No files were deleted."
fi
else
echo "No files were deleted."
fi
else
echo "The files have different content. They are not deleted."
fi
6.find even or odd numberand addition of that number
#!/bin/bash
echo "Enter the starting number: "
read start
echo "Enter the ending number: "
read end
even_sum=0
odd_sum=0
for ((num=start; num<=end; num++)); do
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even."
even_sum=$((even_sum + num))
else
echo "$num is odd."
odd_sum=$((odd_sum + num))
fi
done
echo "Sum of even numbers: $even_sum"
echo "Sum of odd numbers: $odd_sum"
7.reverse string or number check pelindrom
#!/bin/bash
echo "Enter a string or number: "
read input
# Function to reverse a string
reverse_string() {
local input="$1"
local reversed=""
local len=${#input}
for ((i = len - 1; i >= 0; i--)); do
reversed="${reversed}${input:i:1}"
done
echo "$reversed"
}
# Check if the input is a palindrome
is_palindrome() {
local original="$1"
local reversed="$2"
if [ "$original" = "$reversed" ]; then
echo "Palindrome: Yes"
else
echo "Palindrome: No"
fi
}
# Remove non-alphanumeric characters and convert to lowercase
input_cleaned=$(echo "$input" | tr -d -c '[:alnum:]' | tr '[:upper:]' '[:lower:]')
reversed_input=$(reverse_string "$input_cleaned")
echo "Reversed: $reversed_input"
is_palindrome "$input_cleaned" "$reversed_input"
8.enter directory and check sub folder , empty file permisson of file
#!/bin/bash
echo "Enter the directory path: "
read directory
if [ -d "$directory" ]; then
# List subdirectories
echo "Subdirectories in $directory:"
for subdirectory in "$directory"/*; do
if [ -d "$subdirectory" ]; then
echo "$subdirectory"
fi
done
# Check and display empty files and their permissions
echo "Empty files and their permissions in $directory:"
for file in "$directory"/*; do
if [ -f "$file" ]; then
if [ ! -s "$file" ]; then
echo "Empty file: $file"
permissions=$(stat -c "%a" "$file")
echo "Permissions: $permissions"
fi
fi
done
else
echo "$directory is not a valid directory."
fi
Comments
Post a Comment