【发布时间】:2016-08-09 13:54:47
【问题描述】:
当使用sed -e 更新配置文件的某些参数并将其通过管道传输到| tee(将更新的内容写入文件)时,这会随机中断并导致文件无效(大小为0)。
总之,这段代码用于更新参数:
# based on the provided linenumber, add some comments, add the new value, delete old line
sed -e "$lineNr a # comments" -e "$lineNr a $newValue" -e "$lineNr d" $myFile | sudo tee $myFile
我设置了一个调用此更新命令 100 次的脚本。
- 在与 OSX 共享目录上的 Ubuntu VM (Parallels Desktop) 中,此 行为最多发生 50 次
- 在 Ubuntu VM (Parallels Desktop) 上 Ubuntu 分区此行为最多出现 40 次
- 在本机系统(带有 Ubuntu 的 IntelNUC)上,此行为最多发生 15 次
有人可以解释为什么会这样吗?
这是一个功能齐全的脚本,您也可以在其中运行实验。 (所有必要的文件都由脚本生成,因此您可以简单地将其复制/粘贴到 bashscriptfile 中并运行它)
#!/bin/bash
# main function at bottom
#====================
#===HELPER METHOD====
#====================
# This method updates parameters with a new value. The replacement is performed linewise.
doUpdateParameterInFile()
{
local valueOfInterest="$1"
local newValue="$2"
local filePath="$3"
# stores all matching linenumbers
local listOfLines=""
# stores the linenumber which is going to be replaced
local lineToReplace=""
# find value of interest in all non-commented lines and store related lineNumber
lineToReplace=$( grep -nr "^[^#]*$valueOfInterest" $filePath | sed -n 's/^\([0-9]*\)[:].*/\1/p' )
# Update parameters
# replace the matching line with the desired value
oldValue=$( sed -n "$lineToReplace p" $filePath )
sed -e "$lineToReplace a # $(date '+%Y-%m-%d %H:%M:%S'): replaced: $oldValue with: $newValue" -e "$lineToReplace a $newValue" -e "$lineToReplace d" $filePath | sudo tee $filePath >/dev/null
# Sanity check to make sure file did not get corrupted by updating parameters
if [[ ! -s $filePath ]] ; then
echo "[ERROR]: While updating file it turned invalid."
return 31
fi
}
#===============================
#=== Actual Update Function ====
#===============================
main_script()
{
echo -n "Update Parameter1 ..."
doUpdateParameterInFile "Parameter1" "Parameter1 YES" "config.txt"
if [[ "$?" == "0" ]] ; then echo "[ OK ]" ; else echo "[FAIL]"; return 33 ; fi
echo -n "Update Parameter2 ..."
doUpdateParameterInFile "Parameter2" "Parameter2=90" "config.txt"
if [[ "$?" == "0" ]] ; then echo "[ OK ]" ; else echo "[FAIL]"; return 34 ; fi
echo -n "Update Parameter3 ..."
doUpdateParameterInFile "Parameter3" "Parameter3 YES" "config.txt"
if [[ "$?" == "0" ]] ; then echo "[ OK ]" ; else echo "[FAIL]"; return 35 ; fi
}
#=================
#=== Main Loop ===
#=================
#generate file config.txt
printf "# Configfile with 3 Parameters\n#[Parameter1]\n#only takes YES or NO\nParameter1 NO \n\n#[Parameter2]\n#Parameter2 takes numbers\nParameter2 = 100 \n\n#[Parameter3]\n#Parameter3 takes YES or NO \nParameter3 YES\n" > config.txt
cp config.txt config.txt.bkup
# Start the experiment and let it run 100 times
cnt=0
failSum=0
while [[ $cnt != "100" ]] ; do
echo "==========run: $cnt; fails: $failSum======="
main_script
if [[ $? != "0" ]] ; then cp config.txt.bkup config.txt ; failSum=$(($failSum+1)) ; fi
cnt=$((cnt+1))
sleep 0.5
done
问候 唐普罗米洛
【问题讨论】:
-
如果你要扔掉
stdout,为什么还要使用tee?此外,看起来您很可能遇到了竞争条件,因为您使用tee覆盖文件的同时使用sed处理它。如果tee在sed可以得到它之前截断文件,你将得到一个0长度的文件。如果您的sed支持它,您可以让它修改文件,否则您应该将输出写入临时文件,然后将其移动到原始名称 -
管道的两侧是异步的;你不能保证
sed在tee覆盖之前完全消耗了myFile的内容。 -
感谢@EricRenouf 和@chepner 非常清楚地指出了这一点。我使用
tee的原因基本上是对管道的好奇。