Linux 倒计时关机。 (同时制作)

Linux shutdown with countdown. (made with while)

你好,我想创建一个脚本,在特定时间(例如 15 秒)后关闭 Linux,并在终端中显示倒计时(Linux 在 15 秒后关闭。)。它必须与 while 一起。

您可以使用以下内容:

#!/bin/bash
COUNTER=15
while [ 1 ] 
do
    if [ ${COUNTER} -eq 0 ]
    then
        break
    fi
    echo "Linux is shutting down after ${COUNTER}s."
    sleep 1
    COUNTER=$( echo "${COUNTER}-1" | bc )
done
sudo poweroff

如果您真的想在屏幕上显示计数器,可以这样做:

#!/bin/bash

seconds=15
echo "Linux is shutting down after ${seconds}s"          
date_1=$((`date +%s` + $seconds))

while [ "$date_1" -ge `date +%s` ]; do
echo -ne "$(date -u --date @$(($date_1 - `date +%s` )) +%H:%M:%S)\r"
     done

echo -e "Do you really want to shutdown the system? Press [Y/y] to shutdown or [n/N] to cancel/exit:" 
read var
if [[ "$var" == "y" ]] || [[ "$var" == "Y" ]]
then
sudo poweroff
elif [[ "$var" == "n" ]] || [[ "$var" == "N" ]]
then
exit 1
fi

基本上,它会接受用户 [Y/y] 关闭和 [n/N] 退出。