#TWSBashBlazeChallenge - Day 6

The Mysterious Script
In this Bash scripting challenge, focusing on a mysterious Bash script called mystery.sh Participants are tasked with deciphering the script's purpose, understanding its functionality, and enhancing it by adding comments and explanations. The challenge involves steps such as exploring the script, running and debugging it, identifying its objective, optimizing its performance, and finally, presenting findings. Participants are guided to use Bash debugging techniques, pay attention to variable names, and responsibly use online resources for assistance. The challenge aims to improve participants' Bash scripting skills while unraveling the intrigue of the enigmatic script.
###################################################################
# Author: Sasiram Beeke
# Date: 05/08/2023
# Description: The Mysterious Script,
# Tip: to execute shell give permision +x scriptname
###################################################################
#!/bin/bash
#Print shell command before execute it.
#set -x
#it will exit if any command fails.
#set -e
# Welcome to the Mysterious Script Challenge!
# The Mysterious Function
mysterious_function() {
local input_file="$1"
local output_file="$2"
#here it uses ROT13 ciper encryption technique.the tr command is being used to perform a simple letter substitution cipher known as the ROT13 cipher .
#This means that each letter is replaced by the letter that is 13 positions ahead of it in the alphabet and saves the result into output file.
tr 'A-Za-z' 'N-ZA-Mn-za-m' < "$input_file" > "$output_file"
#It then reverses the content of the output file and saves the reversed content in a temporary file named "reversed_temp.txt."
rev "$output_file" > "reversed_temp.txt"
# A random number between 1 and 10 is generated.
random_number=$(( ( RANDOM % 10 ) + 1 ))
#echo $random_number
# Mystery loop: loop is executed whatever random number is generated.
for (( i=0; i<$random_number; i++ )); do
# The content of "reversed_temp.txt" is reversed and saved in "temp_rev.txt."
rev "reversed_temp.txt" > "temp_rev.txt"
# Again ROT13 ceaser-cipher used and result save in temp_enc.txt
tr 'A-Za-z' 'N-ZA-Mn-za-m' < "temp_rev.txt" > "temp_enc.txt"
# temp_enc.txt file is renamed with reversed_temp.txt
mv "temp_enc.txt" "reversed_temp.txt"
done
# Clean up temporary files
rm "temp_rev.txt"
}
# Main Script Execution
# Check if two arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <input_file> <output_file>"
exit 1
fi
input_file="$1"
output_file="$2"
# Check if the input file exists
if [ ! -f "$input_file" ]; then
echo "Error: Input file not found!"
exit 1
fi
# Call the mysterious function to begin the process
mysterious_function "$input_file" "$output_file"
# Display the mysterious output
echo "The mysterious process is complete. Check the '$output_file' for the result!"
Here, I have executed the script with an unavailable file to perform a test case.

I have created mystery.txt file as an input file, and stores output content In output.txt. after executing Shell prompt message.After I see the content in both file output.txt and reversed_temp.txt

Here, I have used set -x for debugging the script and observing its execution flow for better understanding. Additionally, I have used set -e to exit the script if any command fails. Furthermore, I employed the echo command to print the random number.
In the ROT13 cipher, each letter in the alphabet is shifted 13 positions down the alphabet. This means that each letter is replaced by the letter that is 13 positions ahead of it in the alphabet.
For example, applying ROT13 to the English alphabet:
Original: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Ciphered: N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
my content inside is emiway = rzvnjl by using ROT13 cipher .
Restaurant Order System Challenge
Challenge Description
In this challenge, you will be working on a Bash-based restaurant order system. The provided script, restaurant_order.sh, is partially broken and missing some crucial parts. Your task is to fix and complete the script to create a fully functional restaurant order system.
The Broken Script
The restaurant_order.sh script is provided to you, but some essential parts are missing or not functioning correctly. Here are the broken parts:
Missing Menu Display: The script is missing the code to read and display the menu from the
menu.txtfile. You need to implement the function to read the menu and display it to the customers.Invalid User Input Handling: The script is not handling invalid user input, such as entering a non-existent item number or negative quantities. You need to implement error handling to prompt users for valid inputs when necessary.
Total Bill Calculation: The script is not calculating the correct total bill based on the customer's order. You need to implement the function to calculate the total bill accurately.
#!/bin/bash
#set -e
#set -x
filepath="menu.txt"
# Function to read and display the menu from menu.txt file
function display_menu() {
if [ -e "$filepath" ]; then
echo "Welcome to the Restaurant!"
echo "Menu:"
echo
# Use awk to format the menu items as a numbered list with prices
awk -F',' '{ printf " %d. %s - ₹%d\n", NR, $1, $2 }' "$filepath"
else
echo "Menu not available."
fi
}
# Function to calculate the total bill
function calculate_total_bill() {
local total=0
for item_number in "${!order[@]}"; do
local quantity="${order[$item_number]}"
local price=$(awk -F',' -v n="$item_number" 'NR == n {print $2}' "$filepath")
total=$((total + price * quantity))
done
echo "$total"
}
# Function to handle invalid user input
function handle_invalid_input() {
echo "Invalid input! Please enter a valid item number and quantity."
}
# Main script
display_menu
# TODO: Ask the customer for their name and store it in a variable "customer_name"
echo "Please entre your name:"
read -a customer_name
# Ask for the order
echo "Please enter the item number and quantity (e.g., 1 2 for two Burgers):"
read -a input_order
# Process the customer's order
declare -A order
for (( i=0; i<${#input_order[@]}; i+=2 )); do
item_number="${input_order[i]}"
quantity="${input_order[i+1]}"
# TODO: Add the item number and quantity to the "order" array
# Check if item number is valid and quantity is positive
if [ "$item_number" -gt 0 ] && [ "$item_number" -le $(awk 'END{print NR}' "menu.txt") ] && [ "$quantity" -gt 0 ]; then
order["$item_number"]=$quantity
else
handle_invalid_input
exit 1
fi
done
# Calculate the total bill
total_bill=$(calculate_total_bill)
# TODO: Display a thank-you message to the customer along with the total bill
echo "Thank you, $customer_name! Your total bill is ₹ $total_bill."
Here, I displayed the content inside menu.txt. After that, I executed the script, which produced an output along with a message.

Here, I executed the script with incorrect input to validate our test case, which worked correctly. I provided an item number that is not available in our menu.txt.

here I again validate our test case, Which works correctly. I have given negative number which is not available in our menu.txt

here I again validate our test case, Which works correctly. I have given 0 quantity and 0 item number which is not available in our menu.txt

Recursive Directory Search Challenge
Description
The "Recursive Directory Search" challenge is part of the day-6. In this challenge, participants are tasked with creating a Bash script that performs a recursive search for a specific file within a given directory and its subdirectories. The script provided for this challenge is not functioning correctly, and participants must fix and improve it to achieve the desired Behavior.
Challenge Details
Objective: Your goal is to fix the provided Bash script, recursive_search.sh, and ensure it performs the recursive search as described below:
The script should take two command-line arguments: the directory to start the search and the target file name to find.
The search should be recursive, meaning it should look for the target file not only in the specified directory but also in all its subdirectories and their subdirectories, and so on.
When the target file is found, the script should print the absolute path of the file and then exit.
Proper error handling should be in place to handle cases where the directory does not exist or the target file is not found.
###################################################################
# Author: Sasiram Beeke
# Date: 05/08/2023
# Description: Recursive Directory Search Challenge
# Tip: to execute shell give permision +x scriptname
###################################################################
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: ./recursive_search.sh <directory> <target_file>"
exit 1
fi
search_directory="$1"
target_file="$2"
# Recursive function to search for the target file
search_file_recursive() {
current_dir="$1"
for item in "$current_dir"/*; do
if [ -f "$item" ] && [ "${item##*/}" = "$target_file" ]; then
echo "Found: $item"
exit 0
elif [ -d "$item" ]; then
search_file_recursive "$item"
fi
done
}
search_file_recursive "$search_directory"
echo "File not found: $target_file"
exit 1
Here, I first created three directories: test, test1, and test2, along with their respective files. Afterward, I executed the script to search for t7.txt inside the test directory, which is not present, resulting in a "not found" message. Subsequently, I provided a filename that does exist, and the search was successful.

for more testing i have creatd team.txt inside test/test3/test4/test5 and execute script which works correctly.




