Welcome to Day 1 of the Bash Scripting Challenge! Today, we will cover the basics of bash scripting to get you started. Let's dive in:
Task 1: Comments
In bash scripts, comments are used to add explanatory notes or disable certain lines of code. Your task is to create a bash script with comments explaining what the script does.
Solutions:
Ex:
#!/usr/bin/env bash
# Say hello to the community
echo "Hello TWS Community!"
Task 2: Echo
The echo
command is used to display messages on the terminal. Your task is to create a bash script that uses echo
to print a message of your choice.
Solutions:
Ex:
echo "Your current working directory is:"
pwd
echo "These are the contents of this directory:"
ls -l
Task 3: Variables
Variables in bash are used to store data and can be referenced by their name. Your task is to create a bash script that declares variables and assigns values to them.
Solutions
Ex:
# Variables
val1="Hello"
val2="TWS Cummunity"
Task 4: Using Variables
Now that you have declared variables, let's use them to perform a simple task. Create a bash script that takes two variables (numbers) as input and prints their sum using those variables.
Solutions
Ex:
# Using Variables
print="$val1,$val2!"
echo "$print Welcome to the SHELL SCRIPTING CHALLENAGE"
Task 5: Using Built-in Variables
Bash provides several built-in variables that hold useful information. Your task is to create a bash script that utilizes at least three different built-in variables to display relevant information.
Solutions
Ex:
# Using Built-in Variables
echo "My current bash path - $BASH"
echo "Bash version I am using - $BASH_VERSION"
echo "PID of bash I am running - $$"
echo "My home directory - $HOME"
echo "Where am I currently? - $PWD"
echo "My hostname - $HOSTNAME"
Task 6: Wildcards
Wildcards are special characters used to perform pattern matching when working with files. Your task is to create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.
Solutions
Ex:
echo "Files with .txt extension in the current directory:"
ls *.txt
# Prompt the user to enter the directory path
read -p "Enter the directory path: " directory_path
# Prompt the user to enter the file extension
read -p "Enter the file extension (e.g., txt, jpg, etc.): " file_extension
# List all files with the specified extension in the given directory
echo "Files with the .$file_extension extension in $directory_path:"
ls "$directory_path"/*."$file_extension"