如何用名称中的时间戳替换备份文件而不在Linux Bash中产生重复项(Shell脚本)

发布于 2025-01-30 01:19:46 字数 1203 浏览 2 评论 0原文

#!/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都会复制和重命名文件。 我尝试了管道cpmv等等,但是以某种方式我最终会产生许多重复项,因为由于时间戳,文件名是不同的,或者当我尝试删除时根本没有。文件夹的内容。 我所需要的只是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果我完全理解您的问题,那么只需

rm -f /home/of/BACKUP/weather_*.txt
cp /home/of/weather.txt /home/of/BACKUP/weather_"$time".txt

cp让您重命名要复制的文件;单独cp然后mv是没有意义的。

为了方便起见,您可能需要cd/home/of,因此您不必拼写完整的路径,也不必将它们放入变量中。

dir=/home/of
rm -f "$dir"/BACKUP/weather_*.txt
cp "$dir"/weather.txt "$dir"/BACKUP/weather_"$time".txt

如果您的用户 的用户 >为了能够从任何地方手动运行脚本,这是无法保证的)。

显然,确保通配符与您实际要保留的任何文件都不匹配。

顺便说一句,您可以稍微简化Tee命令。如果这只能更新文件而不打印到终端,您甚至可以随身携带

curl \
     -H -x "Accept-Language: ${language}" \
     -x wttr.in/"${1:-${location}}" |
head -n 7 |
tee /home/of/weather.txt \
    >>/home/of/weather.log

Tee到备份文件,因为无论如何您都立即将其删除。您可以首先清空备份目录,但是如果curl失败,则不会有备份。

如果您也想继续打印到终端,则可能在cron作业中以重定向为/dev/null将脚本运行未读取输出的副本。

If I understand your question at all, then simply

rm -f /home/of/BACKUP/weather_*.txt
cp /home/of/weather.txt /home/of/BACKUP/weather_"$time".txt

cp lets you rename the file you are copying to; it doesn't make sense to separately cp and then mv.

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.

dir=/home/of
rm -f "$dir"/BACKUP/weather_*.txt
cp "$dir"/weather.txt "$dir"/BACKUP/weather_"$time".txt

If you are running out of the cron of the user named of 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 with

curl \
     -H -x "Accept-Language: ${language}" \
     -x wttr.in/"${1:-${location}}" |
head -n 7 |
tee /home/of/weather.txt \
    >>/home/of/weather.log

I 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 the curl fails.

If you want to keep printing to the terminal, too, probably run the script with redirection to /dev/null in the cron job to avoid having your email inbox fill up with unread copies of the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文