Week11 USP Lab Tasks
Week11 USP Lab Tasks
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")
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
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
for i in {1..10}
do
echo "$number x $i = $((number * i))"
done