Understanding Bash and Script Basics
After a successful login, Bash starts and awaits your commands. Every command you type is interpreted and executed. Instead of entering commands one by one, you can place them in a script file. A script is simply a file with multiple commands executed in order from top to bottom. For example, running the following command interactively prints the current date and time:Creating Your First Script
-
Create a new file called “script.sh”. (The
.shextension is optional but helpful for identification.) -
Open “script.sh” in your favorite text editor, and add the following content. The first line must include the shebang:
This tells the system to use
/bin/bashas the interpreter. Comments in the script, indicated by a hash sign (#), describe the functionality but are not executed. -
Append a command to log the current date and time to a file:
/proc/version (which shows the Linux kernel details) to the same log file. The complete content of the script now looks like this:
Setting Permissions and Running Your Script
To create and edit the script using the command line:/tmp/script.log:
Enhancing Your Script with Bash Built-ins
Bash built-ins can add powerful logic to your scripts. To view available built-ins, type:if and test. In the next example, we archive the contents of the /etc/dnf directory and later use conditional logic to manage backups.
Archiving a Directory
Create a script called “archive-dnf.sh”. Use your favorite editor:tar to archive the contents:
Improving the Backup with Conditional Logic
Imagine a scenario where files in/etc/dnf are accidentally deleted. Running the backup script again would overwrite the existing (and potentially good) backup. To prevent this, modify your script to check if an archive exists. If it does, rename it to /tmp/archive.tar.gz.OLD before creating a new archive.
Create a new script named “archive-dnf-2.sh”:
The
if test -f construct checks whether the archive file exists as a regular file. If it does, the old archive is renamed before creating a new one./tmp directory to confirm that both the new archive and its backup exist:
Using Exit Status in Conditional Statements
Each command returns an exit status code after execution, where0 indicates success and any non-zero value signals an error. For example, grep returns 0 if it finds a match, and 1 if it does not.
Consider the following script that checks if the file /etc/default/grub contains the number 5:
-q flag makes grep run quietly. Save this script as “check-grub-timeout.sh”, make it executable, and run it:
Additional Scripting Concepts
These examples illustrate basic Bash scripting techniques using built-in commands. As you progress, you can integrate more advanced features such as loops (for, while), functions, and variable handling to create more robust scripts.
For a quick scripting refresher, consider reviewing the contents of the /etc/cron.d/hourly/0anacron file. This file serves as a practical cheat sheet covering essential scripting conventions, including the shebang and conditional syntax:
