Practical Shell Scripting Scenarios
In this section, we will explore some practical shell scripting scenarios that demonstrate the use of file existence checks and other shell test operators. These examples will help you understand how to apply the concepts learned in the previous sections to real-world problems.
Backup Script
Let's create a simple backup script that checks if a file exists before backing it up:
#!/bin/bash
## Specify the file to be backed up
file_to_backup="/path/to/important_file.txt"
## Specify the backup directory
backup_dir="/path/to/backup"
## Check if the file exists
if [ -f "$file_to_backup" ]; then
## Create the backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
mkdir "$backup_dir"
fi
## Perform the backup
cp "$file_to_backup" "$backup_dir/important_file_$(date +%Y%m%d).txt"
echo "File backed up successfully."
else
echo "File does not exist. No backup performed."
fi
In this script, we first specify the file to be backed up and the backup directory. We then use the -f
option to check if the file exists. If the file exists, we create the backup directory (if it doesn't already exist) and copy the file to the backup directory with a timestamp in the filename. If the file doesn't exist, we simply print a message indicating that no backup was performed.
Error Handling
It's important to handle errors gracefully in your shell scripts. Here's an example of how to check the exit status of a command and take appropriate action:
#!/bin/bash
## Attempt to create a directory
dir_to_create="/path/to/new_directory"
if [ ! -d "$dir_to_create" ]; then
mkdir "$dir_to_create"
if [ $? -eq 0 ]; then
echo "Directory created successfully."
else
echo "Error occurred while creating the directory."
fi
else
echo "Directory already exists."
fi
In this script, we first check if the directory we want to create already exists. If it doesn't, we attempt to create it using the mkdir
command. We then check the exit status of the mkdir
command using the $?
variable. If the exit status is 0 (indicating success), we print a success message. If the exit status is non-zero (indicating an error), we print an error message.
Automation Example
Shell scripts can be used to automate various tasks. Here's an example of a script that automatically checks for updates and installs them on an Ubuntu system:
#!/bin/bash
## Update the package lists
sudo apt-get update
## Check for available upgrades
upgrades=$(sudo apt-get -s upgrade | grep -c "^Inst")
if [ $upgrades -gt 0 ]; then
## Install the upgrades
sudo apt-get upgrade -y
echo "Upgrades installed successfully."
else
echo "No upgrades available."
fi
In this script, we first update the package lists using apt-get update
. We then check for available upgrades using the -s
(simulate) option of apt-get upgrade
and counting the number of lines that start with "Inst" (indicating an available upgrade). If there are any upgrades, we install them using apt-get upgrade -y
(the -y
option automatically answers "yes" to any prompts). If there are no upgrades, we simply print a message indicating that.
These examples demonstrate how you can use shell scripting and file existence checks to automate various tasks, handle errors, and create more robust and reliable scripts. By understanding these concepts, you can start building your own shell scripts to streamline your workflow and improve your productivity.