Skip to main content

Command Palette

Search for a command to run...

Linux Bash Shell Scripting

Some basic understanding

Updated
5 min read
Linux Bash Shell Scripting
S

Hello Team,

Working Devops Support Engineer having 8.2 years of experience in Unix, Shell Scripting, SQL. Since one year i started my journey as a DevOps support engineer where i am involved in Deployments, Infrastructure monitoring using AWS- cloud-watch, Automating simple task using shell scripts.

  1. What is a Script?

    The Shell script consists of a group of commands that have to be executed repeatedly and should be kept in a file and executed as a shell script or Shell program. The Shell reads the file and carries out the commands as they have been entered into the command line.

  2. What are Variables and Arguments in a script?

    Variables are the temporary storage location. In shell scripting, variables are used to store data/value within a script to inorder to call the value echo $variablename. Shell variables are typically defined without specifying a data type and can hold various types of information, such as strings, numbers, or even arrays. There is no data type in shell scripting. By default, every variable is treated as a single string.

    i) Here are the basics of using variables.

    name="John"

    Variable assignment: Variables are assigned values using the = (equal) operator. No spaces should be present around the equal sign.

    To call the value stored in a variable, you prepend the variable name with the $ symbol. The variable substitution operator is

    ${variable_name} or $variable_name

    echo $variable_name

    UDV(User defined Variable):-- These are the variables defined by the user(created and maintained by user) this type of variable is defined by lower letters. it's classified into three types.

    1. Local variable 2. Global Variable 3. Constant Variable

      Local variable Example:--

      x=100, y=1.5, name="Techno Soft"

      x="Techno"

      x=${x}soft#echo $x Technosoft o/p:- Technosoft

      //To add some text to an existing variable, use { }

      Constant Variable:-- "Read-only is the keyword to create a constant variable". x= 100 then readonly x

      "Export" is the command to create a Global variable. export variable name

    2. Environment variables are predefined variables that are available to all shell scripts and programs. They hold information about the system environment, user settings, and other configurations. Common

    3. environment variables include PATH, HOME, and USER. You can access them just like any other variable.

    4. set command we can see all the system-defined variables along with its values. 1. echo $HOME :-- after you login in unix by default it will place you in home directory.

      echo $MAILCHECK :-- The UNIX mail handling system doesn't inform the user that mail has arrived the job is done by shell. Mail checking interval for incoming mail. echo $MAIL:-- Absolute path for user mailbox file.

      echo $LOGNAME:-- it will show the login name details(who am i)

      echo $SHELL :-- Users login Shell. echo $TERM:- Type of terminal.

      echo $CDPATH:-- List of directories searched by cd when used with a non-absolute pathname,

    5. We can take input from the user with read command.

    6. read -p "prompt:" variable name.

  3. if else ladder, conditional statement, break, loops

  4. Operators in Shell Scripting

  5. Script to Check whether any file or directory is present or not

  6. File Conditions

  7. Positional Parameters and its usage in a shell script

  8. Relational Operators. Numeric Comparison Operators

  9. (-lt = less than; -le = less than or equal to; -gt = greator then; -ge = greator than or equal to; -eq= equal to; -ne= not equal to

  10. Except assignment Operator, every Operator should contain space before and after the Operator. assignment operator like a=b;(nospace) c=`expr $a + $b` ;c=`expr $a - $b`

  11. c=`expr $a \* $b`; c=`expr $a / $b`; c= `expr $a % $b` . expr is the command in shell script which enables to perform arithmetic operation in shell script. Instead of saving result to a varaible it instead prints the answer.

  12. #!/bin/bash (#! —> It represents the calling symbol of the interpreter and /bin/bash is the location of the shell).

  13. Conditions

    • [[ -z STRING ]] - Empty string

    • [[ -n STRING ]] - Not empty string

    • [[ STRING == STRING ]] - Equal

    • [[ STRING != STRING ]] - Not equal

    • [[ NUM -eq NUM ]] - Equal

    • [[ NUM -ne NUM ]] - Not equal

    • [[ NUM -lt NUM ]] - Less than

    • [[ NUM -le NUM ]] - Less than or equal

    • [[ NUM -gt NUM ]] - Greater than

    • [[ NUM -ge NUM ]] - Greater than or equal

    • [[ ! EXPR ]] - Not

    • [[ X && Y ]] - And

    • [[ X || Y ]] - Or

File test commands:--

  • [[ -e FILE ]] - Exists

  • [[ -r FILE ]] - Readable

  • [[ -h FILE ]] - Symbolic link

  • [[ -d FILE ]] - Directory

  • [[ -w FILE ]] - Writable file

  • [[ -s FILE ]] - File size is > 0 bytes

  • [[ -f FILE ]] - File

  • [[ -x FILE ]] - Executable file

  • [[ -c FILE ]] - If its a character special file

  1. While loop:--

    while [condition]

    do

    --(if condition is true this block will execute)

    done

    --(if false this block will execute)

    --

  2. for loop:--

    for variable in val1, val2, val3, val4,-----,valn

    do

    -- (It will execute for the values mentioned in the iteration)

    done

    --

  3. while [condition]

    do

    \====

    break

    --

    done

    \== (break statement will execute the code mentioned in ==)

  4. if (condition)

    then

    \== (it will execute the block if the condition is true)

    else

    \==(it will execute this block if condition is false)

    fi

    --

  5. while(condition)

    do

    \==

    Continue (Continue is the keyword that will start the loop again)

    --

    done

    \==

  6. untill [condition]

    do

    --

    \== [it will execute this block if the condition is false]

    done

    -- [it will execute the below block if the condition is true]

  7. basename:-- This command used with two arguments which strips off the second argument from the first argument.

  8. basename abc.txt txt [txt has been stripped off].

  9. Positional parameters:-- $1,$2, $3, $4, $5,---- $9:-- Positional parameters representing the command line arguments. $#:-- Number(count) of arguments specified in the command line. $0:-- Name of the executed command.

    $*:--It will take all the parameters as a string

    $#:-- Gives total number of arguments. It will take all the parameter values but every parameter value is enclosed with in double quotes.

    $?:-- It will hold the exit status of the last command. If the command executed successfully it holds 0 otherwise non-zero.

    $$:-- It holds user parent shell process id.

  10. $@ --> It shows all the parameters values as a list. Here the collection of arguments is treated as a separate string.

  11. $* --> It will take all the parameters values . Here the collection of arguments is treated as a single string.

More from this blog

Sidharth's Blog

10 posts