Linux Execution Delaying

LinuxLinuxBeginner
Practice Now

Introduction

In the cloaked corners of a bustling digital metropolis, a mystical night market emerges with the setting sun, humming softly with the glow of ethereal code and shrouded networks. Here, a hidden collector of arcane artifacts, who goes by the pseudonym "The Curator," navigates through the alleys, seeking new additions that rumor claims can bestow the power to manipulate time itself within the realms of Linux.

Your task, should you choose to accept it, is to assist The Curator by mastering the arcane command known as sleep, a spell that can delay the execution of other commands in the Linux environment. By learning to wield this trick, you will not only help The Curator secure these artifacts but also enhance your own mastery of Linux command line.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") subgraph Lab Skills linux/sleep -.-> lab-271383{{"`Linux Execution Delaying`"}} end

Discovering the Temporal Command

In this step, you will harness the basic usage of sleep command to introduce a delay in a Linux environment. This simple spell allows you to pause between other commands, giving you control over the timing of your scripts.

First, navigate to the default working path in your zsh terminal:

cd ~/project

Now, let's create a new file where you will write your first script. Use the following command to create the file named temporal.sh within the ~/project directory:

touch temporal.sh

Open the file with your favorite editor, and enter the following lines of code:

#!/bin/zsh

echo "The market begins to bustle."
sleep 5
echo "A hush falls over the crowd as The Curator approaches."

The sleep 5 line will make the script wait for 5 seconds before executing the next command. Don't forget to make your script executable:

chmod +x temporal.sh

Run the script and observe the delay:

./temporal.sh

Expected output:

The market begins to bustle.
## 5 second pause
A hush falls over the crowd as The Curator approaches.

Delay Script with Variables

In this step, you'll enhance your script by introducing a variable to control the duration of delay. This practice will give you more dynamic and flexible control over script timing.

Adjust your script, temporal.sh, to include a delay variable:

#!/bin/zsh

DELAY=10
echo "The Curator whispers an incantation."
sleep $DELAY
echo "Magical energy pulses, the artifact is nearly within reach."

This script now pauses for 10 seconds, using DELAY as the duration. Try running your updated script and watch for the longer pause:

./temporal.sh

Expected output:

The Curator whispers an incantation.
## 10 second pause
Magical energy pulses, the artifact is nearly within reach.

Summary

In this lab, we traversed the cobblestone bytes of a digital night market, brewing scripts and enchantments that manipulate the flow of time in a Linux environment. Your journey began with a simple invocation of the sleep command, progressing to the artful use of variables to fine-tune the delay.

My design approach was to craft a narrative that ties a fantasy element to the technical skills being learned, which often helps in making the learning experience more engaging and memorable.

The learning here is twofold: mastery over the sleep command and how to use variables within scripts for more dynamic programming. By the end of this lab, one should feel like a true script wizard, ready to conjure pauses and delays with nothing more than a line of command in the Linux terminal.

Other Linux Tutorials you may like