Search code examples
bashshellcygwindos2unix

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


My Script is :

#!/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}"

I originally wrote this on Ubuntu 16.04.1 LTS x64. I then installed cygwin on Win8 x64 and tried to run the script. I get the errors :

(standard_in) 1: illegal character: ^M

(standard_in) 1: syntax error

I searched a little, and found that it's primarily caused by unix characters in DOS and vice versa. I ran dos2unix on the file durationAdder.sh (my script) and the output suggested successful conversion. But I still keep getting the error. Why is this, and how do I fix it?


Solution

  • For future reference I'm posting my own answer for any other poor soul who might face the same problem.

    The problem was that ffprobe, which I got precompiled (as cygwin couldn't compile it) was outputting in DOS format. This cygwin didn't like, and was throwing an error. The solution was to dump the output in a file ffpOut, convert it silently using dos2unix and then feed back the input into the appropriate variable.

    The working code is :

    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}"
    

    Thank you, everyone for your time and your answers.