Linux Classroom Series – 13/Aug/2020

Tools we will be using to write bash scripts

  • Visual Studio Code
  • Git Bash (Windows) / Terminal (Other Systems)
  • RedHat Family & Ubuntu
  • How to install Refer Here
  • Creating Virtual Machines Refer Here for AWS Azure

Hello World

  • Lets create a new file called as helloworld.sh and inside this
#!/bin/bash

echo "Hello World!"
  • Now lets execute the script using bash helloworld.sh
  • Generally all the shell scripts will have extensions of .sh
  • What is #!/bin/bash?
    • It is called the shebang
    • It tells the linux system which binary to use to execute the script
    • For example if you were writing python code the shebang might be #!/usr/bin/python3
    • Since we are executing the shell scripts on bash prompt our shebang is #!/bin/bash
  • To execute the program we have used bash command. If i have a shebang which already tells where the executable is still why do i need bash?
  • Remember file permissions in linux (RWX) to execute scripts we need execute permissions, so lets check whats the permission which we have
  • Now lets add execute permissions and run the script

Readability

  • When writing scripts, aim to make sure the code is as readable as possible.

  • How to make scripts readable?

    • Comments
    • Verbosity
  • Comments:

    • Comments is nothing more than bit of text explaining what you are doing.
    • In shell the character which we use to denote comments is #
    • So lets add comments
    • Approach 1:
    #!/bin/bash
    
    # Print the text to the terminal
    echo "Hello World!"
    
    
    • Approach 2:
    #!/bin/bash
    
    echo "Hello World!" # Print the text to the terminal
    
    • Its often a good practice to have script header with fields like
      • Author
      • Version
      • Date
      • Description
      • Usage
    • Script headers and Comments make your scripts more professional
  • Verbosity:

    • Verbosity is the balancing act between not too much but also not less explanation.
    • Lets discuss three types of verbosity
      • Verbosity in Comments:
        • Ensure your comments are punctual and smaller.
      • Verbosity in Commands:
        • Try to use long variant in commands as shown in the below example with ls -R & ls --recursive. Both give same results but long variant is readable
      • Verbosity in Command Outputs:
        • Always try to build scripts which take two argument -v/–verbose and -q/–quiet. When user runs your script with verbose parameter give detailed information about what is happening & in other case don’t display anything or display only absolutely required stuff to the terminal.
  • Shell script is a sequence of linux commands that are to be executed in an order.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%