bash 创建带日期的文件名时,shell 脚本中出现不明确的重定向错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38936805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:03:13  来源:igfitidea点击:

Ambiguous redirect error in shell scripting when creating file name with date

bashshelldateredirectambiguous

提问by Gabor

I'm trying to make this temperature log in Raspberry Pi happen (though some of the code did not work therefore I used slightly diff. solutions):

我正在尝试在 Raspberry Pi 中生成此温度日志(尽管某些代码不起作用,因此我使用了稍微不同的解决方案):

https://www.raspberrypi.org/learning/temperature-log/worksheet/

https://www.raspberrypi.org/learning/temperature-log/worksheet/

I'm stuck with writing the results in a file. I want to create a file that's name contains the actual date, e.g. temperature_log_2016_08_13.txt

我坚持将结果写入文件。我想创建一个名称包含实际日期的文件,例如 temperature_log_2016_08_13.txt

When running the script I get an ambiguous redirection erroreach time lines with the echo commands execute. I tried all sort of combinations with quotes, double-quotes, no quotes. Pls advice.

运行脚本时,每次执行带有回显命令的行时,我都会收到一个不明确的重定向错误。我尝试了各种带引号、双引号和无引号的组合。请建议。

#!/bin/bash

timestamp() {
    date +%F_%H-%M-%S
}

temperature() {
    /opt/vc/bin/vcgencmd measure_temp
}

temp=$(temperature)
temp2=${temp:5:9}

echo Temperature Log_$(date) >/home/pi/logs/temperature_log_$(date).txt

for i in {1..5}
do
    echo ${temp2} $(timestamp) >>/home/pi/logs/temperature_log_$(date).txt
    sleep 1
done

UPDATE: by putting the file name between quotation marks, as advised below, the ambiguous redirect error is gone, but instead the script produces 5 files with the date/timestamp in its name. I need only 1 file into which all the temperatures are written.

更新:通过将文件名放在引号之间,如下所示,不明确的重定向错误消失了,而是脚本生成了 5 个文件,其名称中带有日期/时间戳。我只需要 1 个文件,所有温度都写入其中。

回答by eckes

The $(date)in your example produces a timestamp with blanks. You might either use your function ($(timestamp)) instead, or quote the whole target filename (or both):

$(date)你的榜样产生用空格时间戳。您可以改用您的函数 ( $(timestamp)),或者引用整个目标文件名(或两者):

echo bla >> "/home/pi/logs/temperature_log_$(timestamp).txt"

And it is best to actually evaluate the timestamp in the filename only before the loop, otherwise you get a new file every second or minute:

最好只在循环之前实际评估文件名中的时间戳,否则每秒钟或每分钟都会得到一个新文件:

# %F: full date; same as %Y-%m-%d
logfile = "/home/pi/logs/temperature_log_$(date +%F).txt" 
echo "Temperature Log_$(date)" > "$logfile" # overwrite if started 2 times a day
for i in {1..5}
do
    temp=$(/opt/vc/bin/vcgencmd measure_temp)
    temp=${temp:5:9}
    echo "${temp} $(timestamp)" >> "$logfile"
    sleep 1
done

回答by choroba

Output of datecontains spaces. You need to doublequote the filename to make the spaces part of it:

输出date包含空格。您需要双引号文件名以使其成为空格的一部分:

echo Temperature Log_$(date) > "/home/pi/logs/temperature_log_$(date).txt"

Or use your function, but quoting the file name is a good habit anyway.

或者使用你的函数,但无论如何引用文件名是一个好习惯。