Gice

Technology and General Blog

When you produce your Bash scripts, their functionality is this kind of that you have to hold out for some function inside of it to total its processing before proceeding more. The wait inside of a Bash script is launched with the “sleep” command. The sleep command in Bash makes it possible for you to hold out for as extended as you want before doing any even further processing. This operation finds a exclusive use situation whilst dealing with shoppers and servers in Linux.

Many clients can hook up to a solitary server depending on its request handling capability in a client-server design. A customer often initiates a connection request, whereas a server listens to this ask for. Having said that, at moments, a server could possibly be chaotic processing other requests. Consequently, there should really be a time length outlined for the consumer for which that shopper really should wait around right before canceling the relationship initiation ask for. This time length can be outlined although producing use of the snooze command.

This was just a easy use scenario of the rest command with the customer-server design having said that, this command can also serve other unique uses within just this model. Our motive is to find out how we can sleep in a Bash script in Ubuntu 20.04. For executing that, we have built this tutorial so that you will get the maximum benefit out of it once you abide by the examples shared in it.

How do I Rest in a Bash Script in Ubuntu 20.04?

For sleeping within a Bash script, the command that is utilised is recognised as “sleep”. For your relieve, the syntax of this command is stated below:

Here, duration refers to the quantity of seconds, minutes, several hours, or times for which you want your software to slumber. The default rest period is in seconds, which indicates that if you execute the command “sleep 2”, your system will merely rest for 2 seconds. Even so, if you want your software to sleep for minutes, hours, or times, then you will have to use the “m”, “h”, or “d” figures just after the slumber period for specifying the minutes, hrs, or times respectively.

Now, to have an understanding of the doing work of the sleep command in a superior way, you will have to browse through the following illustrations that we have specifically built for you to get your fingers on the usage of the slumber command in Bash in Ubuntu 20.04.

Instance # 1: Basic Usage of the Sleep Command in Bash:

The to start with illustration is the most basic 1 of all in which we just supposed to teach you how you can develop a Bash script that uses the snooze command. The sample Bash script is revealed below:

We have just utilised the sleep command in this script adopted by the rest period, which in our case was 2. It suggests that our script will rest for two seconds ahead of performing any further processing. Soon after sleeping for two seconds, we desired our script to print a random information on the terminal with the assist of the “echo” command.

Now, to execute this script by the Ubuntu 20.04 terminal, we will operate the subsequent command in it:

Sleep.sh is the file’s title in which our Bash script for this distinct illustration is composed.

After this command was executed, our terminal waited for 2 seconds prior to displaying the message said in our script on the terminal, as proven in the graphic down below:

You will confirm it once you make a similar Bash script and execute it on your Ubuntu 20.04 program.

Instance # 2: Applying the Sleep Command to Look at Two Unique Situations in Bash:

Now we want to choose you a minimal more with the use of the slumber command in Bash in Ubuntu 20.04. For that, you really should 1st get a look at the adhering to Bash script that we have developed:

In this Bash script, we first utilised the date command to print the latest program time in the “hours, minutes, seconds” structure. Right after that, we have made use of the slumber command for putting the script to snooze for 2 seconds. Then again, we have employed the day command to print the existing program time. Fundamentally, we needed to compare the two distinctive instances, or in other terms, we needed to look at whether our rest command has basically place our script to slumber for 2 seconds or not.

This Bash script can be executed with the identical command that we used in our initial instance. We have exhibited the output of this script in the picture shown under:

In this output, you can see the change between the two moments. The initially time was 18:26:06, right after which our Bash script slept for 2 seconds. The 2nd time was 18:26:08. Both of those periods vary accurately by 2 seconds which indicates that our slumber command has been executed accurately.

Illustration # 3: Using the Rest Command inside of a For Loop in Bash:

Finally, we will now structure an instance Bash script that will use the sleep command within the “for loop”. You can 1st take a seem at the adhering to Bash script that we have intended for this objective:

This Bash script begins with declaring an array named “numbers,” and 3 values, i.e., 1, 2, and 3 have been assigned to this array, which means that the declared array has three elements. Then, we have a variable to which we have assigned the length of this array so that our “for loop” can simply iterate as a result of this array. Then, we have our “for loop,” which will have a total of a few iterations due to the fact it iterates by way of the duration of the “numbers” array, which is 3. In just this “for loop,” we required to print the features of our “numbers” array just one by a person with 1 -the second pause ahead of printing the subsequent value. That is why we have initially made use of the “echo” command to print the array index price adopted by a random concept. Then our script will rest for a single second, following which the subsequent price will be printed.

This script will be executed in the exact way as we executed our 1st two example scripts. The output of this Bash script is demonstrated in the picture under:

You can effortlessly visualize from the output proven above that our Bash script slept precisely for a second following printing every single index value of our “numbers” array.

Summary:

This article begun with a brief description of the rest command in Bash in Ubuntu 20.04 method, followed by its standard syntax. Then, we shared three unique examples that use this command in just a Bash script with you. These examples started with a incredibly straightforward to complexity degree and rose to a somewhat tough complexity level. On the other hand, our most important goal was to display you how to use the sleep command in a Bash script on Ubuntu 20.04 method. Ideally, by likely by way of this tutorial, you will be ready to use the rest command in just your Bash scripts quite competently.

Leave a Reply

Your email address will not be published. Required fields are marked *