Day 1 : Shell Scripting
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.
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.
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.
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.
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.
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.
Solution :
Create a file using vi editor say day1.sh
Write the code inside it
#Shebang or hashbang. It is the first line of a script file and is used to specify the interpreter that should be used to execute the script.
#!/bin/bash
# Task 1: Comments
# This script demonstrates various bash scripting tasks with comments explaining each step.
# Task 2: Echo
echo "Welcome to the Bash Scripting Adventure!"
# Task 3: Variables
# Declare variables
name="Shivam Anand"
age=240
city="India"
# Task 4: Using Variables
num1=10
num2=20
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"
# Task 5: Using Built-in Variables
echo "Current User: $USER"
echo "Home Directory: $HOME"
echo "Current Date: $(date)"
# Task 6: Wildcards
# List all files with .txt extension in the current directory
echo "Text files in current directory:"
ls *.txt
# List all files with "file" in the middle of the filename
echo "Files containing 'file' in the name:"
ls file*.txt
Save and exit the file using 'ESC +:wq'
Make the file executable
chmod +x day1.sh
- Run the file
./day1.sh
- See the output
Here is my day 1 solution for all the tasks, and I am attaching my GitHub repository for the same. I'm happy to take the challenge, let's wait for more exciting scripting.
Here is my solution repository: Github Link.
Thank you for taking out valuable time and effort to read till the very end. Feedback is always appreciated.