Basics of 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.
##############################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task1.
# Tip: to execute shell give permision +x scriptname
#############################################################################
#!/bin/bash
echo '############### With single lineComment ###############################'
echo "welcome to my first example for single line comment"
#echo "this line us for comment purpose"
echo "single line comment example ends here"
echo '############### using << With multi line comment ######################'
<<EOF
echo "this is example for multi line comment"
echo "multi line comment example"
EOF
echo 'after multiline comment code print here'
echo '############ using : operator with multi line comment #################'
: '
echo "this is first line"
echo "this is second line"
echo "this is third line"
'
echo "this is fourth line"
Output
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.
##############################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task2 .
# Tip: to execute shell give permision +x scriptname
#############################################################################
#!/bin/bash
echo "echo is used to display text or message in terminal"
echo "welcome to the shell scripting"
Output
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.
######################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task3.
# Tip: to execute shell give permision +x scriptname
######################################################################
#!/bin/bash
name="sasiram beeke"
echo "my name is $name"
address='pune'
echo "I am from $address"
echo "I am $name and i am from $address"
Output
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.
######################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task4.
# Tip: to execute shell give permision +x scriptname
######################################################################
#!/bin/bash
echo 'Enter first number'
read a
echo 'Enter second number'
read b
result=$((a+b))
echo "Addition for two number i.e. $a and $b is:- $result"
Output
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.
######################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task5.
# Tip: to execute shell give permision +x scriptname
######################################################################
#!/bin/bash
echo "todays date is `date`" ##describe date.
echo "$0" ##describe name of your file or script
echo "$#" ##describe number of arguments passed to the script
echo "$USER" ##desribe current username of the user
echo "$SHELL" ##describe path to the shell program being used
Output
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.
######################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Task6.
# Tip: to execute shell give permision +x scriptname
######################################################################
#!/bin/bash
echo "All text files which are available in current directory:"
ls *.txt #it will display all text files using * wildcard