bash shell : lost first element data partially

标签 bash shell ffmpeg centos

使用 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"

第一次执行成功,但当循环执行第二次时。 变量filebbbbb.mp4丢失了bbbbb 并输出以下内容

输出:

user@domain [~/public_html/Videos]$ sh imager.sh 
bbbbb.mp4thumb/hashdata.gif

.mp4thumb/hashdata2.gif
.mp4: No such file or directory

lineempty

最佳答案

请查看Bash FAQ 89 - I'm using a loop which runs once per line of input but it only seems to run once; everything after the first line is ignored?这似乎对您的情况有帮助。


旁白:

IFS 中使用同一字符两次是没有意义的。

IFS=\`

够了。

看看这个:

var='abc``def'
IFS=\`\` read -ra arr <<< "$var"
printf '<%s>\n' "${arr[@]}"

输出:

<abc>
<>
<def>

如您所见,arr[0]abcarr[1],并且arr[2]def,而不是 arr[0]abcarr[1 ] 正如人们所期望的那样,是 def

取自 IFS wikiGreycatLhunath 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/

相关文章:

bash - 使用 awk 从特定行获取第 n 个字段

bash - 在另一个目录中建立链接时符号链接(symbolic link)不起作用?

linux - Bash ASCII 实现

ffmpeg - 使用 FFMPEG,创建与视频比例成比例的缩略图

linux - 从数字 X 到最大数字 Y 的重数列表生成随机输出?

bash - 通过 tail 通过 tail 和 head 传输文件

shell - 在 shell 脚本中执行带有空格的动态创建的命令

macos - 如何使 zsh 在 Mac OS X 上作为登录 shell 运行(在 iTerm 中)?

ffmpeg - avfoundation 错误。无法检测到 list_devices

ffmpeg - 如何使用 ffmpeg 重新编码(有限的 x264)