»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2003, 06:02 PM   #1 (permalink)
Registered User
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 597
Ruler2112 is on a distinguished road
Kids Script Variables

I have a question for the people who know what they're talking about.

I'm writing a script. In it, I have a variable named ALLOWIP followed by a number. What I want to do is control the script based on if the variable has something in it or not and run a command (iptables if you must know) for every variable that isn't empty. The only problem is that I don't know how to evaluate a variable in a loop like this. I've done some testing and have so far:

for counter in 1 2 3
do
echo "$ALLOWIP$counter"
done


I'm planning on restructuring the loop as a while once it's done, but the concept is the same. I need to be able to concatenate a string with the contents of a variable, then use the result as the variable name and obtain the contents of that. Anybody know how to do such a thing?

__________________
About 5% of the people in the world can't think.
Another 5% can think and do.
The remaining 90% can think, but don't.
Ruler2112 is offline   Reply With Quote
Old 11-03-2003, 06:24 PM   #2 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
What shell are you using to interpret the script? Would I be correct in assuming bash?

String concat example..

foo="foo"
echo "bar"$foo

Will append the contents of the variable foo to the string bar, resulting in the output "barfoo".

Instead of echoing that you could assign it to another variable....

foo="foo"
bar="bar"$foo
echo $bar

I am not entirely sure what you are attempting to achieve, so some more details would be a great help.

Regards

ed

Last edited by SpookyEddy; 11-03-2003 at 06:31 PM.
SpookyEddy is offline   Reply With Quote
Old 11-03-2003, 09:39 PM   #3 (permalink)
Registered User
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 597
Ruler2112 is on a distinguished road
What I'm trying to do is have a section at the top of the script identifying what IPs should be allowed to connect to the machine. I have this like so:

ALLOWIP1="192.168.100.1"
ALLOWIP2="192.168.100.2"
ALLOWIP3="192.168.100.3"
ALLOWIP4="192.168.100.4"
ALLOWIPn="192.168.100.n"


What I want to be able to do is, within a loop so I don't have to cut & paste a plethora of if statements and iptables commands, access the value in ALLOWIP1 through n. I know the base name of the variable - $ALLOWIP - and the number - $counter - but don't know how to concatenate the $counter variable onto the string $ALLOWIP and access the value in the variable by that name.
__________________
About 5% of the people in the world can't think.
Another 5% can think and do.
The remaining 90% can think, but don't.
Ruler2112 is offline   Reply With Quote
Old 11-04-2003, 02:52 AM   #4 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
Would simply iterating over a list work for you? Something like this..
Code:
#!/bin/bash

list=("ip_one ip_two ip_three")

for i in $list
do
echo $i
done
Regards

ed
SpookyEddy is offline   Reply With Quote
Old 11-04-2003, 11:36 AM   #5 (permalink)
Registered User
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 597
Ruler2112 is on a distinguished road
That works perfectly- thanks dude. I've never seen that structure in a script before, but it does exactly what I need.
__________________
About 5% of the people in the world can't think.
Another 5% can think and do.
The remaining 90% can think, but don't.
Ruler2112 is offline   Reply With Quote
Old 11-04-2003, 11:42 AM   #6 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
Your welcome, glad it worked.
SpookyEddy is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 07:07 AM.