Home > database >  How to understand syntax used in "while sleep 1000; do :; done" for dash?
How to understand syntax used in "while sleep 1000; do :; done" for dash?

Time:10-19

I see this line of script from this tutorial on VS Code doc.

while sleep 1000; do :; done

I know the functionality of this line is to prevent process exiting. But I don't understand the syntax. Could you please help to explain the script? Do you have advices on learning dash syntax?

CodePudding user response:

The : is a shell builtin, that stands for true. The true command does nothing and succeeds (by returning 0).

The while command follows the syntax:

while command1 ; do command2 ; done

The command1 is executed, and when finished, the shell checks if the command has succeed or failed. In the first case, the command2 is executed, then the command1 is fired again, else the loop is stopped.

In your example, sleep 1000 waits for 1000 seconds, succeeds, then true is called, then the loop is called again and again (sleep will never failed, this is an infinite loop).

This kind of one-liner might in fact be of interest to keep a script alive, while running other things like co-routines or signal traps; it is very economical since during the sleep, the process is stopped: here every 1000 seconds, the process is resumed and stopped right again.

CodePudding user response:

Here are some quotes explaining your question from https://www.gnu.org/software/bash/manual/bash.html#Shell-Syntax:

The syntax of the while command is:

while test-commands; do consequent-commands; done

Execute consequent-commands as long as test-commands has an exit status of zero. The return status is the exit status of the last command executed in consequent-commands, or zero if none was executed.


; - You can use semicolons to separate statements. Commands separated by a semicolon are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

: - If the colon is included in a statement, the operator tests for both parameters' existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

CodePudding user response:

The : is the shell's null command. It does nothing other than evaluating its arguments and redirections. It then returns true. This is used for two purposes:

  1. When you need a command for syntactic reasons, but don't want to execute anything.
  2. When you want to perform certain parameter expansions.
  3. Truncate a file.

A use case for 1. is

while :; do
    someCommand
done

This runs someCommand repeatedly until the end of time.

Another is

while someCommand; do
    :
done

which runs someCommand until it fails.

A use case for 2. is setting default values for variables, as in

: {EDITOR:=vi}

This assigns EDITOR=vi unless it is already set or empty.

A use case for 3. is

: >file

which truncates file. Many shells allow the non-POSIX variation >file but portable scripts should not rely on it.

  • Related