CN Lab1 202211099.2
CN Lab1 202211099.2
IIITV-ICD
BASIC SHELL SCRIPTING
By: 202211099
Output-
Output-
Output-
fact() {
local num=$1
local result=1
for ((i=num; i>=1; i--)); do
result=$((result * i))
done
echo $result
}
read -p "Enter a number: " num
fact $num
Output:
6) Write a shell script to check whether a given character is vowel or consonant.
if [[ $ch == [AEIOUaeiou] ]]
then
echo "$ch is a vowel."
else
echo "$ch is a consonant."
fi
Output:
Output-
8) Write a shell script to check whether a given string is a palindrome.
Output-
Output-
10) Write a shell script to reverse a string.
Output:
reverse=0
original=$number
Output:
12) Write a shell script to check whether a file exists and is readable.
if [ -r x.txt ]
then
echo "File exists and is readable"
else
echo "File doesn't exists."
fi
Output:
13) Write a shell script to count the number of lines, words, and characters in a file.
Output:
Output:
15) Write a shell script to find all prime numbers between two numbers.
echo "Enter a number:"
read number
i=2
if [ $number -lt 2 ]
then
echo "$number is not a prime number."
exit
fi
Output:
16) Write a shell script to find whether an input year is leap year or not.
Output:
17) Write a shell script to delete files older than 7 days from a specific directory.
if [ -d "$DIRECTORY" ]; then
find "$DIRECTORY" -type f -mtime +7 -exec rm -f {} \;
echo "Files older than 7 days have been deleted from $DIRECTORY."
else
echo "Error: Directory $DIRECTORY does not exist."
fi
Output:
Output:
19) Write a shell script to display all the files and directories in the current directory.
Output:
20) Write a shell script to find the sum of all even numbers from 1 to n.
Output:
21) Write a shell script to implement a basic calculator (addition, subtraction, multiplication,
division) using case statements.
case $op in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a - $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo "scale=2; $a / $b" | bc`
;;
esac
echo "Result : $res"
Output:
22) Write a shell script to find the number of months between two dates.
calculate_months_between() {
local start_date=$1
local end_date=$2
echo $month_diff
}
Output:
23) Write a shellscript to Swapping Two Numbers Without Using a Third Variable.
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
echo "Before swapping: num1 = $num1, num2 = $num2"
num1=$((num1 + num2))
num2=$((num1 - num2))
num1=$((num1 - num2))
echo "After swapping: num1 = $num1, num2 = $num2"
Output:
Output:
25) Write a shell script to concatenate two strings input by the user.
Output:
26) Write a shell script to display all the files and directories in the current directory.
echo "Files and directories in the current directory:"
ls -l
Output:
27) Write a shell script to read a file line by line and print each line with its line number.
read -p "Enter the file name: " filename
if [ ! -f "$filename" ]; then
echo "Error: File $filename not found."
exit 1
fi
line_number=1
Output:
28) Write a shell script to create a backup of a directory by copying it to another location.
if [ ! -d "$source_dir" ]; then
echo "Error: Source directory $source_dir does not exist."
exit 1
fi
if [ ! -d "$backup_dest" ]; then
echo "Error: Backup destination $backup_dest does not exist. Creating it now..."
mkdir -p "$backup_dest"
fi
cp -r "$source_dir" "$backup_dir"
Output:
29) Write a shell script to check the disk usage and send an alert if it exceeds a certain threshold.
THRESHOLD=80
ALERT_EMAIL="[email protected]"
Output:
found=0
for i in "${!array[@]}"; do
if [ "${array[$i]}" -eq "$search_element" ]; then
echo "Element $search_element found at index $i."
found=1
break
fi
done
Output: