Sed in Script problem using expr

Diaa Radwan diaaradwan at gmail.com
Tue Nov 14 13:16:28 UTC 2006


On 11/14/06, Paul Ward <pnward at googlemail.com> wrote:
> Hi All,
>
> Can anyone help me with the following script problem please its driving me nuts.
>
> I am trying to make a script capable of commenting and uncommenting
> lines on a custom config file similar to this.
>
> myconf:
> #Months
> 02
> 04
> 06
> #08
> 10
> 12
>
> myscript.sh:
> #!/bin/bash
> echo "Enter a number from 1 - 10 to sed"
> read i
> NUM=`expr $i - 2`; printf %02d $NUM ; echo " "
> echo "I will sed this " $NUM
> read
> sed '/$NUM/s/^#//' myconf > newconf
>
> When you enter the number 10 you lose the decimal point after the expr
> sum subtracts 2 it is still 08 but on the sed I think it uses 8 not 08
> so the sed line fails
> Is there another way to do this?
>
> Any other methods would be welcome.
once you add `` ' `` single quote shell will not recognize the variable ,
I had this problem the only way I found is to use "sed -f" and
generate sed scripts on the run time on temp dir then run sed -f .

example
cat << EOS
sed statments
$NUM
sed statments
>> num.sed
sed -f num.sed
rm -f num.sed

hope this will help,I think there is smart way to do it.

>
> Thanks
>
> Paul
>
> --
> fedora-list mailing list
> fedora-list at redhat.com
> To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
>
--
Diaa Radwan,RHCE




More information about the fedora-list mailing list