0% found this document useful (0 votes)
6 views3 pages

L1.p New

The document contains multiple shell scripts for various tasks, including displaying the first fifty prime numbers, calculating the length of a string, extracting a substring, copying file contents, checking if a number is an Armstrong number, and determining the type of a command-line argument. Each script includes prompts for user input and appropriate logic to perform the specified operations. The scripts demonstrate basic shell scripting techniques and string manipulation in a Unix-like environment.

Uploaded by

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

L1.p New

The document contains multiple shell scripts for various tasks, including displaying the first fifty prime numbers, calculating the length of a string, extracting a substring, copying file contents, checking if a number is an Armstrong number, and determining the type of a command-line argument. Each script includes prompts for user input and appropriate logic to perform the specified operations. The scripts demonstrate basic shell scripting techniques and string manipulation in a Unix-like environment.

Uploaded by

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

Write a shell script to display first fifty prime numbers.

count=0

num=2

echo "The first 50 prime numbers are:"

# Loop until 50 prime numbers are found

while [ $count -lt 50 ]; do

is_prime=1

for ((i = 2; i * i <= num; i++)); do

if ((num % i == 0)); then

is_prime=0

break

fi

done

if [ $is_prime -eq 1 ]; then

echo -n "$num "

count=$((count + 1))

fi

num=$((num + 1))

done

Write a shell script to perform the following string operations to find the length of a given string.

echo "Enter a string: "

read str

# Calculate the length of the string len=${#str}

# Display the length of the string echo "The

length of the string \"$str\" is: $len"

Write a shell script to perform to extract a sub-string from a given string


# Prompt the user to enter a string

echo "Enter a string: " read str

# Prompt the user to enter the starting position and length of the substring

echo "Enter the starting position " read start_pos echo "Enter the length

of the substring: "

read len

# Extract the substring using parameter expansion

substring=${input_string:start_pos:len} # Display

the extracted substring echo "The extracted

substring is: \"$substring\""

Write a shell script to copy the contents of one file into another

echo "Enter the source file name: " read source_file echo "Enter

the destination file name: " read destination_file

cp "$source_file" "$destination_file" echo "Contents of \"$source_file\" have

been copied to \"$destination_file\"."

Write a shell script to check whether given number is Armstrong or not.

# Read input from the user

echo "Enter a number: "

read num

temp=$num

sum=0

digits=${#num}

while [ $temp -gt 0 ]; do


digit=$((temp % 10))
sum=$((sum + digit**digits))
temp=$((temp / 10))
done

Write a shell script that takes a command –line argument and reports on whether it is directory, a
file, or something else.

if [ -d " $1 " ]; then

echo "$1 is a directory."

elif [ -f "$1 " ]; then

echo "$1 is a regular file."

else

echo "$1 is something else.”

Fi

Q—Sub String

echo "Enter a string”

read str

echo “start point”

read start

echo “length enter”

read len

substr=$[str:start:len]

You might also like