You are not logged in.

#1 2010-02-06 20:14:15

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

[Solved] Shell script to read unhashed lines from file.

Hello.
I want to write a shell script  (it can be bash,sh,ksh,csh or zsh) that will be:
1.Read unhashed lines from file named mirrors.conf.
2.Put them in ~/.profile as PKG_PATH=line catted from mirrors.conf.
Just like as mirrors.conf file for pacman.
Now it can cat lines but how to put them in files as PKG_PATH?
Thanks for answers.

Last edited by SpeedVin (2010-03-01 17:33:20)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#2 2010-02-06 23:53:33

Kitty
Member
From: The Burning Desert
Registered: 2008-01-11
Posts: 88

Re: [Solved] Shell script to read unhashed lines from file.

try:

{kitty|~}$ sed -n '/^#\|^$/d; s/^Server = /PKG_PATH=/p' /etc/pacman.d/mirrorlist
PKG_PATH=ftp://mirror.giantix-server.de/archlinux/$repo/os/i686

using the mirrorlist as an example.


/etc/rc.d/ is where daemons reside. Beware.

Offline

#3 2010-02-07 00:42:06

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [Solved] Shell script to read unhashed lines from file.

Kitty wrote:

try:

{kitty|~}$ sed -n '/^#\|^$/d; s/^Server = /PKG_PATH=/p' /etc/pacman.d/mirrorlist
PKG_PATH=ftp://mirror.giantix-server.de/archlinux/$repo/os/i686

using the mirrorlist as an example.

FYI: You don't need the -n flag or the 'p' modifier at the end of your s/// command.

Offline

#4 2010-02-07 03:21:10

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Shell script to read unhashed lines from file.

grep -v '^#\|^$' /etc/pacman.d/mirrorlist | while read mirror; do
  echo "${mirror/Server = /PKG_PATH=}" >> ~/.profile
done

i hate sed smile.

Offline

#5 2010-02-07 05:04:23

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [Solved] Shell script to read unhashed lines from file.

brisbin33 wrote:
grep -v '^#\|^$' /etc/pacman.d/mirrorlist | while read mirror; do
  echo "${mirror/Server = /PKG_PATH=}" >> ~/.profile
done

i hate sed smile.

That will mess with the whitespace. Maybe for the intended use that doesn't matter. If it did matter, you'd want to adjust that to:

... | while IFS= read -r mirror; do
    ....
done

Offline

#6 2010-02-07 05:29:59

res
Member
Registered: 2010-01-14
Posts: 55

Re: [Solved] Shell script to read unhashed lines from file.

^ IFS is not necessary here; it won't mess with the white space:

$ echo 'args    with  formatted   spaces  ' | while read -r; do echo "$REPLY"; done
args    with  formatted   spaces

Offline

#7 2010-02-07 06:28:20

sdolim
Member
Registered: 2010-01-20
Posts: 67

Re: [Solved] Shell script to read unhashed lines from file.

Add this line to ~/.profile:

PKG_PATH=$(awk '/^Server/ { print $3; exit }' /etc/pacman.d/mirrorlist)

Note: You can also use `...` instead of $(...). 

This way you don't have to do anything when mirrorlist changes.

Offline

#8 2010-02-07 11:00:38

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [Solved] Shell script to read unhashed lines from file.

res wrote:

^ IFS is not necessary here; it won't mess with the white space:

$ echo 'args    with  formatted   spaces  ' | while read -r; do echo "$REPLY"; done
args    with  formatted   spaces

It matters for leading/trailing whitespace:

$ echo '    after   some  ' | while read -r line; do echo "<$line>"; done
<after   some>

$ echo '    after   some  ' | while IFS= read -r line; do echo "<$line>"; done
<    after   some  >

Offline

#9 2010-02-28 18:39:15

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Shell script to read unhashed lines from file.

Thank you all for answers I can't use sed then I used brishbin33 script smile
Kitty: I like your script and I think that I will learn Sed smile


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

Board footer

Powered by FluxBB