[nycbug-talk] sed Question

Kevin Reiter tux at penguinnetwerx.net
Thu Mar 30 19:28:32 EST 2006


Kevin Reiter wrote:
> Jan Schaumann wrote:
>> Kevin Reiter <tux at penguinnetwerx.net> wrote:
>>  
>>> #!/bin/sh
>>>
>>> echo ""
>>> echo "Finding the fastest cvsup server."
>>> echo ""
>>> fastest_cvsup -c us | grep 1st | cut -d : -f 2 > server.txt
>>> sed 's/^[ \t]*//' server.txt > fast.txt
>>> fastest=`cat fast.txt`
>> You can do that without any temporary files (and one pipe less):
>>
>> fastest=`fastest_cvsup -c us | awk '/1st/ {print $3;}'`
>> sed -e "s/host=cvsup.*FreeBSD\.org/host=$fastest/" ports-sup
>>       ^^^
>>
>> Use regular quotes (") instead of ', otherwise the variable '$fastest'
>> won't be expanded.
> 
> Tried that, but it didn't write to the file (ports-sup):
> 
> kevin at chronos [~/scripts/practice/sed]$ ./update_ports.sh
> 
> Finding the fastest cvsup server and modifying
> /root/ports-sup to use that server.
> 
> *default tag=.
> *default host=cvsup3.us.freebsd.org
> *default prefix=/usr
> *default base=/usr/local/cvsup
> *default release=cvs delete use-rel-suffix compress
> 
> ports-all
> 
> 
> Done.  Now run cvsup ports-sup from root's directory.
> 
> kevin at chronos [~/scripts/practice/sed]$ cat fast.txt
> cvsup11.us.freebsd.org
> kevin at chronos [~/scripts/practice/sed]$ cat ports-sup
> *default tag=.
> *default host=cvsup11.FreeBSD.org
> *default prefix=/usr
> *default base=/usr/local/cvsup
> *default release=cvs delete use-rel-suffix compress
> 
> ports-all
> 
> 
> kevin at chronos [~/scripts/practice/sed]$
> 
> (I'm not writing to /root/ports-sup, but a copy of it in that directory)

It *appears* to work, but it's not actually writing to the ports-sup 
file, even though it displays what it would write to it:

kevin at chronos [~/scripts/practice/sed]$ ./test1.sh
Checking servers...

The fastest server seems to be cvsup11.us.freebsd.org
*default tag=.
*default host=cvsup11.us.freebsd.org
*default prefix=/usr
*default base=/usr/local/cvsup
*default release=cvs delete use-rel-suffix compress

ports-all


Now, go get some coffee while the tree is updating.

kevin at chronos [~/scripts/practice/sed]$ cat ports-sup
*default tag=.
*default host=cvsup99.FreeBSD.org
*default prefix=/usr
*default base=/usr/local/cvsup
*default release=cvs delete use-rel-suffix compress

ports-all

kevin at chronos [~/scripts/practice/sed]$ ls -l ports-sup
-rw-rw-rw-  1 kevin  wheel  164 Mar 30 19:28 ports-sup

Here's what I have now:

#!/bin/sh

# Find the fastest cvsup server and use that for the ports update.

echo "Checking servers..."
echo ""
fastest=`fastest_cvsup -c us | awk '/1st/ {print $3;}'`
echo "The fastest server seems to be $fastest"
sed -e "s/host=.*/host=$fastest/" ports-sup
echo ""
echo ""
echo "Now, go get some coffee while the tree is updating."
#sudo cvsup ports-sup
echo ""

Is there a reason it isn't actually writing to the file?



More information about the talk mailing list