Author Topic: shell variables  (Read 1272 times)

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« on: 21 October 2002, 04:43 »
hi guys -
how do i get a shell variable data available
that is changed in a "while" loop?
eg.

#==
ipad="168.12"
export ipad
echo "$ipad before"

cat afile|while read thevar therest
do
ipad=$thevar
echo "$ipad inside"
done

echo "$ipad after"
#==

if the file has one line as follows
202.15 and the rest of the line

the first echo gives "168.12 before"
the 2nd gives "202.15 inside"
But the 3rd gives "168.12 after"

So how do I get the value for $ipad
that was set inside the loop?

Is this clear - or am I confusing?

Running redhat 7.3
2.4.18-17.7.x

Regards in advance!
Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #1 on: 21 October 2002, 05:03 »
Your while statement is after a pipe (|) symbol which means your "do" loop is done in a sub shell. The variable would be changed in the subshell but  not passed back to the parent shell when control is returned. You are going to end up with what was the variable last was in the parent. Varible values can be transmitted from a parent to a subshell (if they are "export"ed) but variable values can not be returned to the parent from a sub shell.

Here is one solution. It's not a "while" loop but it accomplishes the same thing. If you absolutely need a while loop I can give you an example of that. The key is not to use the pipe before the while:

Code: [Select]

[ October 20, 2002: Message edited by: void main ]

Someone please remove this account. Thanks...

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« Reply #2 on: 21 October 2002, 05:17 »
TX voidmain

That is a solution - but my previous script
omitted the obvious - ie. many params.

#====
ipad="168.12"
export ipad
echo "$ipad before"
dpad="thename"
export dpad
echo "$dpad before"

cat ctrl_1.pps|while read thevar thename therest
do
   ipad=$thevar
   dpad=$thename
   echo "$ipad inside"
   echo "$dpad inside"
done

echo "$ipad after"
echo "$dpad after"
#===

and the file afile as follows
202.45 aname some more params

If the solution you gave is the only answer
I'll have to use a counter and lotsa code
to get each parameter set.

Tx again in advance.
Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #3 on: 21 October 2002, 05:26 »
If you need the values after the loop, I assume there will only be one line in the file. If so what is the purpose of the while loop?  Unless you only need the last line. But if there is only one line in the file why not just do this:

Code: [Select]

But if you really want to do it in a loop, do it like this:

Code: [Select]

That's ugly. I would probably switch to Perl at this point.  

[ October 20, 2002: Message edited by: void main ]

Someone please remove this account. Thanks...

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« Reply #4 on: 21 October 2002, 05:55 »
TX again voidmain

Here is a homebrew solution
Primary 'cause I don't know before
hand which line to use

#===
ipad="168.12"
export ipad
echo "$ipad before"
dpad="thename"
export dpad
echo "$dpad before"

theline=`grep -v qqq afile`

read thevar thename therest <<THEEND
$theline
THEEND
   ipad=$thevar
   dpad=$thename
   echo "$ipad inside"
   echo "$dpad inside"

echo "$ipad after"
echo "$dpad after"
#===

so with a file with 3 lines - and somewhat as follows
this line to ignore qqq
202.456 para2 para3 and other paras
and ignore this line qqq

As I know I only need 1 line this'll work??
In any case multiple returns - and
only the first line is used..

TX for your time voidmain.
Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #5 on: 21 October 2002, 06:07 »
Well, it's tough to tell without knowing the bigger picture of what you are working on. If it works for you go for it. I rarely use the "read" command in a shell script though. I usually use "cut" and other utilities, even though read may be more appropriate in this example. But like I mentioned. For parsing text files I find Perl to be *very* easy for this kind of stuff. Don't know if that is an option for you but Perl really makes this a no brainer.
Someone please remove this account. Thanks...

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« Reply #6 on: 21 October 2002, 06:14 »
TX.

Yes - perl was an option - should have started
there.

But I started with this - and then got stuck
on solving the problem via bash (ksh actually).

In fact - the output gets fed to a perl script
but I want to keep that as generic as possible.

TX.

PS. How to edit a post? can it be done after I
sent it?
Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #7 on: 21 October 2002, 06:25 »
Yes you can edit your post after you post it by clicking on the little icon above your post that looks like a little pencil/paper ICON.

I also love the Korn shell. Bash is very much like Korn shell but it can not do math operations in a while loop like Korn shell can:

Code: [Select]
Someone please remove this account. Thanks...

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« Reply #8 on: 21 October 2002, 06:44 »
yep - I found that out the hard way.

So now I when I write scripts I need to
comment out some code until I can get to
the RS6000 box to test it.

Tx for all the help.

It's 03h40 where I am - so off to bed
and get ready to be at the client by 07h00.


[ October 20, 2002: Message edited by: caveman ]
just test da edit  :D   !

And finding myself hitting the ESC k and vi
edit commands in bash. Only to find it really screws with my bash command line.

g'night

[ October 20, 2002: Message edited by: caveman ]

Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #9 on: 21 October 2002, 06:52 »
quote:
Originally posted by caveman:
yep - I found that out the hard way.

So now I when I write scripts I need to
comment out some code until I can get to
the RS6000 box to test it.



Why is that? You know you can get Korn shell for your Linux box right? I used to admin a cluster of RS/6000s (990s, 590s, 340s, M20s, and Xterminals) several years ago. I really liked those boxen. And I used a lot of Linux as cheap X clients into the cluster.
Someone please remove this account. Thanks...

caveman_piet

  • Member
  • **
  • Posts: 52
  • Kudos: 0
    • http://www.pexy.co.za
shell variables
« Reply #10 on: 21 October 2002, 06:58 »
Nope

I didn't know that.

Is it a standard download?
Idea where I can get it?

Will have to go use uncle google and
fix this oversight - haste pronto!!

But first to bed.

While I'm at it?
Where can I read up on how to get an ISDN modem connected!. Just got it installed - using serial it works (slowly) but this thing has an USB port!

Its a US Robotics Model # 3CP993469

Now g'morning - off to bed - can't take any
more caffeine!
Microsoft apparently thinks that R&D stands for 'Rewrap & Disguise'.

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
shell variables
« Reply #11 on: 21 October 2002, 07:15 »
If you are using an RPM based system get this:

ftp://speakeasy.rpmfind.net/linux/contrib/libc6/i386/ksh93-2000.10.31.0-1.i386.rpm

Of you can get the "real deal" here at the main site:

http://www.research.att.com/~gsf/download/

More specifically here:
http://www.research.att.com/~gsf/download/tgz/ast-ksh.2002-09-22.linux.i386.tgz

[ October 20, 2002: Message edited by: void main ]

Someone please remove this account. Thanks...