Thursday, September 16, 2021

Why you should do shell scripting and what is The Shebang: ?


Why you should do shell scripting :

Whether you have you perform the same task, again and again, every day and it is boring, right?  “For example, if there is a slight chance that you will have to perform the same set of commands again, to make it easier you should try to create a shell script then and there you have to do it again.  
When I need to do that task again, I could execute that script.  If maintenance needs to be performed on a system at 3:00 in the morning, we can write a script that does the required work and schedule a job to run it.”



SHELL SCRIPTING, in brief:- 


  • A script is a command-line program that contains a series of commands. 
  • The commands contained in the script are executed by an interpreter. 
  • In the case of shell scripts, the shell acts as the interpreter and executes the commands listed in the script one after the other.
  • Anything you can execute at the command line, you can put into a shell script.  
  • Shell scripts are great at automating tasks.  
  • If you find yourself running a series of commands to accomplish a given task and will need to perform that task again in the future, you can—and probably should—create a shell script for that task.

A simple shell script:-


The name of this script is the script.sh  

#!/bin/bash
echo "Hello, this is my first script and it's awesome!"

Before you try to execute the script, make sure that it is executable

$ chmod 755 script1.sh
$ chmod  +x script1.sh

Here is what happens when you execute the script.
$ ./script.sh

The Shebang:

You'll notice that the first line of the script starts with #! followed by the path to the bash shell program, /bin/bash. 

The number sign is very similar to the sharp sign used in music notations - “Also, some people refer to the exclamation mark as a "bang."  So, #! can be spoken as "sharp bang."  The term "Shebang" is an inexact contraction of "sharp bang."
When a script's first line starts with a shebang, what follows is used as the interpreter for the commands listed in the script.  Here are three examples of shell scripts, each using a different shell program as the interpreter.”

- Excerpt From Cannon, Jason. “Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming”. Apple Books.  

No comments:

Post a Comment

Featured Post

Managing CA Certificates on Red Hat Linux 9: Understanding update-ca-trust extract

  Managing CA Certificates on RHEL9 RHEL8 OracleLinux9 OracleLinux8 In today's digital landscape, securing communications and verifying ...