Bash脚本-跟踪脚本运行时的脚本
我想创建一个bash脚本,可以使用start、stop和status启动,并执行以下操作: start参数将当前时间保存在文件中(从1970年1月1日算起的秒数) status参数显示自脚本启动以来的时间量 stop参数停止脚本并显示自脚本启动以来的时间量 这就是我所拥有的,但是Bash脚本-跟踪脚本运行时的脚本,bash,shell,ubuntu,case,Bash,Shell,Ubuntu,Case,我想创建一个bash脚本,可以使用start、stop和status启动,并执行以下操作: start参数将当前时间保存在文件中(从1970年1月1日算起的秒数) status参数显示自脚本启动以来的时间量 stop参数停止脚本并显示自脚本启动以来的时间量 这就是我所拥有的,但是fi和的语法有问题但我不明白为什么,我遵循了“案例教程” 正确的bash语法需要在测试和->if[condition]之间加上分号;然后。或者干脆换一行->复制粘贴脚本,并在shellcheck.net上报告+1时修复错
fi
和的语法有问题代码>但我不明白为什么,我遵循了“案例教程”
正确的bash语法需要在测试和->if[condition]之间加上分号;然后
。或者干脆换一行->复制粘贴脚本,并在shellcheck.net上报告+1时修复错误。另外,在您的stop)
案例中,在计算$difference
时,您需要使用日期'+%s'
。
#!/bin/bash
#script to run timetrack
date=$(date +%s) #make an own variabel for seconds since 1970 1/1
case $1 in
start)
echo "Script started"
echo $date > oldTime.txt #add the current time to oldTime.txt
;;
status)
echo "Script has been running:"
if [ -e ./oldTime.txt ] then #check to see if the file exists
difference=$(( $(date) - oldTime.txt)) #calculate the time the script
has been running
printf "%s\n" "${difference#-}" #print out the time
fi #dont know? stop script maybe?
;;
stop)
echo "Script will not stop and has been running:"
if [ -e ./oldTime.txt ] then
difference=$((date) - oldTime.txt))
printf "%s\n" "${difference#-}"
rm oldTime.txt #remove the file so when i start the script again it
starts from 0
fi
exit 1 #stops script?
;;