Sem5Linux
Sem5Linux
#!/bin/bash
echo "Enter the filename to check:"
read filename
if [ -e "$filename" ]; then
echo "The file '$filename' exists."
else
echo "The file '$filename' does not exist."
Fi
Explana on:
1. read filename → Takes user input for the file name.
2. [ -e "$filename" ] → Checks if the file exists (-e flag is used for
existence check).
3. if...else → Prints a message based on whether the file exists or
not.
Addi onal Condi ons:
[ -f "$filename" ] → Checks if it's a regular file.
[ -d "$filename" ] → Checks if it's a directory.
[ -r "$filename" ] → Checks if the file is readable.
[ -w "$filename" ] → Checks if the file is writable.
2)write a shell script to copy a file
#!/bin/bash
echo "Enter the source filename:"
read source
echo "Enter the des na on filename:"
read des na on
if [ -e "$source" ]
then
cp "$source" "$des na on"
echo "File copied successfully from '$source' to '$des na on'."
else
echo "Error: Source file '$source' does not exist."
fi
Explana on:
1. read source → Takes the name of the source file from the user.
2. read des na on → Takes the name of the des na on file.
3. [ -e "$source" ] → Checks if the source file exists before
copying.
4. cp "$source" "$des na on" → Copies the file.
5. if-else → Displays a success message or an error if the source
file does not exist.
3)write a shell script to check the given number is odd or even
#!/bin/bash
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is an even number."
else
echo "$num is an odd number."
fi
Explana on:
1. read num → Takes user input for the number.
2. $((num % 2)) → Computes the remainder when divided by 2.
3. if [ $((num % 2)) -eq 0 ] → If the remainder is 0, the number is
even; otherwise, it's odd.
4)write a shell script to check file permission
#!/bin/bash
echo "Enter the filename to check permissions:"
read filename
if [ ! -e "$filename" ]; then
echo "Error: File '$filename' does not exist."
exit 1
fi
if [ -r "$filename" ]; then
echo "File '$filename' has READ permission."
else
echo "File '$filename' does NOT have READ permission."
fi
if [ -w "$filename" ]; then
echo "File '$filename' has WRITE permission."
else
echo "File '$filename' does NOT have WRITE permission."
fi
if [ -x "$filename" ]; then
echo "File '$filename' has EXECUTE permission."
else
echo "File '$filename' does NOT have EXECUTE permission."
fi
5)write a shell script to calculate grade of student
#!/bin/bash
echo "Enter the student's marks (out of 100):"
read marks
if [ "$marks" -ge 90 ]; then
grade="A+"
elif [ "$marks" -ge 80 ]; then
grade="A"
elif [ "$marks" -ge 70 ]; then
grade="B"
elif [ "$marks" -ge 60 ]; then
grade="C"
elif [ "$marks" -ge 50 ]; then
grade="D"
else
grade="Fail"
fi
echo "Student's Grade: $grade"
Explana on:
3. Grading Logic:
#!/bin/bash
echo "Enter a word:"
read word
if [[ "$word" =~ [AEIOUaeiou] ]]; then
echo "The word '$word' contains vowels."
if [[ "$word" =~ [aeiou] ]]; then
echo "The word contains lowercase vowels."
fi
if [[ "$word" =~ [AEIOU] ]]; then
echo "The word contains uppercase vowels."
fi
else
echo "The word '$word' does not contain any vowels."
fi
Explana on:
1. User Input: Reads a word from the user.
2. Regex Check:
o [[ "$word" =~ [AEIOUaeiou] ]] → Checks if the word contains any vowel.
o [[ "$word" =~ [aeiou] ]] → Checks for lowercase vowels.
o [[ "$word" =~ [AEIOU] ]] → Checks for uppercase vowels.
3. Output: Displays whether the word contains vowels and specifies their case.
7)write a shell script to display given year is leap year or not.
#!/bin/bash
echo "Enter a year:"
read year
if (( year % 400 == 0 )); then
echo "$year is a Leap Year."
elif (( year % 100 == 0 )); then
echo "$year is NOT a Leap Year."
elif (( year % 4 == 0 )); then
echo "$year is a Leap Year."
else
echo "$year is NOT a Leap Year."
fi
Explana on:
1. User Input: Reads the year from the user.
2. Valida on: Ensures the input is a posi ve number.
3. Leap Year Condi ons:
o If divisible by 400 → Leap Year
o Else if divisible by 100 → Not a Leap Year
o Else if divisible by 4 → Leap Year
o Otherwise → Not a Leap Year
4. Displays the result.
8)write a shell script to greet message according to me
#!/bin/bash
hour=$(date +"%H")
if [ "$hour" -ge 5 ] && [ "$hour" -lt 12 ]; then
gree ng="Good Morning!"
elif [ "$hour" -ge 12 ] && [ "$hour" -lt 17 ]; then
gree ng="Good A ernoon!"
elif [ "$hour" -ge 17 ] && [ "$hour" -lt 21 ]; then
gree ng="Good Evening!"
else
gree ng="Good Night!"
fi
echo "$gree ng"
Explana on:
1. Gets the Current Hour using date +"%H".
2. Checks the Time Range:
o 05:00 - 11:59 → Good Morning!
o 12:00 - 16:59 → Good A ernoon!
o 17:00 - 20:59 → Good Evening!
o 21:00 - 04:59 → Good Night!
3. Displays the Appropriate Gree ng.
9)write a shell script to print the fibbonacci series
#!/bin/bash
a=0
b=1
for i in {1..10}
do
echo "$a"
fn=$((a+b))
a=$b
b=$fn
done
How It Works:
Starts with 0 and 1 (Fibonacci sequence begins with these
numbers).
In each itera on:
1. Prints the current value of a.
2. Computes the next Fibonacci number as fn = a + b.
3. Updates values of a and b for the next itera on.
The loop runs 10 mes to print the first 10 Fibonacci numbers.
10)write a shell script to print the number between 1 to 10
#!/bin/bash
for num in {1..10}
do
echo "$num"
done
11) write a shell script to read name gender and mari al status and
display the same
#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your gender (Male/Female/Other):"
read gender
echo "Enter your marital status (Single/Married/Divorced):"
read marital_status
echo "===== User Details ====="
echo "Name : $name"
echo "Gender : $gender"
echo "Marital Status : $marital_status"