Skip to main content

Command Palette

Search for a command to run...

Shell Scripting Challenge

Published
•3 min read•View as Markdown
Shell Scripting Challenge

This script will demonstrates practical use of comments, echo, variables, built-in variables, and wildcards in bash scripting.

#!/bin/bash

#Shell Scripting Challenge
#This script will demonstrates practical use of comments, echo, variables, built-in variables, and wildcards in bash scripting.

# 1. Comments
# Comments will help to undersatnd the scripts.
# There are two types of comments
# i> single line comments
# ii> multiline comments

# 2. Echo
# This command will help you to display message of your choice.
echo "Hello, Welcome to Shell Scripting Challenge"

# 3. variables
# It is used to store data of your choice like words,alphabets,and numbers etc.
# You can declare and assign data to variables.

name="Imran"
education="B.Tech"
echo "My name is $name, and i completed $education"

# Above the 'name' is variable and the value is 'Imran'
# To use variables need to use '$' and assign them into commands of your requirment

# 4. Using Variables
# Calculating sum of two numbers

num1=10 #First Number
num2=20 #Second Number
i=`expr $num1 + $num2` #Use the keyword 'expr' to perform arithmatic operations
echo "The sum of $num1 & $num2 is: $i" #Print the output

# Second method to calculate sum
sum=$((num1 + num2))
echo "The sum of $num1 & $num2 is: $i" #Print the output

# 5. Using Built-in Variables
# Bash provides several built-in variables that store useful information.

echo "The Current Working Directory is: $PWD" #Print the current working directory
echo "The Current logged in User ID is: $UID" #Print the User ID
echo "The Script file name is: $0" #Print the file name

# 6.Wildcards
# Wildcards are special characters used to perform pattern matching when working with files.

ls *.txt # List all the files who have .txt extension

Let’s breakdown the script:

  1. Comments:

    Comments begin with #. They are usefull to undarstand code or any script.

    It is not executable by shell.

     # This is a comment.
    
  2. Echo:

    Echo print the text to terminal. You can print any text you want.

     echo "Hello, Welcome to Shell Scripting Challenge"
    
  3. Variables:

    Variables are used to store data.

     name="Imran"
     education="B.Tech"
    
  4. Using Variables:

    Performing arithmatic operations by using variables

     num1=10 #First Number
     num2=20 #Second Number
    
     i=`expr $num1 + $num2` #Use the keyword 'expr' to perform arithmatic operations
    

    Output:

     echo "The sum of $num1 & $num2 is: $i"
    
  5. Built-in Variables:

    Bash provides several built-in variables that hold useful information.

    • $0 holds the name of the script.

    • $UID gives the current logged in User ID.

    • $PWD holds the current working directory.

    echo "The Current Working Directory is: $PWD" #Print the current working directory
    echo "The Current logged in User ID is: $UID" #Print the User ID
    echo "The Script file name is: $0" #Print the file name
  1. Wildcards:

    Wildcards are special characters used to perform pattern matching when working with files.

     ls *.txt
    

    This command lists all files that match the pattern *.txt (any file ending with .txt).

Happy Learning:)

More from this blog

L

Learn DevOps

12 posts