Shell scripting is a robust tool that can automate repetitive tasks, control system processes, and perform complex operations. An important part of mastering shell scripting is understanding how to add delays or pauses in your scripts. This can be handy, for instance, when you want to space out commands or ensure certain processes have finished before moving on. Here, we provide a step-by-step guide to adding delays in your shell scripts using the ‘sleep’ command.

Advertisement

What is a Shell Script?

Before we delve into the specifics of adding delays, let’s quickly cover what a shell script is. A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

The Basics of Adding Delays

The ‘sleep’ command is used in shell scripts to pause the execution of the next command for a specified amount of time. The basic syntax of the ‘sleep’ command is straightforward:

In this case, NUMBER specifies the length of the delay, and SUFFIX may be ‘s’ for seconds (the default), ‘m’ for minutes, ‘h’ for hours, or ‘d’ for days. For example, ‘sleep 5’ would pause for five seconds, while ‘sleep 2m’ would pause for two minutes.

Step-by-Step Guide to Adding Delays

Now that we understand the basic syntax of the ‘sleep’ command, let’s explore how to use it within a shell script.

  1. Open your text editor: Shell scripts are written in plain text, so you can use any text editor to write them. This includes vi, nano, or emacs on Unix-based systems, or programs like Notepad++ on Windows.
  2. Start your script: Every shell script begins with a shebang (#!) followed by the path to the shell. For most scripts, you’ll use /bin/bash, like so:

  3. Write your commands: After defining the shell, you can begin scripting. For instance, you might want to print text, then pause, then print more text:

  4. Save your script: Save the script with a .sh extension, such as delayScript.sh. This identifies it as a shell script.
  5. Make your script executable: Before you can run your script, you’ll need to make it executable. You can do this with the chmod command:
    chmod +x delayScript.sh 
    
  6. Run your script: Finally, you can run your script. If you’re in the same directory as the script, you use the ./ prefix:
    ./delayScript.sh 
    

    The script will print “Before sleep”, pause for five seconds, then print “After sleep”.

Further Examples

You can add delays between any commands in your shell scripts. The following script, for example, would print a number, wait for a second, then print the next number:

You can also use a variable to set your sleep time:

Conclusion

The ability to add delays to shell scripts is a powerful tool. It allows for more precise control over the execution of your scripts and can be critical in a wide range of tasks. By using the ‘sleep’ command and understanding its syntax, you can elevate your scripting to new levels. Happy scripting!

Share.
Leave A Reply


Exit mobile version