Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am writing a script am I am getting an error on lines 2-3. The error is printed like this:

./ex6.sh: line2: $'\r': command not found
./ex6.sh: line3: syntax error near unexpected token '$'\r''
./ex6.sh: line3: 'fund ()

The file is called ex6.sh. Could someone provide some guidance as to why this error is happening?

#!/bin/sh

func () 
{
if [ "$1" == "" ]; then
echo "Default";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        if [ "$i" != "./script.sh" ]; then
            echo $i "is a file";        
        fi
    fi
done
fi

if [ "$1" == "--long" ]; then
echo "--long";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        echo $i "is a file";        
    fi
done
fi

if [ "$1" == "--rm" ]; then
echo "--rm";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        echo $i "is a file";        
    fi
done
fi
}

getArgs () {
if [ "$1" == "--long" ]; then
    echo "got the first param $1";
else
    if [ "$1" == "--rm" ]; then
        echo "got the second param $1";
    else
        if [ "$1" == "" ]; then
            echo "got default param";
        else
            echo "script.sh: unknown option $1";
            exit;
        fi  
    fi
fi

}

getArgs $1;
#ARGS=$1;

func $1;
share|improve this question

closed as too localized by Daniel Beck, slhck, Nifle, Sathya Oct 7 '11 at 6:58

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
And what's your question? Also, please take at least the 20 seconds it takes to learn how to insert formatted code into your post on this site. –  Daniel Beck Oct 1 '11 at 18:15
    
Your script probably can't handle file names that contain spaces. Any white space is considered a divider in collections (e.g. find return values), not just newlines. –  Daniel Beck Oct 1 '11 at 18:19
1  
That's what I meant by 20 seconds: That's my estimation how long it'll take you to press the question mark link/button, then on Code, and read that you need to indent multiple lines for pre formatted text. –  Daniel Beck Oct 1 '11 at 18:21
1  
Cygwin is a set of libraries and executables. It doesn't say anything. –  Ignacio Vazquez-Abrams Oct 1 '11 at 18:21
1  
Edit your old question, please don't post a new one. I already did this for you, but please remember it for the future. –  slhck Oct 1 '11 at 18:31
show 2 more comments

2 Answers

Use dos2unix or a proper text editor to convert all your line breaks from Windows \r\n to \n.

share|improve this answer
add comment

Check the end line character in the file. Probably you have Windows style (CR + LF) and you should have Unix/Linux style (only LF).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.