DevOps & Linux Administration
Shell Scripting Basics
Module V: Shell Scripting Basics. This module introduces shell scripting in Linux, including script creation, variables, user input, conditional statements, loops, and automation basics. Shell scripting is an essential skill in Linux administration, DevOps workflows, and server automation.
What is Shell Scripting?
A shell script is a file containing a sequence of Linux commands that are executed automatically by the shell. Instead of typing commands manually one by one, users can automate repetitive tasks using scripts.
Shell scripting is widely used for:
- Task automation
- System administration
- Server management
- Software deployment
- Backup and monitoring systems
Shell Script Workflow
Creating a Shell Script
Shell scripts are usually saved with the .sh extension. The first line of a script generally contains a special statement called the shebang.
Example Script
#!/bin/bash echo "Hello Linux" echo "Welcome to Shell Scripting"
| Component | Purpose |
|---|---|
| #!/bin/bash | Specifies Bash shell interpreter |
| echo | Displays output on terminal |
Executing a Shell Script
Before running a shell script, execute permission must be granted using the chmod command.
Execution Commands
$ chmod +x script.sh $ ./script.sh
chmod +x
Gives execute permission to the script file.
./script.sh
Executes the script in the current directory.
Variables in Shell Scripting
Variables are used to store data values in shell scripts. Values can later be accessed and reused whenever required.
Variable Example
#!/bin/bash name="Tanmay" echo "Hello $name"
- Variables are assigned without spaces
$is used to access variable values- Variables improve code reusability
Taking User Input
Shell scripts can accept input from users using the read command.
User Input Example
#!/bin/bash echo "Enter your name" read name echo "Welcome $name"
Input Flow
Conditional Statements
Conditional statements allow scripts to make decisions based on conditions. The most commonly used conditional statement is if.
if Statement Example
#!/bin/bash num=10 if [ $num -gt 5 ] then echo "Number is greater than 5" fi
Decision Making Flow
Loops in Shell Scripting
Loops are used to repeat a set of commands multiple times automatically.
| Loop Type | Purpose |
|---|---|
| for loop | Repeats for fixed values |
| while loop | Executes while condition is true |
for Loop Example
#!/bin/bash for i in 1 2 3 4 5 do echo $i done
Automation using Shell Scripts
Shell scripting is extremely powerful for automating repetitive tasks. DevOps engineers use shell scripts for deployment, backups, monitoring, and server configuration.
Backups
Automate file and database backups
Monitoring
Monitor servers and system health
Deployment
Automate software deployment workflows
Summary
In this chapter, we learned the fundamentals of shell scripting, including script creation, execution, variables, user input, conditional statements, loops, and automation basics. Shell scripting plays a major role in Linux administration and DevOps automation.