Variables in shell script not working
Carl Friis-Hansen
ubuntuuser at carl-fh.com
Sat Nov 15 15:41:06 UTC 2008
Jef Driesen wrote:
> Karl Auer wrote:
>> On Sat, 2008-11-15 at 14:40 +0100, Jef Driesen wrote:
>>> If I quote the variable, the error disappears, but the rest of my
>>> script does not work anymore. No output is produced on stdout.
>>> Echo'ing the variable works, but the script does not seem to execute
>>> the contents of the variable when it reaches the line
>>>
>>> $SOURCE | sed 's/foo/bar'
>> What happens? What output do you see, if any?
>>
>> Does this script work?
>>
>> X="echo fred"
>> $X | grep fred
>>
>> It should output
>>
>> fred
>>
>> If that works, try this:
>>
>> #!/bin/sh
>> X="echo fred"
>> $X | sed s/fred/martha/
>>
>> It should output
>>
>> martha
>>
>> If both those work, I suspect dos2unix does not output what you think it
>> outputs. As far as I can tell, dos2unix outputs nothing unless it has a
>> problem with a conversion (it converts in place by default), so I'm a
>> bit puzzled about what sed is supposed to be doing.
>
> 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"
Sorry I was missing something:
SOURCE="dos2unix '$FILENAME' && cat '$FILENAME' | sed s/fred/martha/"
--
+---------------------------------+-------------------+
| Carl Friis-Hansen | Fiskeryd Nybygget |
| http://computingconfidence.com/ | 341 91 Ljungby |
| Phone: +46 (0)372 15033 | Sweden |
+---------------------------------+-------------------+
More information about the ubuntu-users
mailing list