Basic of bash scripting
Rakesh Kumar
Test engineer|Deployment|HSS| HLR| IMS| 5G| SIP| SDP|HTTP2|PFCP|Diameter protocol|IMS Deployment| Docker &Kubernetes|
Here we will cover the basics of bash scripting.
Task1: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. for example:-
#!/bin/bash
# First line of the script is the shebang which tells the system how to execute.
Task2: 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.
Syntax: echo [string]
echo "Scripting is fun with me"
Task3: 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.
variable1="hello"
variable2="bash"
Task4: Using variables
Now that we 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.
greeting="$variable1, $variable2"
echo "$greeting Welcome to the world of Bash scripting!"
Task5: using built in variables
Bash provides several built-in variables that hold useful information. This task is to create a bash script that utilizes at least three different built-in variables to display relevant information.
领英推荐
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"
Task6: Wildcard
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.
echo "Files with .txt extension in the current directory:"
ls *.txt
screenshot:
Note:
#Make sure to provide execution permission with the following command:
#chmod +x script.sh
Happy scripting!