View List of All Posts

2023-09-29T15:50

Exploring Bourne Shell (sh) Scripts: Examples and Explanations

The Unix shell is a versatile and powerful tool that allows users to interact with the operating system through command-line interfaces. Among the various Unix shell variants, the Bourne Shell (sh) stands out as one of the most fundamental and widely used. In this article, we will explore Bourne Shell scripts through practical examples and in-depth explanations. <br><br> Understanding Bourne Shell The Bourne Shell, often referred to as "sh," was created by Stephen Bourne in 1979 and has since become a cornerstone of Unix-based systems. It provides a command-line interface for users and system administrators to execute commands and write scripts for automating tasks. Let's start by examining some basic examples to grasp its essential concepts. <br><br> Example 1: Hello World<br> <code><br> #!/bin/sh<br> echo "Hello, World!"<br> </code><br> In this simple script, we start with a shebang line (#!/bin/sh) that tells the system to use the Bourne Shell to interpret the script. The echo command is then used to display "Hello, World!" on the screen. <br><br> Example 2: Variables and Input<br> <code><br> #!/bin/sh<br> name="John"<br> echo "Hello, $name!"<br> </code><br> In this script, we introduce variables. The name variable stores the value "John," and we use string interpolation to include it in the echo statement. <br><br> Flow Control in Bourne Shell<br> Bourne Shell scripts can also incorporate flow control structures like<br> conditional statements and loops.<br> <br><br> Example 3: Conditional Statement<br> <code><br> #!/bin/sh<br> age=25<br> <br> if [ "$age" -ge 18 ]; then<br> echo "You are an adult."<br> else<br> echo "You are a minor."<br> fi<br> </code><br> This script demonstrates a simple if statement. It checks if the variable age is greater than or equal to 18 and prints a corresponding message. <br><br> Example 4: Looping<br> <code><br> #!/bin/sh<br> for i in 1 2 3 4 5<br> do<br> echo "Iteration $i"<br> done<br> </code><br> Here, we use a for loop to iterate through a list of numbers and print each iteration's message.<br><br> Scripting for Automation<br> Bourne Shell scripts shine when it comes to automating repetitive tasks. Let's look at an example of how to backup files using a shell script. <br><br> Example 5: File Backup Script<br> <code><br> #!/bin/sh<br> backup_dir="/backup"<br> source_dir="/data"<br> <br> # Create a timestamp for the backup folder<br> timestamp=$(date +"%Y%m%d%H%M%S")<br> backup_folder="$backup_dir/backup_$timestamp"<br> <br><br> # Create the backup directory<br> mkdir -p "$backup_folder"<br> <br><br> # Copy files to the backup folder<br> cp -r "$source_dir"/* "$backup_folder"<br> <br><br> echo "Backup completed in $backup_folder"<br> </code><br> In this script, we define the source and backup directories, create a timestamp for the backup folder's name, and use mkdir and cp commands to create the backup. <br><br>