检查屏幕是否处于活动状态,然后显示倒计时

检查屏幕是否处于活动状态,然后显示倒计时
#!/bin/bash
DATE=$(date +"%m-%d-%Y")
Backup_from="/home/mc/server/prison/"
Backup_to="/home/mc/backups/PRISON-$DATE.tar" 

echo "Starting backup of $Backup_from to $Backup_to"
screen -dmS backup tar -zcvpf $Backup_to $Backup_from
if ! screen -list | grep -q "backup"; then
    for i in {10..1};do echo -n "$i." && sleep 1; done 
fi
echo "Done" 

这是我当前的备份脚本,我希望它在屏幕处于活动状态时显示倒计时。但在屏幕处于非活动状态/移除/删除时停止。

答案1

你想倒计时吗?尽管屏幕是否处于活动状态?如果是,您可以更改ifand forwithwhile循环,如下所示:

while (screen -list backup|grep -q backup); do
    # delay
    sleep 1

    # display another dot
    echo -n .
done

这会每秒显示一个点,直到您的screen会话终止。至于倒计时,您怎么知道正确的开始时间(要计数多少秒)?


另外,如果您想要重复 3 个点的动画:

# init counter
i=0;
while (screen -list backup|grep -q backup); do
    # delay
    sleep 1

    # display dot
    echo -n "."

    # increase conter
    i=$((i+1))

    # on every 3rd loop...
    if [ $i -gt 3 ]; then
        # ...reset counter...
        i=0

        # ...and clear current line and return the cursor
        echo -en "\r\e[K"
    fi
done 

相关内容