Skip to main content

Command Palette

Search for a command to run...

Shell Script - Basic [Part - 1]

Updated
3 min readView as Markdown

First Script:

  • To find the path of bash on the Linux Terminal
    which bash

  • Run the below cmd to run/execute your shell script
    bash yourshellscript.sh
    Or
    ./yourshellscript.sh
    Or
    sh yourshellscript.sh

  • Make sure the script has executable permissions and
    chmod 777 <script_name.sh> Or chmod 755 <script_name.sh>

  • When you want to Run the script as Command, make sure your script path is defined in the $PATH variable so the shell searches your script/cmd and executes it.
    echo $PATH $ export PATH=$PATH:/home/<Userdirectory>

  • Variables:
    Variable names must be in lower-case with underscores to separate words”
    Variables are case-sensitive.
    Only ALPHANUMERIC OR UNDERSCORES should be used for variables when using - (hyphen) will cause errors for the Variables
    i.e (Bad Variable: product-name)

Variables are represented via $ (Dollar sign)

i.e
System variable:
echo $BASH

  • Manually define variable
    name="Devops Team"

  • Call the variables in the script
    name="Devops Team" echo "hello ${name}"

  • To comment or to skip from execution you can use #(hash) at the start of the cmd

  • User Input and arguments using "read"

    • Option: 1
      echo "Please enter your age: "
      read age
      echo "My age is ${age}"

    • Option: 2

      read -p "Please enter your age:" age
      echo "Your age is $age"

  • Halting using sleep
    sleep 2

  • Take arguments from the user when running the script using $1 (First argument)

    • Run the shell script using
      bashyour-shell-script.sh <argument>

    • The file
      echo "Sub: Hello, I am $1"

  • If else statement
    if [ "$1" = "like" ]
    then
    echo "Hello, Please $1 this video"
    else
    echo "Okay, then please Subscribe:)"
    fi

  • Shell Script that prints I will complete #90DaysOofDevOps challenge

    • Shell Script with coloring effects I will complete #90DaysOofDevOps challenge

Code:-
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Colour"; done

Reference Url:-
https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
https://devopscube.com/linux-shell-scripting-for-devops/
https://www.geeksforgeeks.org/bash-script-read-user-input/

  • Write a Shell Script to take user input, input from arguments, and print the variables.

Code:-

Script Output:-

SHELL is a program which provides the interface between the user and an operating system. When the user logs in OS starts a shell for user. Kernel controls all essential computer operations, and provides the restriction to hardware access, coordinates all executing utilities, and manages Resources between process. Using kernel only user can access utilities provided by operating system.

Popular Types of Shell:

  1. Bourne Shell (sh)

  2. C Shell (csh)

  3. KornShell (ksh)

  4. Bourne Again Shell (bash)

  5. Z Shell (zsh)

    This #! is called shebang or hashbang. The shebang plays an important role in shell scripting, specially while dealing with different types of shell.

    The shebang is the combination of the # (pound key) and ! (exclamation mark). This character combination has a special meaning when it is used in the very first line of the script. It is used to specify the interpreter with which the given script will be run by default.

    So, if the first line of a script is:

     #!/bin/bash
    

    It means the interpreter should be bash shell.

More from this blog

DevOps

16 posts

I am Ketan Gharate. I started writing articles on my DevOps and cloud journey. I am constantly willing to learn and develop, looking for new challenges, knowledge, and growth opportunities...