USP LAB Week 11 Programs
USP LAB Week 11 Programs
To write a shell script that greets the user with "Good Morning," "Good Afternoon," "Good
Evening," or "Good Night" based on the current time.
Description
This shell script uses the current hour of the system time to determine the appropriate
greeting message. The script reads the current hour using the date command and, based on
conditions, it prints the corresponding greeting.
1. Date Command:
o The date command with the +"%H" format extracts the current hour (in 24-
hour format).
2. Conditional Execution:
o if-elif-else statements are used to display different greetings based on the
current hour.
o Relational Operators (-ge, -lt) help determine the time range for each
greeting.
3. Shell Variables:
o The hour variable stores the current hour value.
Code
#!/bin/ksh
Output Examples
Example Run 1
Time: 10:30 AM
$ ./greeting.ksh
Good Morning!
Example Run 2
Time: 2:00 PM
$ ./greeting.ksh
Good Afternoon!
Example Run 3
Time: 7:15 PM
$ ./greeting.ksh
Good Evening!
Example Run 4
Time: 10:45 PM
$ ./greeting.ksh
Good Night!
Inferences
To write a shell script that performs a binary search on a sorted array to find the position of
a target element.
Description
This shell script allows the user to input a sorted array of elements and a target element to
search for. It implements the binary search algorithm to efficiently find the position of the
target element in the array. The script assumes that the array is sorted in ascending order.
1. Input Handling:
o The script reads a sorted array from the user and the target element to search
for.
2. Positional Parameters:
o The input array is converted into positional parameters using set -- $input,
allowing easy access to array elements.
3. Binary Search Logic:
o Low and High Indices: Initial indices are set to track the search range.
o Looping Structure: A while loop continues until the low index is less than or
equal to the high index.
o Midpoint Calculation: The midpoint of the current search range is calculated
and compared with the target.
4. Error Handling:
o The script uses 2>/dev/null to suppress errors when comparing non-numeric
values.
Code
#!/bin/sh
1. Reading Inputs:
o The script prompts the user to enter the sorted array and the target element
to search for.
2. Setting Positional Parameters:
o The command set -- $input converts the input string into positional
parameters, allowing each element of the array to be accessed via $1, $2, etc.
3. Binary Search Logic:
o The script initializes low and high indices to define the search range.
o A while loop continues as long as low is less than or equal to high:
The midpoint is calculated.
The middle element is fetched using eval.
The script compares the middle element with the target:
If equal, the position is printed, and the script exits.
If the middle element is less than the target, the low index is
updated.
If the middle element is greater than the target, the high index
is updated.
4. Output Results:
o If the target element is found, its position is displayed. If not found, a message
indicates that the target is not in the array.
Output Examples
Example Run 1
$ ./binary_search.ksh
Enter the sorted array elements (space-separated):
1 2 3 4 5 6 7 8 9 10
Enter the element to search for:
5
Element 5 found at position 5.
Example Run 2
$ ./binary_search.ksh
Enter the sorted array elements (space-separated):
1 2 3 4 5 6 7 8 9 10
Enter the element to search for:
11
Element 11 not found.
Inferences
The script effectively utilizes binary search, which significantly reduces the number
of comparisons required to find an element in a sorted array compared to linear
search.
The handling of positional parameters demonstrates how to work with dynamic input
in shell scripts.
To write a Korn shell (ksh) script that sets a password, locks the screen, and allows
unlocking only upon entering the correct password.
Description
This shell script demonstrates how to create a basic password-protected screen lock. The
user first sets a password to lock the screen. The screen remains locked until the correct
password is entered. The input is hidden while entering the password using stty -echo,
ensuring privacy. This script demonstrates basic input handling, looping, and conditional
logic.
Code
#!/bin/ksh
Outputs
$ ./screen_lock.ksh
Set a password to lock the screen:
<password input not displayed>
Password has been set. Screen is now locked.
$ ./screen_lock.ksh
Set a password to lock the screen:
<password input not displayed>
Password has been set. Screen is now locked.
Inferences
Hiding password input using stty -echo ensures security during password entry.
The while loop provides continuous prompts until the correct password is entered,
ensuring access control.
Conditional statements allow the script to unlock the screen only if the correct
password is entered.
This script demonstrates simple authentication logic and secure input handling
using Korn shell commands.
AIM
To write a Korn shell (ksh) script that counts the number of vowels, consonants, digits,
whitespaces, and special characters in a given string.
Description
This shell script reads a string from the user and counts different types of characters within
the string. The grep command is used with appropriate regular expressions to extract each
character type, and wc -l is used to count the occurrences.
The task demonstrates string manipulation, pattern matching using grep, and counting
operations in a shell script.
Code
#!/bin/ksh
Outputs
Example Run 1
$ ./char_count.ksh
Enter a string:
Hello World! 123
Vowels: 3
Consonants: 7
Digits: 3
Whitespaces: 2
Special Characters: 1
Example Run 2
$ ./char_count.ksh
Enter a string:
#Ksh_Scripting_101!
Vowels: 3
Consonants: 11
Digits: 3
Whitespaces: 0
Special Characters: 3
Inferences
This script efficiently processes the input string to count specific character types using
pattern matching.
grep is used with various regular expressions to filter character types, and wc -l
gives the counts.
The script demonstrates string manipulation techniques and basic regex operations
in shell scripting.
#!/bin/ksh