Need end of vari. string value. (REALLY SOLVED)

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Need end of vari. string value. (REALLY SOLVED)

#1 Post by sunburnt »

I'm using a lanpup.cfg file to feed values to my setup script, looks like:

SERV=160SEA
SMBHOME=PUPPYPC
PW=guest
USRMODE=file

I need to get the value after the "=", a simple thing, except for me that is.

SERV=`cat /sbin/lanpup.cfg | grep -i "SERV=" | sed s/^.*$//`
______________________________________________^
Can't see how to cut the first part off, what goes above here?
Or is there a better way to do it? The wanted segment is unknown in length, I'm stumped.
In VB to get the end of the SERV= string would be: S = Right$(String, Len(String) - 5).

Quote by Jeremy Anderson:
"There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence."
Last edited by sunburnt on Fri 02 Dec 2005, 05:30, edited 1 time in total.

Guest

#2 Post by Guest »

you could try

# eval `grep 'SERV=' /sbin/lanpup.cfg`
# echo $SERV
160SEA
#

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#3 Post by MU »

SERV=`cat /sbin/lanpup.cfg | grep -i "SERV=" | sed s/^.*=//`

^ : from the beginning
.*= : whatever char until the last "="

Mark

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

Guest; what you've said makes perfect sense, I've not tried it yet, but it looks good.
MU; I had to stare at it for about 10 mins. before I saw what was going on.
The "s/^.*=/" I got, but it took awhile till I saw how that was being erased by "//", leaving only the last part.
It'll take awhile till the chaos logic has realigned my brain synapses from the M$ world, MANY THANKS.

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#5 Post by rarsa »

Actually the easiest way to get the 'value' for all the name/value pairs is:

Code: Select all

cut -d= -f2 /sbin/lanpup.cfg
Or if you want just for one value (assuming that the name starts at the begining of the line)

Code: Select all

grep "^SERV=" /sbin/lanpup.cfg | cut -d= -f2
Explanation

cut : splits an input line into fields
-d : specifies the delimiter. In this case "="
-f : specifies the field number. In this case the second field.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post by sunburnt »

rarsa; again I had to stare at it, but I understand what's happening, cut is like VBs InStr$ command, sort of.
The first example I want to know how to retreve each separate value if all of them are gotten at once.

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#7 Post by rarsa »

I went to bed yesterday thinking that we are getting it all wrong.

If you need to know the values of all those variables from within a script, Why not executing the cfg file as if it was a script? e.g.:

Code: Select all

sh /sbin/lanpup.cfg
echo $SERV
echo $SMBHOME

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

rarsa; I knew there had to be an even easier way to do it... you found it.
I took guest's suggestion & put it in a loop, & that was the tightest code till your idea, how simple!

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

How to create a shell script

#9 Post by Lobster »

We could do with a ScriptTutorials page on the wiki
incorporating or cutting / pasting from here:

http://puppylinux.org/wikka/AzBash
http://puppylinux.org/wikka/AshSh

with links such as
http://www.grymoire.com/Unix/Sed.html

and a wink video on "How to create a shell script"

:wink:
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#10 Post by sunburnt »

I'd help, but I'm afraid I'd get the info wrong somehow, I still need too much help myself, maybe in the future I could do a few things like that.

Post Reply