Variables in shell script not working

Jef Driesen jefdriesen at
Sat Nov 15 16:04:31 UTC 2008

Karl Auer wrote:
> On Sat, 2008-11-15 at 16:20 +0100, Jef Driesen wrote:
>> This is indeed the problem! At first, I only had the sed command,
>> which does accept a filename AND outputs to stdout. I simply didn't
>> think about this difference when adding the dos2unix command.
>> Is it possible to have the pipe command in the variable, without cat 
>> interpreting both the pipe symbol "|" and the "dos2unix" as its
>> argument?
>> SOURCE="cat $FILENAME | dos2unix"
> Yes, that will work fine - but it doesn't run sed, and the output goes
> to stdout rather than being saved in the original file.
> This will convert the filename and (if the conversion worked) run it
> through sed, sending the output to stdout:
>    FILENAME=$1
>    dos2unix $FILENAME && cat $FILENAME | sed s/foo/bar/
> That's basically what Carl was saying, too.
> But what is it you actually want to achieve here? The above will send
> the sedded output to stdout after unixifying the specified file, but the
> sedded output will not be saved in the file.

What I want to achieve is process some data that is passed through stdin 
or a filename. Which source is used depends on whether a filename is 
passed on the commandline or not. That way I can run my script in two 
ways (similar to how you can use sed):

./myscript myfile
myprogram | ./myscript

Because sometimes my input is stored in a file and sometimes it is the 
output of another program.

The output should always go to stdout and when the script is used with a 
filename, the original should not be modified. So that excludes running 
dos2unix on it directly.

More information about the ubuntu-users mailing list