【发布时间】:2010-10-08 02:16:30
【问题描述】:
我有一个 shell 脚本,它将执行的命令的输出保存到 CSV 文件中。它从以下格式的 shell 脚本中读取它必须执行的命令:
ffmpeg -i /home/test/videos/avi/418kb.avi /home/test/videos/done/418kb.flv
ffmpeg -i /home/test/videos/avi/1253kb.avi /home/test/videos/done/1253kb.flv
ffmpeg -i /home/test/videos/avi/2093kb.avi /home/test/videos/done/2093kb.flv
你可以看到每一行都是一个 ffmpeg 命令。但是,脚本只执行第一行。就在一分钟前,它正在执行几乎所有的命令。由于某种原因,它丢失了一半。我编辑了包含命令的文本文件,现在它只会执行第一行。这是我的 bash 脚本:
#!/bin/bash
# Shell script utility to read a file line line.
# Once line is read it will run processLine() function
#Function processLine
processLine(){
line="$@"
START=$(date +%s.%N)
eval $line > /dev/null 2>&1
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "$line, $START, $END, $DIFF" >> file.csv 2>&1
echo "It took $DIFF seconds"
echo $line
}
# Store file name
FILE=""
# get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi
fi
# read $FILE using the file descriptors
# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<$FILE
while read line
do
# use $line variable to process line in processLine() function
processLine $line
done
exec 0<&3
# restore $IFS which was used to determine what the field separators are
BAKIFS=$ORIGIFS
exit 0
感谢您的帮助。
更新 2
是 ffmpeg 命令而不是 shell 脚本不起作用。但正如保罗指出的那样,我应该只使用“\b”。我也在使用 Johannes 的较短脚本。
【问题讨论】:
-
不将 IFS 设置为 "\n\b" 意味着它正在占用空间吗?
-
我刚试过"\n",它仍然只读取第一行。我不太擅长 shell 脚本。
-
并在 processLine 中添加一些回声,以查看传入的内容以及完成处理的时间。也许是 ffmpeg 没有返回。
-
我认为你是对的。我将通过手动执行一些命令来检查 ffmpeg 正在做什么。
-
现在您知道为什么要立即将内容置于 CM 控制之下了;所以你可以回到工作版本!
标签: linux bash shell command-line scripting