alex(a)loes.org.lu wrote:
Hi
I have written a little script to change a html file. Several variables
are entered and will then be replaced in a file using sed. But this
doesn't work. I change the file menu using:
eval sed "
^^^^
Is there any particular reason for this eval? Eval is often used when a
double evaluation is needed, which is very rarely the case. In all other
cases, it only makes matters more complicated because you've got to be
very careful about quoting...
s/prix1/\"${prix[1]}\"/
If you have multiple substitutions, you need to prefix each one with -e
Or else the script will mistake each one except the first for a filename.
s/prix2/\"${prix[2]}\"/
s/prix3/\"${prix[3]}\"/
s/prix4/\"${prix[4]}\"/
s/prix5/\"${prix[5]}\"/
" menu
The computer replies: ./cantine.sh: line 38: s/prix1/3,77/: No such file
or directory
./cantine.sh: line 39: s/prix2/ndggnd/: No such file or directory
./cantine.sh: line 40: s/prix3/fgsngfsn/: No such file or directory
./cantine.sh: line 41: s/prix4/sfgng/: No such file or directory
./cantine.sh: line 42: s/prix5/gnfgsn/: No such file or directory
Indeed, that's what happens (every substitution mistaken for a filename)
./cantine.sh: line 43: menu: command not found
I suppose menu is supposed to be the input file. No idea why it is being
mistaken as a command, maybe the eval?
Try this instead:
sed \
-e "s/prix1/\"${prix[1]}\"/g" \
-e "s/prix2/\"${prix[2]}\"/g" \
-e "s/prix3/\"${prix[3]}\"/g" \
-e "s/prix4/\"${prix[4]}\"/g" \
-e "s/prix5/\"${prix[5]}\"/g" \
menu
(This is assuming you actually want quotes in your output. If the quotes
were only there for the eval, just leave them out:)
sed \
-e "s/prix1/${prix[1]}/g" \
-e "s/prix2/${prix[2]}/g" \
-e "s/prix3/${prix[3]}/g" \
-e "s/prix4/${prix[4]}/g" \
-e "s/prix5/${prix[5]}/g" \
menu
> Normal, since I don't use simple quotas ' .
> When I put the quotas :
> eval sed '
s/prix1/\"${prix[1]}\"/
With single quote, you don't need to escape the doublequote sign....
s/prix2/\"${prix[2]}\"/
s/prix3/\"${prix[3]}\"/
s/prix4/\"${prix[4]}\"/
s/prix5/\"${prix[5]}\"/
' menu
it gives the same answer. The first line is always well interpreted, but
the follows give the error messages.
How do I group sed commands together?
-e
I use Kubuntu 6.10
Thanx
Al
Alain