Appearance
Day 5: Shell Scripting and Automation
Objectives
- Understand the basics of shell scripting.
- Learn to write simple scripts to automate tasks.
- Use variables, conditionals, and loops in scripts.
- Schedule tasks using
cron
.
1. Introduction to Shell Scripting
What is a Shell Script?
- A shell script is a text file containing a sequence of commands for a Unix-based operating system.
- It can automate repetitive tasks, manage system operations, and perform batch processing.
Creating and Running a Shell Script
Create a Script File
- Use a text editor like
nano
orgedit
to create a script file.bash$ nano myscript.sh
- Use a text editor like
Write a Simple Script
- Example content:bash
#!/bin/bash echo "Hello, World!"
- Example content:
Make the Script Executable
- Change the file permissions to make it executable:bash
$ chmod +x myscript.sh
- Change the file permissions to make it executable:
Run the Script
- Execute the script:bash
$ ./myscript.sh
- Execute the script:
2. Shell Scripting Basics
Using Variables
- Assign values to variables:bash
#!/bin/bash NAME="John" echo "Hello, $NAME"
Reading User Input
- Read input from the user:bash
#!/bin/bash echo "Enter your name:" read NAME echo "Hello, $NAME"
Using Conditionals
- Use
if
statements to execute commands based on conditions:bash#!/bin/bash echo "Enter a number:" read NUM if [ $NUM -gt 10 ]; then echo "The number is greater than 10" else echo "The number is 10 or less" fi
Using Loops
- Use
for
andwhile
loops to repeat commands:bash#!/bin/bash for i in 1 2 3 4 5; do echo "Looping ... number $i" done COUNT=1 while [ $COUNT -le 5 ]; do echo "Count is $COUNT" COUNT=$((COUNT + 1)) done
3. Automating Tasks with cron
Introduction to cron
cron
is a time-based job scheduler in Unix-like operating systems.- It allows users to schedule scripts or commands to run automatically at specified times and intervals.
Editing the Crontab
Open the crontab file for editing:
bash$ crontab -e
Add a new cron job using the following format:
plaintext* * * * * /path/to/script.sh
Cron Job Timing Format
- The cron timing format consists of five fields representing:
- Minute (0 - 59)
- Hour (0 - 23)
- Day of the month (1 - 31)
- Month (1 - 12)
- Day of the week (0 - 7) (Sunday is both 0 and 7)
Example Cron Jobs
Run a script every day at 2 AM:
plaintext0 2 * * * /path/to/script.sh
Run a script every Monday at 5 PM:
plaintext0 17 * * 1 /path/to/script.sh
Run a script every hour:
plaintext0 * * * * /path/to/script.sh
Practical Exercises
Exercise 1: Create and Run a Basic Shell Script
- Create a script file named
greet.sh
:bash$ nano greet.sh
- Add the following content:bash
#!/bin/bash echo "Hello, welcome to Linux shell scripting!"
- Make the script executable:bash
$ chmod +x greet.sh
- Run the script:bash
$ ./greet.sh
Exercise 2: Write a Script with Variables and User Input
- Create a script file named
userinfo.sh
:bash$ nano userinfo.sh
- Add the following content:bash
#!/bin/bash echo "Enter your name:" read NAME echo "Enter your age:" read AGE echo "Hello, $NAME. You are $AGE years old."
- Make the script executable:bash
$ chmod +x userinfo.sh
- Run the script:bash
$ ./userinfo.sh
Exercise 3: Schedule a Cron Job
- Create a script file named
backup.sh
:bash$ nano backup.sh
- Add the following content to back up a directory:bash
#!/bin/bash tar -czf /path/to/backup/backup.tar.gz /path/to/directory
- Make the script executable:bash
$ chmod +x backup.sh
- Schedule the script to run every day at midnight:bash
$ crontab -e
- Add the following line to the crontab:plaintext
0 0 * * * /path/to/backup.sh
By the end of Day 5, users will be capable of writing basic shell scripts, using variables, conditionals, and loops, and scheduling tasks with cron
. These skills are essential for automating tasks and managing a Linux system efficiently.