views:

103

answers:

2

I am trying to build a script (I'm pretty new to linux scripting) and I can't seem to figure out why I'm not able to run this script. If I keep the header (#!/bin/sh) in, I get the following:

-bash: /tmp/ConvertAndUpdate.sh: /bin/sh^M: bad interpreter: No such file or directory

If I take it out, I get the following:

'tmp/ConvertAndUpdate.sh: line 2: syntax error near unexpected token `do
'tmp/ConvertAndUpdate.sh: line 2: `do

Any ideas? Here is the full script:

#!/bin/sh
for file in *.wav; do
    mp3=$(basename .$file. .wav).mp3;
    #echo $mp3
    nice lame -b 16 -m m -q 9 .resample 8 .$file. .$mp3.;
    touch .reference .$file. .$mp3.;
    chown apache.apache .$mp3.;
    chmod 600 .$mp3.;
    rm -f .$file.;
    mv .$file. /converted;
    sql="UPDATE recordings SET IsReady=1 WHERE Filename='${file%.*}'"
    echo $sql | mysql --user=me --password=pasword Recordings
    #echo $sql
done
+1  A: 

You may actually have a non-printing character in there. You probably saved this file on Windows with the "\r\n" line endings instead of the regular UNIX "\n" endings. Use dos2unix to fix it. Also, just some things....

You don't appear to be quoting "$file" or any of these other variables... so your script might work if there are no spaces in any of the filenames, but you can bet its going to break if there are spaces. Whenever you use a variable, it is very important to ensure that it remains properly quoted.

Unlike in C++ and Java, the semicolon in shell scripting is used to separate, not terminate statements. You should not put semicolons at the end of statements; you should only use semicolons if you want to use more than one statement per line.

There are several different interpreters. If you use /bin/sh, you are only guaranteed the minimum features that are required by POSIX. If you are planning to use BASH-specific features, use /bin/bash. As a habit, I use #! /bin/bash unless I absolutely need to target just /bin/sh, in which case I also do a very thorough review of my code to ensure that I am not using any features beyond those of /bin/sh.

I have fixed a part of your shell script as follows:

#! /bin/bash
for file in *.wav; do
    filebasename=`basename "$file" .wav`
    filemp3="$filebasename.mp3"
    nice lame -b 16 -m m -q 9 --resample 8 "$file" "$filemp3"
    #....
done

I am not sure exactly what you were trying to do with the rest of it.

Michael Aaron Safyan
Very thurough! Thank you!
Benny
A: 

You have apparently saved the file with CRLF line endings instead of the proper LF line endings. The ^M is the giveaway.

Change the file to LF line endings.

ndim