Bash脚本,循环不';t刷新txt.file文件
我想编写一个bash脚本,它将在登录用户的主目录中创建一个文件Bash脚本,循环不';t刷新txt.file文件,bash,Bash,我想编写一个bash脚本,它将在登录用户的主目录中创建一个文件time.txt,将时间放入其中,并每n秒更新一次,直到脚本停止 到目前为止,我已经: #! /bin/bash do echo "Current time" > time.txt chmod +rwx "time.txt" time=$(date + "%T") echo "$time" >> time.txt sleep 2; watch -n 1 head -n2 time.txt. done 脚本
time.txt
,将时间放入其中,并每n秒更新一次,直到脚本停止
到目前为止,我已经:
#! /bin/bash
do
echo "Current time" > time.txt
chmod +rwx "time.txt"
time=$(date + "%T")
echo "$time" >> time.txt
sleep 2;
watch -n 1 head -n2 time.txt.
done
脚本没有刷新。它给出了正确的日期,但只有一次。我做错了什么?脚本在语法上无效。没有
while
或使循环与do
-done
块一起运行。添加一个循环将
while true; do
date +%T >> time.txt
sleep 2
done
您将希望摆脱循环中的watch
调用<代码>监视
本身就是一个永不中断的循环,将阻止连续迭代的运行。脚本在语法上无效。没有while
或使循环与do
-done
块一起运行。添加一个循环将
while true; do
date +%T >> time.txt
sleep 2
done
您将希望摆脱循环中的watch
调用<代码>监视本身就是一个永不停止的循环,它将阻止连续迭代的运行。您应该执行以下操作:
while :; do
# I'm assuming you want to see the date displayed on the terminal too.
echo -e "Current time.\n$(date +%T)" | tee time.txt | sed '/Current time/d'
sleep 2
done
:
是true
命令的缩写。您应该执行以下操作:
while :; do
# I'm assuming you want to see the date displayed on the terminal too.
echo -e "Current time.\n$(date +%T)" | tee time.txt | sed '/Current time/d'
sleep 2
done
:
是true
命令的简写。或watch
允许您执行以下操作。或者它可能根本不存在。:)我认为值得一提的是,因为它不是bash的一部分,所以它不是可移植的。或者,watch
允许您这样做。或者它可能根本不存在。:)我认为值得一提的是,因为它不是bash的一部分,所以它不是可移植的。