上QQ阅读APP看书,第一时间看更新
Producing delays in a script
The sleep command will delay a script's execution period of time given in seconds. The following script counts from 0 to 40 seconds using tput and sleep:
#!/bin/bash #Filename: sleep.sh echo Count: tput sc # Loop for 40 seconds for count in `seq 0 40` do tput rc tput ed echo -n $count sleep 1 done
In the preceding example, a variable steps through the list of numbers generated by the seq command. We use tput sc to store the cursor position. On every loop execution, we write the new count in the terminal by restoring the cursor position using tput rc, and then clearing to the end of the line with tputs ed. After the line is cleared, the script echoes the new value. The sleep command causes the script to delay for 1 second between each iteration of the loop.