Shell Script Assignment
1. Demonstration of General Purpose Utilities
- Common utilities: cat, ls, cp, mv, rm, grep, wc, echo, date, cal, pwd, who, etc.
2. Shell script to check if file exists or not
#!/bin/bash
echo "Enter filename:"
read file
if [ -e "$file" ]; then
echo "File exists."
else
echo "File does not exist."
fi
3. Shell script to copy a file
#!/bin/bash
echo "Enter source file:"
read src
echo "Enter destination file:"
read dest
cp $src $dest
echo "File copied successfully."
4. Shell script to check if number is odd or even
#!/bin/bash
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is Even."
else
echo "$num is Odd."
fi
5. Shell script to check file permission
#!/bin/bash
echo "Enter filename:"
read file
if [ -r "$file" ]; then
echo "File has read permission."
fi
if [ -w "$file" ]; then
echo "File has write permission."
fi
if [ -x "$file" ]; then
echo "File has execute permission."
fi
6. Shell script to calculate grade of student
#!/bin/bash
echo "Enter marks:"
read marks
if [ $marks -ge 90 ]; then
echo "Grade: A+"
elif [ $marks -ge 75 ]; then
echo "Grade: A"
elif [ $marks -ge 60 ]; then
echo "Grade: B"
elif [ $marks -ge 50 ]; then
echo "Grade: C"
else
echo "Grade: Fail"
fi
7. Shell script to check vowel in a word and case
#!/bin/bash
echo "Enter a word:"
read word
if echo "$word" | grep -q '[AEIOUaeiou]'; then
echo "Word contains a vowel."
echo "$word" | grep -o '[AEIOUaeiou]' | while read ch; do
if [[ $ch =~ [AEIOU] ]]; then
echo "Vowel $ch is uppercase."
else
echo "Vowel $ch is lowercase."
fi
done
else
echo "No vowels found."
fi
8. Shell script to check leap year
#!/bin/bash
echo "Enter a year:"
read year
if [ $((year % 400)) -eq 0 ] || { [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ]; }; then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
9. Shell script to greet according to time
#!/bin/bash
hour=$(date +%H)
if [ $hour -lt 12 ]; then
echo "Good Morning!"
elif [ $hour -lt 17 ]; then
echo "Good Afternoon!"
else
echo "Good Evening!"
fi
10. Shell script to print Fibonacci series
#!/bin/bash
echo "Enter number of terms:"
read n
a=0
b=1
echo "Fibonacci series:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
11. Shell script to print numbers 1 to 10
#!/bin/bash
for i in {1..10}
do
echo $i
done
12. Shell script to read and display name, sex, marital status
#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your sex (M/F):"
read sex
echo "Enter your marital status:"
read status
echo "Name: $name"
echo "Sex: $sex"
echo "Marital Status: $status"