Shell Scripting Part 2

Today let's understand the File Naming Convention in Shell Script.

  1. Don't use the system variable name as a variable name

  2. a variable name can be declared up to 255 characters

  3. a variable name can contain _ (underscore character).

Example:

  1. num_one=20 - This is correct.

  2. SHELL=20 - This is also correct but not recommended approach as the SHELL is a system variable name and we are trying to override the value of SHELL.

Comments:

What is this comment and why do we have to use it?

Basically, all the programming language has given the option to use the comment to increase/improve the readability of the code.

In Shell Scripting basically, we define the comments in two types:

  1. Single Comment - declared with #

  2. Multi-Line Comment - Defined with << Comment >>

Variables:

Hey what is variable now, this is a new term for me.

Nope, this is not a new term, most of the programmers who code daily understand the value of variables but I'm, not a programmer I don't know can you explain me?

Sure! The variable is like holding the data of a specific data type. To hold the data, we need to create the variable_name and initialize it with the value that you want to hold.

Ex: int a=20 - In this example, int is the data type and a is the variable name and 20 is the value that will hold.

In Shell Script we don't have anything like a data type. we can just create or follow below syntax:

variable_name=value; // we shouldn't be giving the space like other programming language.

In Shell Script there are two types:

  1. System-Defined Variables - These are defined by the system when the OS is built and released.

  2. User-Defined Variables - These are defined by the user to perform some operation in the Shell Script. The best practice here would be to give small letters while defining the variables.

How to see System Defined variables or Shell Variables?

Basically, we use the $env or printenv to display all the system variables.

Command Line Arguments - It will take input from the user while executing the shell script. Each argument is a separate space.

Let's say that you writing the shell script to take backups every day. You can write the shell script in hard code or take the input from the file.

If you do the hard code then each and every time you will have to change the file name which is not a good idea rather you write the same shell script which takes the input from the user and process the backup request here by doing this we can use the same script to backup anything in server side.

How does the command line argument are stored internally?

Each command line is stored with its corresponding value. What are those?

Reference Link:

What is $? - Previous command execution status. If the previous command is executed successfully it will display the output as Zero (0) or else 127 commands not found or so on.

Assignment -1 :

Let's say that I want to write a script that accepts the command line argument as a two and executes the rest of the command. If the user failed to provide the arguments i.e 2 it should give the custom message as output.

Please follow me for more information about the DevOps Topic and tools with detailed examples and usage.

If you have any feedback, you can send it to .