使用 bash shell:
我正在尝试逐行读取文件。
每行包含两个有意义的完整文件名,以 "``"
文件:1 image_config.txt
bbbbb.mp4``thumb/hashdata.gif
bbbbb.mp4``thumb/hashdata2.gif
Shell 脚本
#!/bin/bash
filename="image_config.txt"
while IFS='' read -r line || [[ -n "$line" ]]; do
IFS='``' read -r -a array <<< "$line"
if [ "$line" = "" ]; then
echo lineempty
else
file=${array[0]}
hash=${array[2]}
echo $file$hash;
output=$(ffmpeg -v warning -ss 2 -t 0.8 -i $file -vf scale=200:-1 -gifflags +transdiff -y $hash);
echo $output;
# echo ${array[0]}${array[1]}${array[2]}
fi;
done < "$filename"
第一次执行成功,但当循环执行第二次时。
变量file
从bbbbb.mp4
丢失了bbbbb
并输出以下内容
输出:
user@domain [~/public_html/Videos]$ sh imager.sh
bbbbb.mp4thumb/hashdata.gif
.mp4thumb/hashdata2.gif
.mp4: No such file or directory
lineempty
最佳答案
旁白:
在 IFS
中使用同一字符两次是没有意义的。
IFS=\`
够了。
看看这个:
var='abc``def'
IFS=\`\` read -ra arr <<< "$var"
printf '<%s>\n' "${arr[@]}"
输出:
<abc>
<>
<def>
如您所见,arr[0]
是 abc
,arr[1]
是空,并且arr[2]
是 def
,而不是 arr[0]
是 abc
和 arr[1 ]
正如人们所期望的那样,是 def
。
取自 IFS wiki的Greycat和 Lhunath Bash Guide :
The IFS variable is used in shells (Bourne, POSIX, ksh, bash) as the input field separator (or internal field separator). Essentially, it is a string of special characters which are to be treated as delimiters between words/fields when splitting a line of input.
关于bash shell : lost first element data partially,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909698/