Shell Script
A shell provides us with an interface to the Unix system. It gathers input and executes based on the input and provides output. Shell is an environment in which we can run our commands, programs, and shell scripts
Shells could be one of the following :
- The Bourne Shell
- The C Shell
- The Korn Shell
- The GNU Bourne-Again Shell
A shell script is a computer program designed to be run by the Unix/Linux shell.
A Bash script is a plain text file which contains a series of commands
Creating Script File and Steps to run
- Create empty file and save the file with
.sh
extension to make the file as script file. - To check the file is executable. run
ls -l
command. - drwr-r--rw the file has only read and write access.
- drwxr-xr-x the file has read, write and execution access.
- To make file to execute: run
chmod -x <file name>
command - To run script add
#!/path to shell
e.g:#!/bin/sh
as first line in the script file #! is called shebang
andecho
: is for printing the line- variables are declared as
<variable name>=<value>
IF Condition, while, for loop and switch case: (we can use single or double brace for condition)
Function in shell scripts
There are 4 type of functions.
- function with parameter
- function without parameter
- function with return type
- function without return type
Calling a function from different files
Exit Statements in shell script
- If function executes successfully it returns
0
: exit status is 0 - If function executes fails it returns
nonZero
: exit status might be 1 or any non zero value
Break and Continue Condition
- break: which breaks the execution of loop
- syntax: break or break nth
- break nth : it breaks nth closing loop
- In nested loop
break
will stops execution of inner loop
- Continue: If condition is success, which breaks the execution of loop and continues the next execution
- syntax:
continue
orcontinue nth
continue nth
: it breaks nth closing loop and continues next execution
Note:
- To get more detail please check my GitHub account for more details on syntax and live examples.
- To know more about shell command like grep, awk, cut, sed and pipe, please check the blog here
LAST REMARKS
I hope you find this blog helpful. If you enjoyed it, feel free to hit the clap button below 👏 to help others find it! and follow me on Medium. Thanks for reading. ❤❤❤❤