sem6Linux
sem6Linux
to 30
#!/bin/bash
echo "Prime numbers between 1 and 30:"
is_prime() {
num=$1
if [ $num -lt 2 ]; then
return 1
fi
for ((i = 2; i * i <= num; i++)); do
if [ $((num % i)) -eq 0 ]; then
return 1
fi
done
return 0
}
for num in {1..30}
do
if is_prime "$num"; then
echo "$num"
fi
done
2)write a shell script to check given input is valid email id or not
#!/bin/bash
echo "Enter an email ID:"
read email
regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if [[ $email =~ $regex ]]; then
echo "Valid Email ID"
else
echo "Invalid Email ID"
fi
Explana on:
1. Prompts the user to enter an email.
2. Uses a regular expression (regex) to match a standard email format:
o ^[a-zA-Z0-9._%+-]+ → Starts with le ers, numbers, dots,
underscores, %, +, or -.
o @ → Must contain @ symbol.
o [a-zA-Z0-9.-]+ → Domain name (le ers, numbers, dots, or
hyphens).
o \.[a-zA-Z]{2,}$ → Must end with a dot (.) followed by at least 2
le ers (e.g., .com, .org).
3. Checks if the input matches the pa ern:
o If it matches, prints "Valid Email ID".
o Otherwise, prints "Invalid Email ID".
2)write a shell script to find whether the supplied user working on network or
not.if he is working then display is login me
#!/bin/bash
echo "Enter the username to check:"
read username
login_info=$(who | grep "^$username")
if [ -n "$login_info" ]; then
login_ me=$(echo "$login_info" | awk '{print $3, $4}')
echo "User '$username' is currently logged in."
echo "Login me: $login_ me"
else
echo "User '$username' is not logged in."
fi
Explana on:
1. Reads the username from the user.
2. Uses who command to list logged-in users and filters for the given
username using grep.
3. Checks if the username exists in the who output:
o If found, extracts the login me using awk.
o If not found, displays "User is not logged in."
3)write a shell script which accepts a filename as a input.find out whether it
is ordinary file or directory .if a file is available then display all file access
permission on screen
#!/bin/bash
echo "Enter the filename or directory name:"
read filename
if [ -e "$filename" ]; then
if [ -d "$filename" ]; then
echo "'$filename' is a directory."
elif [ -f "$filename" ]; then
echo "'$filename' is an ordinary file."
else
echo "'$filename' is neither a regular file nor a directory."
fi
echo "File permissions for '$filename':"
ls -l "$filename" | awk '{print $1, $9}'
else
echo "File or directory '$filename' does not exist."
fi
Explana on:
1. Takes input from the user (read filename).
2. Checks if the file/directory exists (-e "$filename").
3. Determines if it's a directory or ordinary file:
o -d "$filename" → Directory.
o -f "$filename" → Ordinary file.
o Otherwise, it’s neither a regular file nor a directory.
4. Displays file permissions using ls -l and extracts the permission part using awk.
5)write a shell script which copies a files from one directory to another during
copy command
#!/bin/bash
echo "Enter the source file path:"
read source_file
echo "Enter the des na on directory:"
read des na on_dir
if [ -f "$source_file" ]; then
if [ -d "$des na on_dir" ]; then
cp "$source_file" "$des na on_dir"
echo "File copied successfully to $des na on_dir."
else
echo "Error: Des na on directory does not exist."
fi
else
echo "Error: Source file does not exist."
fi
Explana on:
1. Takes user input for:
o Source file path
o Des na on directory
2. Checks if the source file exists (-f "$source_file").
3. Checks if the des na on directory exists (-d "$des na on_dir").
4. Copies the file using the cp command.
5. Displays a success or error message based on the opera on result.
6)create a data file which contains given format and perform the given
opera ons on that data file using sed command