LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2005, 10:12 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
bash: modifying a variable in a block


I have a very rare thing going on here. I have a repetitive cicle that modifies a variable. I actually do echos to check that the value changes. But once I finish the repetitive cicle, I check the variable and it's empty. Is there a simple explanation for this behavior?
 
Old 06-28-2005, 10:51 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Is there a simple explanation for this behavior?
Yes, that's normal shell (bash) behaviour.
You could save it to a temp-file each time it changes. The after the loop ("circle") get it from the tmp-file,
 
Old 06-28-2005, 11:17 AM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Do I really need to go down to a file? :O
 
Old 06-28-2005, 11:28 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Hmm, now it actually tried it. And to my surprise the variable does keep the last value from the loop.
Code:
#!/bin/sh

TMP=1
while [ $TMP -lt 10 ]; do
        TMP=$(($TMP+1))  # change the var TMP
        echo In loop TMP = $TMP
done
echo At the end TMP = $TMP
If you loose the variable after the loop, what does your loop look like?
 
Old 06-28-2005, 11:52 AM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Well... It's a <i>litle</i> bit longer, you know.

Code:
let's create the route
grep '^[ \t]*[a-zA-Z]' /etc/dhep/interfaces | while read an_interface_line; do
        # Let's find the interface configuration file
        length=$(expr length "$an_interface_line")
        interfacestart=$(expr index "$an_interface_line" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)
        interfaceend=$(expr index "$an_interface_line" ' ')
        if [ $niterfaceend = 0 ]; then
                # Only the interface name is written
                interfacelength=$(($length-$interfacestart+1))
                interface=$(expr substr $an_interface_line $interfacestart $interfacelength)
                options=""
        else
                # Interface and options
                interface length=$(($interfaceend-$interfacestart))
                interface=$(expr substr "$an_interface_line" $interfacestart $interfacelength)
                optionslength=$(($length-$interfacelength+1))
                options=$(expr substr "$an_interface_line" $(($interfaceend+1)) $optionslength)
        fi
        echo $interface Options: $options

        # This interfaces configuration
        config=/etc/dhep/$interface.cfg
        if [ $(grep IP $config | wc -l) != 0 ]; then
                # This interface is configured for nexthop
                # let's find its IP address
                aline=$(cat $config | grep IP)
                ip=$(expr substr "$aline" 4 15)
                aline=$(cat $config | grep GW)
                gw=$(expr substr "$aline" 4 15)
                nexthop="nexthop via $gw dev $interface $options $nexthop"
                echo $nexthop > /etc/dhep/nexthop.tmp
        fi
done
echo nexthop: $nexthop
A little explanation is needed, (I guess).

This piece of script is run as part of a dhclient exit hook. This is used to change the router's multipath route in case a address changes.

The interfaces that are included ar in a file named /etc/dhep/interfaces. in each line of the file is the interface name and options (in case needed), for example: weight:
Code:
eth0
#eth2
Here, eth2 is not included for the nexthop evaluation.

And for each interface, the IP address and the GW provided by DHCP are written to a file called /etc/dhep/interface_name.cfg:

Code:
date Tue Jun 28 12:30:44 VET 2005
IP 201.249.120.235
GW 201.249.120.1
By the way... if you think there are better ways to program the script, I'm eager to get them.

Thanks!

(I tried the trick writing down the variable value to a file, and it works!, but I'd like NOT to do it.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifying DISPLAY environment variable on-the-fly ktulu1115 Linux - Software 1 10-07-2005 02:18 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
Modifying the PATH environment variable brunnopessoa Linux - Newbie 11 11-03-2004 10:23 PM
Variable with bash pinkysioux Programming 6 04-15-2004 08:46 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration