为什么Shell脚本中的“读取"命令缺少初始字符? [英] why 'read' command in shell script is missing initial characters?

查看:123
本文介绍了为什么Shell脚本中的“读取"命令缺少初始字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下shell脚本,除了第一行外,每行都缺少一些初始字符(到目前为止,我错过了最初的几个字符).

I have the following shell script and it is missing some initial characters (it misses initial couple of characters, so far in my observation) from each line except the first line.

这仅在我使用ffmpeg命令时发生.否则就可以了.但是此命令可以执行此脚本中的实际任务.

And this happens only when I use the ffmpeg command. Otherwise, it is fine. But this command does the actual task in this script.

为什么会这样,解决方法是什么?

Why is it so and what is the fix?

#!/bin/bash

while read line; do
    printf "%s\n" "$line" 
    ifile=$line
    printf "%s\n" "$ifile" 
    ofile=abc_$line
    printf "%s\n" "$ofile" 

    ############### Problem is the following command: ##########
    ffmpeg -y -i $ifile -c:v libx264rgb -b:v 512k -bf 0 -pix_fmt rgb24  -r 25 -strict -2 $ofile
    ##########rest is fine##########

    echo $ifile
done < file_list

推荐答案

本文对此进行了很好的解释我正在逐行读取文件并运行ssh或ffmpeg,只有第一行得到处理!.逐行读取文件时,如果循环内的命令也读取stdin,则可能会耗尽输入文件.在您的情况下,ffmpeg也会从标准输入中读取.

This is pretty well explained in this post I'm reading a file line by line and running ssh or ffmpeg, only the first line gets processed!. When reading a file line by line, if a command inside the loop also reads stdin, it can exhaust the input file. In your case ffmpeg also reads from stdin.

最常见的症状是while读取循环仅运行一次,即使输入包含很多行.这是因为其余的行被有问题的命令吞没了.解决该问题的最常见方法是通过执行< /dev/null

The most common symptom of this is a while read loop only running once, even though the input contains many lines. This is because the rest of the lines are swallowed by the offending command. The most common fix for the problem is to close the stdin of the ffmpeg by doing < /dev/null

ffmpeg -y -i "$ifile" -c:v libx264rgb -b:v 512k -bf 0 -pix_fmt rgb24  -r 25 -strict -2 "$ofile" < /dev/null

或使用标准输入以外的其他文件描述符

or use another file descriptor other than standard input

 while read -r line <&3; do
     ifile="$line"
     ofile="abc_${line}"
     ffmpeg -y -i "$ifile" -c:v libx264rgb -b:v 512k -bf 0 -pix_fmt rgb24  -r 25 -strict -2 "$ofile"
 done 3<file

或者您的问题可能完全是输入文件具有从DOS环境继承来的DOS样式行结尾的情况.您可以通过在可能显示CRLF line terminators的输入文件(file file_list)上运行file命令来检查.在这种情况下,请清除输入文件dos2unix file_list,然后重新运行脚本.

Or your problem could altogether be a case of the input file having DOS style line endings carried over from a DOS environment. You can check that out by running the file command on the input file (file file_list) which could show CRLF line terminators. In such case do a clean-up of the input file as dos2unix file_list and re-run your script.

这篇关于为什么Shell脚本中的“读取"命令缺少初始字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆