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

Sem5Linux

The document contains a series of shell scripts that perform various tasks, including checking file existence, copying files, determining if a number is odd or even, checking file permissions, calculating student grades, identifying vowels in a word, determining leap years, greeting based on time of day, printing Fibonacci series, and displaying user details. Each script is accompanied by explanations of its functionality and logic. The scripts demonstrate basic shell scripting concepts and conditional statements.

Uploaded by

sankettippe9766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Sem5Linux

The document contains a series of shell scripts that perform various tasks, including checking file existence, copying files, determining if a number is odd or even, checking file permissions, calculating student grades, identifying vowels in a word, determining leap years, greeting based on time of day, printing Fibonacci series, and displaying user details. Each script is accompanied by explanations of its functionality and logic. The scripts demonstrate basic shell scripting concepts and conditional statements.

Uploaded by

sankettippe9766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1)write a shell script using if statement to check file exist or not

#!/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."

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."

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

if [ -r "$filename" ]; then
echo "File '$filename' has READ permission."
else
echo "File '$filename' does NOT have READ permission."

if [ -w "$filename" ]; then
echo "File '$filename' has WRITE permission."
else
echo "File '$filename' does NOT have WRITE permission."

if [ -x "$filename" ]; then
echo "File '$filename' has EXECUTE permission."
else
echo "File '$filename' does NOT have EXECUTE permission."

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"

echo "Student's Grade: $grade"

Explana on:

1. User Input: Reads student marks.

2. Valida on: Ensures input is a number between 0 and 100.

3. Grading Logic:

o 90-100 → A+ 80-89 → A 70-79 → B 60-69 → C 50-59 → D <50 → Fail

4. Displays the Grade.


6)write a shell script to find out given word contains vowel and also
the entered vowel is small case or capital

#!/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."

if [[ "$word" =~ [AEIOU] ]]; then
echo "The word contains uppercase vowels."

else
echo "The word '$word' does not contain any vowels."

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."

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!"

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"

You might also like