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

时间:2017-01-16 10:46:15

标签: bash shell cygwin dos2unix

我的剧本是:

#!/bin/bash
LGREEN='\033[1;32m'
LBLUE='\033[1;36m'
NC='\033[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=$1
    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=$1
        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 $1 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:非法字符:^ M

     

(standard_in)1:语法错误

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

1 个答案:

答案 0 :(得分:1)

为了将来的参考,我会为可能遇到同样问题的其他可怜的灵魂发布我自己的答案。

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

工作代码是:

LGREEN='\033[1;32m';
LBLUE='\033[1;36m';
NC='\033[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=$1;
 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=$1;
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 $1 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}"

谢谢大家的时间和答案。