# 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
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679638635892/c1b2fafd-dde4-46e0-b48f-aa4e675aa1db.jpeg align="center")
    

### **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
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679640669600/5c7183db-2ea9-4ef2-9c44-aa0174c5291a.png align="center")
    
2. Save and exit the vi editor
    
3. Give execute permission to test.sh file
    
    `chmod u+x test.sh`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679640540920/38bf4136-53dc-4bd7-942a-861515d576e2.png align="center")
    
4. Run the .sh file now
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679640736091/4244a975-95bc-44f8-9b22-48dc8a219fed.png align="center")
    

### **What is <mark>#!/bin/bash</mark>??**

* This is called "<mark>The Shebang (#!)</mark>"
    
* 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.
    

**<mark>Example1 - Read input Statement</mark>**

* 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.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679642149848/8b491595-5e09-4348-ab8c-e8a807753535.png align="center")

* Give execute permission to the shell script & run the script
    
    `chmod u+x read.sh`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679642451012/aa98d9bb-0607-4f57-bc0c-c3d83d4e08dd.png align="center")
    

**<mark>Example2 - Compare two Numbers</mark>**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679643866750/0154360e-abe4-4f59-b870-161cdd574894.png align="center")

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679643902095/f45cde45-c744-4c9d-87eb-047017119db3.png align="center")

---

Happy Learning :)

Thank you for the Reading

Hope you find this article useful.

[https://www.linkedin.com/in/vishal-ranmale-907307114/](https://www.linkedin.com/in/vishal-ranmale-907307114/)

#90DaysOfDevops
