倒數計時腳本不起作用

倒數計時腳本不起作用

我正在嘗試創建一個腳本,該腳本將從我給出的數字倒數到 0。下面是我的腳本,基本上沒有任何反應,沒有錯誤訊息,我只是得到標準命令列提示符。

#!/bin/bash
#countdown
#counts down to 0 from whatever number you give it
#displaying a number each second

NUM=${1:-0}
if [ $NUM -gt 0 ]
then
   while [ $NUM -gt 0 ]
   do
      if [ -f /usr/bin/banner ]
      then
         /usr/bin/banner "$NUM"
      else
         echo $NUM
      fi
         NUM=$(($NUM-1))
         sleep 2
   done
fi

答案1

#!/bin/bash

printf "Type an integer number: " && read NUM
if [ $NUM -gt 0 ]
then
   while [ $NUM -ge 0 ]
   do
      if [ -f /usr/bin/banner ]
      then
         /usr/bin/banner "$NUM"
      else
         echo $NUM
      fi
         NUM=$(($NUM-1))
         sleep 2
   done
fi

輸出:

:~$ ./countdown.sh 
Type an integer number: 10
10
9
8
7
6
5
4
3
2
1
0

解釋:
⠀1.第3行提示使用者輸入一個整數並將其讀入變數NUM。
⠀2.將-gt第 6 行改為-ge,使其倒數為零。
⠀3.如果安裝了 sysvbanner,則輸出將以橫幅格式顯示;如果未安裝,則以文字顯示。

答案2

改進並註解的程式碼:

#!/bin/bash

num=${1:-undefined}                                   # If $1 (the first argument passed to the script) is set, then num=$1, else num=undefined.
cmd=$(which {banner,echo} | head -1 | xargs basename) # If banner is installed, then cmd=baner, else cmd=echo.

until [[ "$num" =~ ^[0-9]+$ ]]; do                    # Until $num become a valid number (loop will not be executed if $1 is set):
    read -p "Type a number: " num                         # Ask the user for a valid number.
done                                                  # End of the until loop.

for ((num;num>=0;num--)); do                          # Loop using $num as variable; while $num is greater or equal than zero; num=$num-1.
   $cmd $num                                              # Runs $cmd (banner or echo) passing $num as argument.
   sleep 1                                                # Stop the program execution for one second.
done                                                  # End of the for loop.

上面的程式碼將在倒數計時中包含零,如果您想在倒數計時達到時停止1,那麼您只需要進行一些更改:

  1. 在第 6 行中,^[0-9]+$改為^[1-9]+[0-9]*$

    until [[ "$num" =~ ^[1-9]+[0-9]*$ ]]; do              # Until $num become a valid number (loop will not be executed if $1 is set):
    
  2. 在第 10 行中,刪除=標誌,使其看起來像這樣(我還更新了註釋):

    for ((num;num>0;num--)); do                           # Loop using $num as variable; while $num is strictly greater than zero; num=$num-1.
    

您原來的程式無法運行,因為:

  • 您沒有將數字作為參數傳遞給程式。
  • 呼叫此命令的範例是./countdown 5where5是數字。
  • 如果你想處理這個問題,你可以else在你的程式碼中加入一個(查看最後五行):

    #!/bin/bash
    #countdown
    #counts down to 0 from whatever number you give it
    #displaying a number each second
    
    NUM=${1:-0}
    if [ $NUM -gt 0 ]
    then
       while [ $NUM -gt 0 ]
       do
          if [ -f /usr/bin/banner ]
          then
             /usr/bin/banner "$NUM"
          else
             echo $NUM
          fi
             NUM=$(($NUM-1))
             sleep 2
       done
    else
        echo "Error: number not specified."
        echo "Usage: $0 <number>"
        exit 1
    fi
    

NUM=${1:-0} 方法:

${PARAMETER:-WORD}

如果參數PARAMETER未設定(從未定義)或 null(空),則該參數將擴展為WORD,否則它將擴展為 的值PARAMETER,就好像它只是 一樣${PARAMETER}

echo "Your home directory is: ${HOME:-/home/$USER}."
echo "${HOME:-/home/$USER} will be used to store your personal data."

如果HOME未設定或為空,則每次您想要列印有用的內容時,您都需要輸入該參數語法。

來源: http://wiki.bash-hackers.org/syntax/pe#use_a_default_value

在你的情況下,這意味著,如果你將一個參數傳遞給腳本,NUM將等於該參數,否則,NUM將等於0

答案3

NUM=${1:-0}行表示如果將參數傳遞給腳本,則將變數NUM設為;如果根本沒有傳遞參數,則將變數設為。這就解釋了為什麼你根本沒有輸出;如果執行腳本時未向其傳遞參數,則閾值始終設定為,例如:$100

bash <script_name>

*<script_name> =bash腳本的名稱;

或者:

./<script_name>

*<script_name> =bash腳本的名稱;

因此,您真正需要做的就是在執行時將閾值數字傳遞給腳本,即:

bash <script_name> <threshold_number>

*<script_name> =bash腳本的名稱; <數字> = 閾值數字

或者:

./<script_name> <threshold_number>

*<script_name> =bash腳本的名稱; <數字> = 閾值數字

相關內容