A shell script is a text file containing a series of commands that can be executed by the shell. The most common shell on Linux systems is the Bash shell. Shell scripts are used to automate repetitive tasks, perform system administration, and execute complex commands.
Variables in shell scripts are used to store data. They can hold strings, numbers, or the output of commands. To declare a variable, you simply assign a value to it using the =
operator. For example:
name="John"
age=25
To access the value of a variable, you prefix it with the $
symbol:
echo "My name is $name and I am $age years old."
Control structures allow you to control the flow of execution in a shell script. The most common control structures are if-else
statements, for
loops, and while
loops.
if [ $age -ge 18 ]; then
echo "You are an adult."
else
echo "You are a minor."
fi
for i in {1..5}; do
echo "Number: $i"
done
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
To create a shell script, you can use a text editor such as nano
or vim
. Here’s an example of creating a simple script that prints “Hello, World!”:
#!/bin/bash
echo "Hello, World!"
The first line #!/bin/bash
is called a shebang, which tells the system which interpreter to use to execute the script.
Before you can execute a shell script, you need to make it executable using the chmod
command:
chmod +x script.sh
Then, you can run the script by specifying its path:
./script.sh
You can use shell scripts to automate file manipulation tasks such as copying, moving, and deleting files. Here’s an example of a script that copies all .txt
files from one directory to another:
#!/bin/bash
source_dir="/home/user/source"
destination_dir="/home/user/destination"
for file in $source_dir/*.txt; do
cp $file $destination_dir
done
Shell scripts can be used to monitor system resources such as CPU usage, memory usage, and disk space. Here’s an example of a script that checks the disk usage and sends an email if it exceeds a certain threshold:
#!/bin/bash
threshold=80
disk_usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $disk_usage -gt $threshold ]; then
echo "Disk usage is above $threshold%. Please free up some space." | mail -s "Disk Usage Alert" [email protected]
fi
You can automate backup and restoration tasks using shell scripts. Here’s an example of a script that backs up a directory to a compressed archive:
#!/bin/bash
backup_dir="/home/user/backup"
source_dir="/home/user/data"
timestamp=$(date +%Y%m%d%H%M%S)
backup_file="$backup_dir/backup_$timestamp.tar.gz"
tar -czvf $backup_file $source_dir
It’s important to handle errors in your shell scripts to prevent them from crashing unexpectedly. You can use the set -e
option at the beginning of your script to make it exit immediately if any command fails:
#!/bin/bash
set -e
# Your script commands here
Adding comments to your shell scripts can make them more understandable and maintainable. You can use the #
symbol to add comments:
#!/bin/bash
# This script copies all .txt files from one directory to another
source_dir="/home/user/source"
destination_dir="/home/user/destination"
for file in $source_dir/*.txt; do
cp $file $destination_dir
done
Before deploying a shell script in a production environment, it’s a good idea to test it thoroughly in a development or testing environment. You can use tools such as shellcheck
to check for syntax errors and potential issues in your script.
Linux command line script automation is a powerful technique that can significantly improve your productivity and efficiency. By understanding the fundamental concepts, usage methods, common practices, and best practices of shell scripting, you can automate a wide range of tasks on your Linux system. Whether you’re a beginner or an experienced user, mastering script automation will make your life easier and more productive.