(standard_in) 1:非法字符:^M - dos2Unix 不工作?

(standard_in) 1: illegal character: ^M - dos2Unix not working?

我的脚本是:

#!/bin/bash
LGREEN='3[1;32m'
LBLUE='3[1;36m'
NC='3[0m'

function convertToTime {
    min=$(echo "$min+$sec/60" | bc)
    sec=$(echo "$sec%60" | bc)
    hrs=$(echo "$hrs+$min/60" | bc)
    min=$(echo "$min%60" | bc)
}

function convertToTimeTotal {
    totalMin=$(echo "$totalMin+$totalSec/60" | bc)
    totalSec=$(echo "$totalSec%60" | bc)
    totalHrs=$(echo "$totalHrs+$totalMin/60" | bc)
    totalMin=$(echo "$totalMin%60" | bc)
}

totalSec=0
totalMin=0
totalHrs=0

function recursiveDirFinder {
    presentDir=
    cd "$presentDir"
    ls >filelist.tmp
    sec=0
    min=0
    hrs=0
    while
        read fileName  
        do
        if 
            test -d "$fileName"
            then
            presentDir+="/$fileName"
            recursiveDirFinder "$presentDir"
            presentDir=
        else
            if [[ $fileName == *.mp4 ]]; then
                s=`ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$fileName"`
                sec=$(echo "$sec+$s" | bc)
            fi
        fi
    done <filelist.tmp
    convertToTime
    echo -e "Video Duration in  is ${LBLUE}$hrs:$min:$sec${NC}"
    totalSec=$(echo "$totalSec+$sec" | bc)
    rm filelist.tmp
    cd ..
}

recursiveDirFinder "E:/RBR Lectures"
convertToTimeTotal
echo -e "Total Duration is ${LGREEN}$totalHrs:$totalMin:$totalSec${NC}"

我最初是在 Ubuntu 16.04.1 LTS x64 上写的。然后我在 Win8 x64 上安装了 cygwin 并尝试 运行 脚本。我收到错误:

(standard_in) 1: illegal character: ^M

(standard_in) 1: syntax error

查了一下,主要是DOS下unix字符引起的,反之亦然。我在文件 durationAdder.sh(我的脚本)上 运行 dos2unix 并且输出表明转换成功。但我仍然不断收到错误。为什么会这样,我该如何解决?

为了将来参考,我将自己的答案发布给可能面临同样问题的任何其他可怜的灵魂。

问题是我预编译的 ffprobe(因为 cygwin 无法编译它)以 DOS 格式输出。这个 cygwin 不喜欢,并且抛出错误。解决方案是将输出转储到文件 ffpOut 中,使用 dos2unix 静默转换它,然后将输入反馈到适当的变量中。

工作代码是:

LGREEN='3[1;32m';
LBLUE='3[1;36m';
NC='3[0m';
function convertToTime { 
min=$(echo "$min+$sec/60" | bc);
sec=$(echo "$sec%60" | bc);
hrs=$(echo "$hrs+$min/60" | bc);
min=$(echo "$min%60" | bc);
};
function convertToTimeTotal {
 totalMin=$(echo "$totalMin+$totalSec/60" | bc);
 totalSec=$(echo "$totalSec%60" | bc);
 totalHrs=$(echo "$totalHrs+$totalMin/60" | bc);
 totalMin=$(echo "$totalMin%60" | bc);
};
totalSec=0;
totalMin=0;
totalHrs=0;
function recursiveDirFinder {
 presentDir=;
 cd "$presentDir";
 ls >filelist.tmp;
 sec=0;
 min=0;
 hrs=0;
 while
 read fileName;
 do 
 if
 test -d "$fileName"
 then presentDir+="/$fileName";
 recursiveDirFinder "$presentDir";
 presentDir=;
else 
    if [[ $fileName == *.mp4 || $fileName == *.MP4 ]];
        then 
        `ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$fileName">ffpOut`
        dos2unix -q ffpOut
        s=`cat ffpOut`
        sec=$(echo "$sec+$s" | bc);
        rm ffpOut
    fi;
fi;
done <filelist.tmp;
convertToTime;
echo -e "Video Duration in  is ${LBLUE}$hrs:$min:$sec${NC}";
totalSec=$(echo "$totalSec+$sec" | bc);
rm filelist.tmp;
cd ..;
};
recursiveDirFinder "E:/RBR Lectures";
convertToTimeTotal;
echo -e "Total Duration is ${LGREEN}$totalHrs:$totalMin:$totalSec${NC}"

谢谢大家的宝贵时间和回答。