如何用名称中的时间戳替换备份文件而不在Linux Bash中产生重复项(Shell脚本)
#!/usr/bin/env bash
# usage: wttr [location], e.g. wttr Berlin, wttr New\ York
# Standard location if no parameters were passed
location=''
language=''
time=`date`
# Expand terminal display
if [ -z "$language" ]; then
language=${LANG%_*}
fi
curl \
-H -x "Accept-Language: ${language}" \
-x wttr.in/"${1:-${location}}" |
head -n 7 |
tee /home/of/weather.txt |
tee -a /home/of/weather.log |
tee /home/of/BACKUP/weather_"$time".txt
#cp weather.txt /home/of/BACKUP
#mv -f /home/of/BACKUP/weather.txt /home/of/BACKUP/weather_"$time".txt
我是Linux Bash和Shell脚本的新手,无法弄清楚以下内容。 我对上面的shell脚本有问题。 到目前为止,它运行良好(网站上的ASCII数据并将其写入Weather.txt
和.log
)。 它也在crontab中每5分钟运行一次。 现在,我需要在/of/of/of/of/of/of/
下备份weather.txt
,在/of/of/of/of/of/backup
中使用文件名<代码> weather_&lt; timestamp&gt; .txt 。 我试图删除(rm Weather*.txt
)/of/of/backup
中的旧时间戳,然后每次运行Cronjob都会复制和重命名文件。 我尝试了管道cp
和mv
等等,但是以某种方式我最终会产生许多重复项,因为由于时间戳,文件名是不同的,或者当我尝试删除时根本没有。文件夹的内容。 我所需要的只是weather.txt
的一个备份文件作为weather_ timestamp&gtxt&gtxt
,每5分钟使用实际的时间戳钻头每5分钟更新一次,我无法弄清楚它。
#!/usr/bin/env bash
# usage: wttr [location], e.g. wttr Berlin, wttr New\ York
# Standard location if no parameters were passed
location=''
language=''
time=`date`
# Expand terminal display
if [ -z "$language" ]; then
language=${LANG%_*}
fi
curl \
-H -x "Accept-Language: ${language}" \
-x wttr.in/"${1:-${location}}" |
head -n 7 |
tee /home/of/weather.txt |
tee -a /home/of/weather.log |
tee /home/of/BACKUP/weather_"$time".txt
#cp weather.txt /home/of/BACKUP
#mv -f /home/of/BACKUP/weather.txt /home/of/BACKUP/weather_"$time".txt
I'm very new to Linux Bash and Shell scripting and can't figure out the following.
I have a problem with the shell script above.
It works fine so far (curling ASCII data from website and writing it to weather.txt
and .log
).
It is also in set in crontab to run every 5 minutes.
Now I need to make a backup of weather.txt
under /home/of/
, in /home/of/BACKUP
with the filename weather_<timestamp>.txt
.
I tried to delete (rm weather*.txt
) the old timestamped files in /home/of/BACKUP
and then copy and rename the file everytime the cronjob is running.
I tried piping cp
and mv
and so on but somehow I end up with producing many duplicates as due to the timestamp the filenames are different or nothing at all when I try to delete the content of the folder first.
All I need is ONE backup file of weather.txt
as weather_<timestamp>.txt
which gets updated every 5 minutes with the actual timestamp bit I can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我完全理解您的问题,那么只需
cp
让您重命名要复制的文件;单独cp
然后mv
是没有意义的。为了方便起见,您可能需要
cd/home/of
,因此您不必拼写完整的路径,也不必将它们放入变量中。如果您的用户 的用户 >为了能够从任何地方手动运行脚本,这是无法保证的)。
显然,确保通配符与您实际要保留的任何文件都不匹配。
顺便说一句,您可以稍微简化
Tee
命令。如果这只能更新文件而不打印到终端,您甚至可以随身携带Tee
到备份文件,因为无论如何您都立即将其删除。您可以首先清空备份目录,但是如果curl
失败,则不会有备份。如果您也想继续打印到终端,则可能在
cron
作业中以重定向为/dev/null
将脚本运行未读取输出的副本。If I understand your question at all, then simply
cp
lets you rename the file you are copying to; it doesn't make sense to separatelycp
and thenmv
.For convenience, you might want to
cd /home/of
so you don't have to spell out the full paths, or put them in a variable.If you are running out of the
cron
of the user namedof
then your current working directory will be/home/of
(though if you need to be able to run the script manually from anywhere, that cannot be guaranteed).Obviously, make sure the wildcard doesn't match any files you actually want to keep.
As an aside, you can simplify the
tee
commands slightly. If this should only update the files and not print anything to the terminal, you could even go withI took out the
tee
to the backup file since you are deleting it immediately after anyway. You could alternatively empty the backup directory first, but then you will have no backups if thecurl
fails.If you want to keep printing to the terminal, too, probably run the script with redirection to
/dev/null
in thecron
job to avoid having your email inbox fill up with unread copies of the output.