Editing
Sleep in Mandarin
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== BASH == === 方法 1:sleep === 在 {{Linux}} 和 {{Mac}} 上測試<ref>[https://bash.cyberciti.biz/guide/Perform_arithmetic_operations 執行算術運算 - Linux Shell 腳本編寫教學 - 初學者手冊]</ref><ref>[https://linuxize.com/post/how-to-use-linux-sleep-command-to-pause-a-bash-script/ Linux Sleep 命令(暫停 Bash 腳本) | Linuxize]</ref> <pre> # 列印當前日期與時間 echo $(date '+%Y-%m-%d %H:%M:%S') # 暫停 5 到 65 秒 timeout=$(($RANDOM * 60 / 32768 + 5)) sleep $timeout # 列印當前日期與時間 echo $(date '+%Y-%m-%d %H:%M:%S') </pre> 暫停幾 [https://en.wikipedia.org/wiki/Microsecond 微秒]<ref>[https://serverfault.com/questions/151109/how-do-i-get-the-current-unix-time-in-milliseconds-in-bash 如何在 Bash 中獲取當前 Unix 時間(毫秒)? - Server Fault]</ref>。在 {{Linux}} 上測試 <pre> # 列印當前 Unix 時間戳,例如 1598346362244 date +%s%N # 暫停 1 毫秒 = 0.001 秒 sleep 0.001s # 列印當前 Unix 時間戳 date +%s%N </pre> 在 {{Mac}} 上測試暫停幾微秒 <pre> # 列印當前 Unix 時間戳,例如 1598346362244 php -r 'echo microtime(TRUE) . PHP_EOL;' # 暫停 1 毫秒 = 0.001 秒 sleep 0.001s # 列印當前 Unix 時間戳 php -r 'echo microtime(TRUE) . PHP_EOL;' </pre> 暫停幾毫秒(1 秒 = 1000 毫秒)例如 1671090194.136471987<ref>[https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal macos - 自紀元以來的時間(毫秒) - 在終端 - Ask Different]</ref> <pre> # 列印當前 Unix 時間戳(毫秒),例如 1671090194.136471987 perl -MTime::HiRes=time -e 'printf "%.9f\n", time' # 暫停 1 毫秒 = 0.001 秒 sleep 0.001s # 列印當前 Unix 時間戳 perl -MTime::HiRes=time -e 'printf "%.9f\n", time' </pre> === 方法 2:ping === 在 {{Linux}} 和 {{Mac}} 上測試 ping 本地地址:{{kbd | key=<nowiki>-c <Count></nowiki>}} "在發送(並接收)數量 count 的 ECHO_RESPONSE 封包後停止。"<ref>[https://linux.die.net/man/8/ping ping(8) - Linux 手冊頁面]</ref> <pre> # 列印當前日期與時間 echo $(date '+%Y-%m-%d %H:%M:%S') # 暫停 5 ~ 65 秒 timeout=$(($RANDOM * 60 / 32768 + 5)) ping 127.0.0.1 -c $timeout > nul # 列印當前日期與時間 echo $(date '+%Y-%m-%d %H:%M:%S') </pre> === 方法 3:usleep === [https://linux.die.net/man/3/usleep usleep(3) - Linux 手冊頁面] "在微秒間隔內暫停執行"(1 秒 = 1,000,000 微秒)。在 {{Linux}} 上測試 <pre> # 列印當前日期、時間與奈秒 echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') # 暫停 0.2 ~ 1.2 秒 timeout=$(($RANDOM * 1000000 / 32768 + 200000)) echo $timeout usleep $timeout # 列印當前日期、時間與奈秒 echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') </pre> === BASH 範例:下載檔案並在 1 ~ 7 秒之間隨機暫停 === 以下腳本檢查特定檔案是否存在於指定路徑中。如果檔案不存在,則從提供的 URL 下載檔案,然後在 1 到 7 秒之間隨機暫停。如果檔案已存在,則僅列印一條消息,表示找到了檔案並跳過了下載。 <pre> if [ ! -f /path/to/paper.pdf ] then curl "https://website/to/pdf" -L -o /path/to/paper.pdf sleep $((1 + RANDOM % 7)) else echo "檔案已找到。跳過下載。" fi </pre> 相同的命令可以寫成一行: <pre> if [ ! -f /path/to/paper.pdf ]; then curl "https://website/to/pdf" -L -o /path/to/paper.pdf; sleep $((1 + RANDOM % 7)); else echo "檔案已找到。跳過下載。"; fi </pre> References * [https://www.cyberciti.biz/faq/bash-check-if-file-does-not-exist-linux-unix/ How to check if file does not exist in Bash - nixCraft]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information