Day 2: Exploring Files and Directories with Bash - #TWSBashBlazeChallenge๐
๐ Introduction:
Welcome to Day 2 of the #TWSBashBlazeChallenge! Today's challenge focuses on building an interactive script that explores files and directories, while also performing character counting in Bash. We'll create a script called explorer.sh
that will provide you with a fun and interactive way to navigate through your files and directories, and even count characters in your input! Let's get started with some Bash magic! ๐งโโ๏ธ
๐ Part 1: File and Directory Exploration ๐ฃ
Upon running the explorer.sh
script without any command-line arguments, it will display a warm welcome message and list all the files and directories in the current path. ๐ For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). We'll accomplish this by using the ls
command with appropriate options. Let's see how it's done! ๐ต๏ธโโ๏ธ
๐ explorer.sh:
#!/bin/bash
# Part 1: File and Directory Exploration
# Welcome message
echo "Welcome to the Interactive File and Directory Explorer! ๐"
# Infinite loop until the user decides to exit
while true; do
# Display files and directories in the current path using ls command with human-readable sizes
echo "Files and Directories in the Current Path:"
ls -lh
# Ask the user to enter a line of text
echo -n "Enter a line of text (Press Enter without text to exit): "
# Read user input into the variable 'input'
read input
# Check if the user entered an empty string (i.e., pressed Enter without any text)
if [[ -z "$input" ]]; then
echo "Exiting the Interactive Explorer. Goodbye! ๐"
break # Exit the loop if the user entered an empty string
else
# Count the number of characters in the input and display the count
char_count=${#input}
echo "Character Count: $char_count"
fi
done
# End of script
๐ Part 2: Character Counting ๐งฎ
After displaying the file and directory list, the script will prompt you to enter a line of text. ๐ The script will keep reading your input until you provide an empty string (i.e., press Enter without any text). For each line of text entered, the script will count the number of characters and show you the character count. It's an excellent way to play with the script and see how many characters are in your input! ๐
๐ Usage:
Save the script in a file named
explorer.sh
.Make the script executable using the command:
chmod +x
explorer.sh
.Execute the script using:
./
explorer.sh
.
๐ Output Screen:
๐ Conclusion:
Congratulations! You have successfully completed Day 2 of the #TWSBashBlazeChallenge. You now have a powerful and interactive script that allows you to explore files and directories in your current path, along with a handy character-counting feature. ๐ I hope you had fun building this script and learning more about Bash scripting. Stay tuned for more exciting challenges in the upcoming days! Happy scripting and exploring! ๐๐
๐ Checkout GitHub Repository for projects:
๐ github.com/sumanprasad007