What
is bash scripting
Generally
bash scripts are used by Developers, System Administers, Network
Engineers, Computer Scientists and almost every one who uses
Unix/Linux systems. Bash script is a simple text file with series of
commands, usually we type on command line(commands that we've
discussed in the previous post). Although .sh extension is commonly
used with bash scripts it is not mandatory since Linux is an
extensionless system.
Contents
of a simple script
The Shebang (#!)
#!/bin/bash
First line of the above script, #! character sequence referred to as shebang and then it is the path to the interpreter that should be required to run the next lines in the script.
Executing the script
Before
we can run the script we have to grant the permission, otherwise
you'll get an error message saying “Permission denied”
Variables
Variable is a temporary storage for information.
Use appropriate names for variables so that you can easily identify those without having further description. Setting a value for a variable can be done by directly or by user input. When referring to or reading a variable we place a $ sign before the variable name.
Setting a value for a variable by user input
If statements
Loops
While loop
Using while loop we can perform some task repeatedly until given condition is true.
Here while loop runs 5 times before it terminates. Since counter starts with 1 and it is incremented by 1 inside while block, after 5 rounds the value of counter will be 6. But the condition satisfies only with the values less than ( refereed by 'lt' ) 6. So the this will print the numbers 1-5.
Until loop
Variables
Variable is a temporary storage for information.
Use appropriate names for variables so that you can easily identify those without having further description. Setting a value for a variable can be done by directly or by user input. When referring to or reading a variable we place a $ sign before the variable name.
Setting a value for a variable by user input
If statements
If statements allow
users to make decisions in bash scripts. If statements decide which part of the code has to be run based on set criteria.
Following code is a simple example to show how if-else blocks are work
Loops
Loops allow user to
do some task repeatedly until given condition is satisfied.
There are three basic loops structures in bash scripting :
- While
- Until
- For
While loop
Using while loop we can perform some task repeatedly until given condition is true.
Here while loop runs 5 times before it terminates. Since counter starts with 1 and it is incremented by 1 inside while block, after 5 rounds the value of counter will be 6. But the condition satisfies only with the values less than ( refereed by 'lt' ) 6. So the this will print the numbers 1-5.
Until loop
Until loop is fairly similar to while loop, but this works until given condition is false where as while loop runs until condition is true.
This loop will run until the counter is greater than ( indicates by '-gt' ) 6. That is when counter become 7, loop will terminate.
For loop
In for loop we can set starting and ending point to repetition. In situations where we know the two extremes we can use this loop.
This will print all the numbers in the sequence (indicates by `seq 1 10`). Here starting and ending points will be 1 and 10 respectively.
Case
when the user input is 1-4 this code will print respective month name, otherwise it will display a error message.
This loop will run until the counter is greater than ( indicates by '-gt' ) 6. That is when counter become 7, loop will terminate.
For loop
In for loop we can set starting and ending point to repetition. In situations where we know the two extremes we can use this loop.
This will print all the numbers in the sequence (indicates by `seq 1 10`). Here starting and ending points will be 1 and 10 respectively.
Case
when the user input is 1-4 this code will print respective month name, otherwise it will display a error message.








No comments:
Post a Comment