LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-02-2007, 05:25 AM   #1
Antimatter
Member
 
Registered: Jul 2003
Posts: 37

Rep: Reputation: 15
Bash script is being captious, needs help


I've checked the tutorials but i keep on struggling with this bash script I wrote, for some reason bash is thinking some variables that I defined are function and complains that it does not exist, and if I prefix it with a let, it complains about = something....

Anyway here's the offensive bash script below...
Code:
#!/bin/sh
#
# This currently will only activate for eth1, to test this functionality
#

# Echo and log the $reason value being given
echo 'dhclient-exit-hooks: ' $reason
logger 'dhclient-exit-hooks: ' $reason

# Programs
IPTABLES="/sbin/iptables"

# Enable Ip_forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward


# Script variables
eth0_oldFile="/var/run/eth0.oldIP"
eth1_oldFile="/var/run/eth0.oldIP"

oldIP=""
oldRoute=""

newIP=""
newRoute=""

get_old_variables () {

        # Check which interface is being called
        if [[ "$interface" == "eth0" ]]; then 

                # Check if the eth0_oldFile file exists
                if [[ -e $eth0_oldFile ]]; then
                        # it exists, so load old IP and router address
                        $oldIP = `cat $eth0_oldFile | awk '{print $1}'`
                        $oldRoute = `cat $eth0_oldFile | awk '{print $2}'`
                else
                        # It does not exist, create it and place the
                        # newIP & newRoute into it
                        echo $newIP $newRoute > $eth0_oldFile
                fi

        elif [[ "$interface" == "eth1" ]]; then

                # Check if the eth1_oldFile file exists
                if [[ -e $eth1_oldFile ]]; then
                        # it exists, so load old IP and router address
                        $oldIP = `cat $eth1_oldFile | awk '{print $1}'`
                        $oldRoute = `cat $eth1_oldFile | awk '{print $2}'`
                else
                        # It does not exist, create it and place the
                        # newIP & newRoute into it
                        echo $newIP $newRoute > $eth1_oldFile
                fi
        fi


echo 'dhclient-exit-hooks: (get_old_variables): ' $interface
logger 'dhclient-exit-hooks: (get_old_variables): ' $interface

echo 'dhclient-exit-hooks: (get_old_variables): ' $oldIP
logger 'dhclient-exit-hooks: (get_old_variables): ' $oldIP

echo 'dhclient-exit-hooks: (get_old_variables): ' $oldRoute
logger 'dhclient-exit-hooks: (get_old_variables): ' $oldRoute
}

flush_NAT () {
        $IPTABLES -P FORWARD ACCEPT
        $IPTABLES -t nat -P PREROUTING ACCEPT
        $IPTABLES -t nat -P POSTROUTING ACCEPT
        $IPTABLES -t nat -F
        $IPTABLES -t nat -X

echo 'dhclient-exit-hooks: (flush_NAT)'
logger 'dhclient-exit-hooks: (flush_NAT)'
}

create_NAT () {
        $IPTABLES -t nat -A PREROUTING -d $newIP -j DNAT --to-destination 172.20.0.3
        $IPTABLES -t nat -A POSTROUTING -s 172.20.0.3 -j SNAT --to-source $newIP

echo 'dhclient-exit-hooks: (create_NAT)'
logger 'dhclient-exit-hooks: (create_NAT)'
}

flush_routing () {
        ip rule del from 172.20.0.3
        ip route flush table 101

echo 'dhclient-exit-hooks: (flush_routing)'
logger 'dhclient-exit-hooks: (flush_routing)'
}

create_routing () {
        ip rule add from 172.20.0.3 table 101
        ip route add default via $newRoute dev eth1 table 101

        ip route flush cache

echo 'dhclient-exit-hooks: (create_routing)'
logger 'dhclient-exit-hooks: (create_routing)'
}

update () {

        # This assumes the old ip/routing and new ip/routing is already set
        # Check to see if they are not equal, if not equal then process it
        if [[ "$newIP" != "$oldIP" || "$newRoute" != "$oldRoute" ]]; then

                # the IP or routing is not the same so update the NAT and routing
                flush_NAT
                flush_routing

                create_NAT
                create_routing

                # Update the oldFile
                if [[ "$interface" == "eth0" ]]; then 
                        echo $newIP $newRoute > $eth0_oldFile
                elif [[ "$interface" == "eth1" ]]; then
                        echo $newIP $newRoute > $eth1_oldFile
                fi
        fi

echo 'dhclient-exit-hooks: (update): ' $newIP $oldIP
logger 'dhclient-exit-hooks: (update): ' $newIP $oldIP

echo 'dhclient-exit-hooks: (update): ' $newRoute $oldRoute
logger 'dhclient-exit-hooks: (update): ' $newRoute $oldRoute
}



# This block determite which $reason code is passed to this script
case "$reason" in

        "MEDIUM" | "PREINIT" | "TIMEOUT" ) # Ignore, not sure what to do with "TIMEOUT" but ignore for now
                exit 0
        ;;

        "EXPIRE" | "FAIL" ) # flush NAT and routing
                flush_NAT
                flush_routing
        ;;

        "BOUND" | "RENEW" | "REBIND" | "REBOOT" ) # Do some processing
                # Assign the IP/Routes to proper variables
                $newIP = $new_ip_address
                $newRoute = $new_routers

                if [[ "$interface" == "eth1" ]]; then
                        get_old_variables
                        update
                fi
        ;;

esac
I've tried a dozen thing for the stupid variable assignment

I've tried:
Code:
newIP = $new_ip_address
newIP = "$new_ip_address"
newIP = `echo $new_ip_address'
$newIP = $new_ip_address
$newIP = "$new_ip_address"
$newIP = `echo $new_ip_address'

let newIP = $new_ip_address
let newIP = "$new_ip_address"
let newIP = `echo $new_ip_address'
let $newIP = $new_ip_address
let $newIP = "$new_ip_address"
let $newIP = `echo $new_ip_address'
And every single time without fail, bash screws up, here's an example of its screwup

Code:
 * Stopping eth1
 *   Bringing down eth1
 *     Stopping dhclient on eth1 ...                                                                            [ ok ]
 *     Shutting down eth1 ...                                                                                   [ ok ]
 * Starting eth1
 *   Bringing up eth1
 *     dhcp
 *       Running dhclient ...
dhclient-exit-hooks:  PREINIT
dhclient-exit-hooks:  REBOOT
/etc/dhcp/dhclient-exit-hooks: line 149: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 150: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 48: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 49: =: command not found
dhclient-exit-hooks: (get_old_variables):  eth1
dhclient-exit-hooks: (get_old_variables): 
dhclient-exit-hooks: (get_old_variables): 
dhclient-exit-hooks: (update): 
dhclient-exit-hooks: (update):                                                                                  [ ok ]
 *       eth1 received address 192.168.60.2/24
 
Old 08-02-2007, 05:33 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Get rid of those spaces.
Code:
$ foo=2       -- assign a number to a variable
$ echo $foo
2
$ bar=$foo    -- assign a value to a variable
$ echo $bar
2
$ bar = 7     -- this is what you did
bash: bar: command not found
$ bar=7
$ echo $bar
7
... now do you understand?

Last edited by Simon Bridge; 08-02-2007 at 05:37 AM.
 
Old 08-02-2007, 05:42 AM   #3
Antimatter
Member
 
Registered: Jul 2003
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Simon Bridge
Get rid of those spaces.
Code:
$ foo=2       -- assign a number to a variable
$ echo $foo
2
$ bar=$foo    -- assign a value to a variable
$ echo $bar
2
$ bar = 7     -- this is what you did
bash: bar: command not found
$ bar=7
$ echo $bar
7
... now do you understand?
Huh? Its that simple?

Its amazing how such a simple thing as spaces tripped me up, I've been occasionally writing bash scripts, and for some reason I never had an issue with that little tiny gotcha...

Well, thanks so much for correcting my oops!
 
Old 08-02-2007, 06:55 PM   #4
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Yeah - bash thinks everything up to the first space is a command, so it never saw the = sign in your script.

Happens to the best of us - especially when we change between script types.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 05:18 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:21 PM.

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