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

sem6Linux

The document contains a series of shell scripts for various tasks, including printing prime numbers, validating email IDs, checking user login status, determining file types and permissions, copying files, performing operations on a data file, and counting file attributes. Each script is accompanied by explanations detailing its functionality and usage. The scripts demonstrate basic shell scripting techniques and command usage in a Linux environment.

Uploaded by

sankettippe9766
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

sem6Linux

The document contains a series of shell scripts for various tasks, including printing prime numbers, validating email IDs, checking user login status, determining file types and permissions, copying files, performing operations on a data file, and counting file attributes. Each script is accompanied by explanations detailing its functionality and usage. The scripts demonstrate basic shell scripting techniques and command usage in a Linux environment.

Uploaded by

sankettippe9766
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)write a shell script using grep command to print prime numbers between 1

to 30
#!/bin/bash
echo "Prime numbers between 1 and 30:"
is_prime() {
num=$1
if [ $num -lt 2 ]; then
return 1

for ((i = 2; i * i <= num; i++)); do
if [ $((num % i)) -eq 0 ]; then
return 1

done

return 0
}
for num in {1..30}
do
if is_prime "$num"; then
echo "$num"

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"

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

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

echo "File permissions for '$filename':"
ls -l "$filename" | awk '{print $1, $9}'
else
echo "File or directory '$filename' does not exist."

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

else
echo "Error: Source file does not exist."

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

Step 1: Create the Data File


cat > data.txt
my name is shak
my age is 24
i am learning bscIII at
shivraj college gadhinglaj
Step 2: Perform sed Opera ons
Delete the 2nd Line
sed '2d' data.txt
Print Only the 3rd Line
sed -n '3p' data.txt
Replace "shak " with "vijay"
sed 's/shak /vijay/g' data.txt
7)write a shell script to copy a file using command line argument ,source file
must be exist and readable and target file must be non exis ng file name
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <source_file> <target_file>"
exit 1

source_file="$1"
target_file="$2"
if [ ! -f "$source_file" ]; then
echo "Error: Source file '$source_file' does not exist."
exit 2

if [ ! -r "$source_file" ]; then
echo "Error: Source file '$source_file' is not readable."
exit 3

if [ -e "$target_file" ]; then
echo "Error: Target file '$target_file' already exists. Choose another name."
exit 4

cp "$source_file" "$target_file"
if [ $? -eq 0 ]; then
echo "File copied successfully from '$source_file' to '$target_file'."
else
echo "Error: File copy opera on failed."
exit 5

8)write a shell script which works similar to wc command accept filename as
command lline argument
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1

file="$1"
if [ ! -f "$file" ]; then
echo "Error: File '$file' does not exist."
exit 2

if [ ! -r "$file" ]; then
echo "Error: File '$file' is not readable."
exit 3

lines=$(wc -l < "$file")
words=$(wc -w < "$file")
characters=$(wc -c < "$file")
echo "File: $file"
echo "Lines: $lines"
echo "Words: $words"
echo "Characters: $characters"
9)Accept any word through command line argument and find out its length
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <word>"
exit 1

word="$1"
length=${#word}
echo "The length of the word '$word' is: $length"

10)write a shell script which sort an array of integers in ascending order


#!/bin/bash
echo "Enter numbers separated by space:"
read -a arr
n=${#arr[@]}
for ((i = 0; i < n; i++)); do
for ((j = 0; j < n - i - 1; j++)); do
if ((arr[j] > arr[j + 1])); then
# Swap elements
temp=${arr[j]}
arr[j]=${arr[j + 1]}
arr[j + 1]=$temp

done
done
echo "Sorted Array in Ascending Order: ${arr[@]}"

You might also like