Skip to main content

Command Palette

Search for a command to run...

Linux Shell Scripting Basic

Updated
•4 min read•View as Markdown
R

I'm Proficient in a variety of DevOps technologies, including AWS, Linux, Python, Shell Scripting, Docker, Terraform, Jenkins, Git/GitHub, and Computer Networking. I have a strong ability to troubleshoot and resolve issues, and are consistently motivated to expand the knowledge and skills through exploration of new technologies.

Day 1 BashBlaze-7-Days-of-Bash-Scripting-Challenge

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

#!/bin/bash

# This is a single-line comment

# that spans multiple lines

# but each line is treated as a separate comment.

echo "This is the main part of the script."

# You can also "comment out" blocks of code

# by adding # at the beginning of each line.

# This effectively disables the code.

# echo "This line will not be executed."


echo "Script execution completed."

#####################################################
: <<'COMMENT'

This is a multiline comment

that spans several lines

and explains the purpose of the script

COMMENT

echo "Hello, world!"

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.

echo”This  is Rubeena khan ”

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.

#!/bin/bash

# Declare variables and assign values

name="John"

age=30

city="New York"


# Print the values of the variables

echo "Name: $name"

echo "Age: $age"

echo "City: $city"

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.

#!/bin/bash


# Prompt the user to enter the first number

read -p "Enter the first number: " num1


# Prompt the user to enter the second number

read -p "Enter the second number: " num2


# Perform the addition operation

sum=$((num1 + num2))


# Print the result

echo "The sum of $num1 and $num2 is: $sum"

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.

#!/bin/bash


# Get the current user's username

username=$(whoami)


# Get the current date

current_date=$(date +"%Y-%m-%d")


# Get the current working directory

current_directory=$(pwd)


# Display relevant information

echo "Hello, $username!"

echo "Today is $current_date."

echo "You are currently in the directory: $current_directory"

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

#!/bin/bash


# 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"

Thanks

###############################################################

I have recreated the bash script :


#TASK 1:
# "#!/bin/bash" is called shebang,
# It is an interpreter which translate your Bash command into mashine language and redirect your command towards .bash

##################################################################################

#TASK 2:
#This "echo" command will tell the interpreter to print the string written in ""
echo "This is #BashBlaze-7-Days-of-Bash-Scripting-Challenge"

###################################################################################

#TASK 3:
#Variables are memory location identified by the name given by user used to store data of any type

var1="Ruby"
Var2="Devops"

#####################################################################################

#TASK 4 :
#we can  input varible value from user and use read to store the value in varible to use it anytime in our script

echo "Enter  two numbers "
num1=$1
num2=$2

addition=$num1+$num2
echo "Total of two number you have entered in $addition "

#####################################################################################

#TASK 5 :
#Using buit in varible : these are some variable whose values are already defined in linux system
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"

#built in special commands
# first "$#" is used to print number of argument passsed at the time of script execution
echo '$#' $#

# Second "$@" is used to print what parameters were passed at the time of execution of file
echo '$@' $@

# third "$?" is used to check "was last command successfully executed , if answer is zero then "yes"
echo '$?' $?

#####################################################################################

#TASK 6:
#wildcard are special characters used to perform pattern matching when working with files.
mkdir wildcard
cd wildcard
touch dev_emp{1..5}.txt
touch prd_emp{6..10}.pdf
ls -la
rm *.txt

#############################################################################################

echo "This script is written by ruby "
echo "Thanks for reading untill last line "
echo "HAPPY LEARNING "