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

Week11 USP Lab Tasks

USP Lab

Uploaded by

srkakarlapudi
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)
28 views3 pages

Week11 USP Lab Tasks

USP Lab

Uploaded by

srkakarlapudi
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

WEEK-11 (Korn Shell Scripting)

1. Greeting Based on Time:


Write a shell script which will greet you “Good Morning,” “Good Afternoon,” “Good
Evening,” and “Good Night” according to the current time.
2. Binary Search:
Write a shell script to search for a given number using a binary search.
3. Terminal Lock:
Write a shell script to lock the terminal. The script should prompt for a password to
unlock the terminal.
4. String Analysis:
Write a shell script to find the number of vowels, consonants, digits, whitespaces, and
special characters in a given string.
5. Mathematical Tables:
Write a shell script to generate the multiplication table of a given number.

1. Write a shell script which will greet you “Good Morning,” “Good Afternoon,”
“Good Evening,” and “Good Night” according to the current time.

#!/bin/ksh

hour=$(date +"%H")

if [ $hour -ge 0 ] && [ $hour -lt 12 ]; then


echo "Good Morning!"
elif [ $hour -ge 12 ] && [ $hour -lt 17 ]; then
echo "Good Afternoon!"
elif [ $hour -ge 17 ] && [ $hour -lt 21 ]; then
echo "Good Evening!"
else
echo "Good Night!"
fi

2. Write a shell script to search for a given number using a binary search.
#!/bin/ksh

binary_search() {
local arr=("$@")
local search=$1
local left=0
local right=$((${#arr[@]} - 1))
while [ $left -le $right ]; do
mid=$(( (left + right) / 2 ))
if [ ${arr[$mid]} -eq $search ]; then
echo "Number found at index $mid"
return 0
elif [ ${arr[$mid]} -lt $search ]; then
left=$((mid + 1))
else
right=$((mid - 1))
fi
done

echo "Number not found"


return 1
}

echo "Enter the numbers (space-separated):"


read -a arr
echo "Enter the number to search:"
read search

# Sorting the array (required for binary search)


arr=($(printf "%s\n" "${arr[@]}" | sort -n))

binary_search "$search" "${arr[@]}"

3. Write a shell script to lock the terminal. The script should prompt for a
password to unlock the terminal.
#!/bin/ksh

password="secret"

while true; do
echo "Terminal is locked. Enter the password to unlock:"
read -s input

if [ "$input" = "$password" ]; then


echo "Terminal unlocked."
break
else
echo "Incorrect password. Try again."
fi
done
4. Write a shell script to find the number of vowels, consonants, digits, whitespaces,
and special characters in a given string.
#!/bin/ksh

echo "Enter a string:"


read str

vowels=$(echo $str | grep -o -i "[aeiou]" | wc -l)


consonants=$(echo $str | grep -o -i "[bcdfghjklmnpqrstvwxyz]" | wc -l)
digits=$(echo $str | grep -o "[0-9]" | wc -l)
whitespaces=$(echo $str | grep -o "[[:space:]]" | wc -l)
special_chars=$(echo $str | grep -o "[^a-zA-Z0-9[:space:]]" | wc -l)

echo "Vowels: $vowels"


echo "Consonants: $consonants"
echo "Digits: $digits"
echo "Whitespaces: $whitespaces"
echo "Special Characters: $special_chars"

5. Write a shell script to generate the multiplication table of a given number.


#!/bin/ksh

echo "Enter a number:"


read number

echo "Multiplication table for $number:"

for i in {1..10}
do
echo "$number x $i = $((number * i))"
done

You might also like