Variables in shell script not working

Jef Driesen jefdriesen at hotmail.com
Sat Nov 15 13:16:25 UTC 2008


I have a fairly simple script that processes some text files, by piping 
the data through a number of chained commands, such as dos2unix, sed, etc:

#!/bin/sh

dos2unix | sed 's/foo/bar'

As you can see, this script requires the data to be passed through stdin 
and I want to extend it to accept a filename. This is what I tried:

#!/bin/sh

FILENAME="$1"

if [ -n "$FILENAME" ]
then
    SOURCE=dos2unix "$FILENAME"
else
    SOURCE=dos2unix
fi

$SOURCE | sed 's/foo/bar'

But when I run this script with:

$./myscript foobar

I get this error:

./myscript: 10: foobar: not found

What am I doing wrong here? If the file contents is piped through stdin:

cat foobar | ./myscript

everything works fine.

Jef





More information about the ubuntu-users mailing list