Unlocking the Power of Shell Scripting: A Beginner's Guide
by Rackspace Technology Staff
What is shell script?
Shell script is basically a computer program which divided into two parts:
- Shell is an Interpreter which converts low level & high level language in to machine language for Unix based Operating systems .
- Script stands for Set of Commands.
What Types of Shells are Supported Shell in Linux?
Below (snapshot) are the few examples of shells supported by Linux 6. It may vary from version to version.
How to write as a shell script?
#!/bin/bash –
This is known as header or shebang of a script.• #!
- Is a symbol which is called the interpreter.
• #Commented line – Only readable format shows what script is executing /bin/bash is the location of shell & shell we are using is bash .
For example.
Here we have created a script in bash shell & executed using sh.
What are variables in shell scripting?
Variables store any values which can be called during the execution of a script.
What are the Types of variables? There are two types of variables.
System defined Variable (SDV) - These are predefined & maintained by Linux only. SDV will always show in Capital letters.
User defined Variable (UDV) – These can be created & maintained by user. UDV should always define in Small letters
What are special variables?
• $0 - Check current script name
• $n - Sequence of arguments
• $# - To check count (number of arguments) in script.
• $* - It stores complete set of positional parameter in a single string
• $@ - Quoted string treated as separate arguments.
• $? - exit status of command
• $$ - Check PID of current shell.
• $! - check PID of last background Job
Below is an example.
Output:
Arithmetic Operators
These operators are used for arithmetic calculations .
Below is an example:
Output:
Functions and Arguments
A function is a group of commands which can be used as a single handle. To execute this set of commands, call the function by the name provided.
Below is an example of single function. We have created a shell script using a single function & called the function in script at very last.
Output
Below is an example of two functions.
Below we have created shell script calling two different functions. Output of script depends upon the calling sequence of functions.
Output:
Conclusion
Shell scripting can be used to automate manual tasks. It is very useful for most repetitive tasks which are time consuming to execute or schedule using one line at a time
Recent Posts
Escalando el acantilado de la ingeniería de plataformas: Un viaje hacia la innovación y la eficiencia
Julio 11th, 2024
Cómo funcionan las estrategias de fragmentación: Párrafo, oración y técnicas inteligentes
Julio 4th, 2024
Arquitectura basada en células en AWS
Mayo 6th, 2024
Una guía esencial para liberar el poder de la AI generativa
Julio 6th, 2023