Basic Linux Shell Scripting

Basic Linux Shell Scripting

What is shell?

  • Computers understand the language of 0's and 1's called binary language.

  • In the early days of computing, instructions are provided using binary language, which is difficult for all of us, to read and write.

  • In OS there is a special program called Shell. Shell accepts your instruction or commands in English (mostly) and if it's a valid command, it is passed to the kernel.

  • Shell is a user program or its environment provided for user interaction.

  • Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file.

  • Shell is not part of the system kernel but uses the system kernel to execute programs and create files.

Shell Types:

  • To know which shells are installed on your system type the following command

What is Shell Script ?

  • A shell script is a list of commands in a computer program that is run by the Unix shell which is a command line interpreter.

  • A shell script usually has comments that describe the steps. The different operations performed by shell scripts are program execution, file manipulation, and text printing.

  • A wrapper is also a kind of shell script that creates the program environment, runs the program, etc.

When to use shell script??

  1. Repeated Tasks

    • Backups • Log monitoring • Check disk space

  2. Occasional Tasks

    • Periodic business related reports(monthly/quarterly/yearly/daily)

    • Offsite backups

    • Purging old data

  3. Complex Manual Tasks

    • Checking for database locks

    • Killing runaway processes

  4. Special Tasks

    • Storing OS information (performance stats, disk usage, etc.) into the database

    • High-frequency monitoring (several times a day or more)

Write your first shell program

  1. Create the test.sh file and below content

  2. Save and exit the vi editor

  3. Give execute permission to test.sh file

    chmod u+x test.sh

  4. Run the .sh file now

What is #!/bin/bash??

  • This is called "The Shebang (#!)"

  • This first line indicates what interpreter to use when running this script

  • The "shebang" is a special comment. Since it is a comment it will not be executed when the script is run. Instead, before the script is run, the shell calling the script will check for the #! pattern.

  • If found it will invoke the script using that interpreter. if no #! is found most shells will use the current shell to run the script.

Example1 - Read input Statement

  • The following script first asks for the user, name & last name and then waits to enter from the user via the keyboard.

  • The entered value stored in Variables defined in the shell script.

  • Give execute permission to the shell script & run the script

    chmod u+x read.sh

Example2 - Compare two Numbers

  • Take 2 numbers as input from the user & compare them.

Output:


Happy Learning :)

Thank you for the Reading

Hope you find this article useful.

https://www.linkedin.com/in/vishal-ranmale-907307114/

#90DaysOfDevops