1

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?

Community
  • 1
  • 1
Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35
  • try running `unix2dos` – Ipor Sircer Jan 16 '17 at 10:57
  • @IporSircer Tried that already. It converted it to unix format so, the file couldn't be executed by cygwin at all. Then I converted the file back using dos2unix (to ensure all the win characters including every single one that might have escaped my grasp originally are converted to unix format). But that didn't help either. I still get the error. – Somenath Sinha Jan 16 '17 at 11:02
  • The script works for me. May be `^M` is in output of ffprobe, that I don't have ? – matzeri Jan 16 '17 at 11:38
  • @matzeri That's exactly what I suspected and was happening. My answer below illustrates the problem. – Somenath Sinha Jan 16 '17 at 11:46

1 Answers1

1

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.

Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35
  • 1
    The backquotes in your `ffprobe` line don't make sense. Write it as `s=$(ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$fileName"|dos2unix)` – user1934428 Jan 16 '17 at 14:19