LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 05-24-2002, 09:01 AM   #1
Noerr
Member
 
Registered: May 2002
Location: Dalec, HU
Distribution: Redhat 7.3
Posts: 696

Rep: Reputation: 30
Question bash for statement with 2 arrays?


Is it possible to use "for" statement and alter two variables ie if I want to echo last name of the username which has logged (i have array variable for the last-name and array variable for name of the person and they all have same positions in the array :
name=(John Frank Paul)
last-name=(J-lastn F-lastn P-lastn)

for user in $name ; done
echo "Hello $user ??$last-name??"
done

Basically I want to feed with two dimensional array, or find the way to show value from the same position of the second variable

The example is bogus.
 
Old 05-24-2002, 09:18 AM   #2
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Bash only works with unidimensional arrays.

Why do you want to echo the lst names?

Try something like:

NAMES="a b c"

for dude in $NAMES
do
echo "Howdy, $dude"
done

See? No complicated array commands, which, btw, in your example are wrong.

Check:

NAMES=([0]="a" [1]="b")

echo ${NAMES[0]}

(I'm not very sure, but the right syntax is something like this.)

HTH, sorry if not.

vfs
 
Old 05-24-2002, 10:26 AM   #3
Noerr
Member
 
Registered: May 2002
Location: Dalec, HU
Distribution: Redhat 7.3
Posts: 696

Original Poster
Rep: Reputation: 30
I found one way of doing it but only for numericals with C-like sytax ie:

for ((a=0,b=5; a < 10;a++, b++)); do
echo "$a some text $b)
done

But how to do this if i have strings in variables?? btw my example was bogus. I'm trying to find something useful from ABS (adv. Bash scripting guide)
 
Old 05-24-2002, 10:35 AM   #4
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
You quite enlightened me!

NAME=([0]="a" [1]="b")

SURNAME=([0]="a-a" [1]="b-b")

for ((i=0; a < ${NAME{[#]}}; i++))
do
echo "Hello ${NAME{[$i]}} ${SURNAME{[$i]}}"
done

I dunno if it's correct, but I think it's on the way


Check 'man bash' to see the corrrect array stuff, which I can't remind...

HTH

vfs
 
Old 05-24-2002, 10:37 AM   #5
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Change variable name "a" in loop for "i".

Sorry...

vfs
 
Old 05-24-2002, 11:12 AM   #6
Noerr
Member
 
Registered: May 2002
Location: Dalec, HU
Distribution: Redhat 7.3
Posts: 696

Original Poster
Rep: Reputation: 30
Yes I got it working but sytax is a little tricky
you have use syntax for arrays in form

${NAME[a]}
and then it works,
I didn't think of a < ${NAME[#]} (to get number of values in array)

This is really cool
thanks
 
Old 05-25-2002, 08:20 AM   #7
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
I have to confess: your problem is very challenging. I never thought about programming such thing...

But I went home, started Linux (I do most of my posts from Windows, at college), read 'man bash', and here's the code I could write for you:

8<---------- cut here

#!/bin/bash
# Valter Ferraz Sanches <vfs@mail.com>

NAME=([0]="John" [1]="Mary" [2]="Fred")
SURNAME=([0]="Doe" [1]="Poppins" [2]="Flintstone")

# this gets args from command line and add them to the arrays.
# it's NOT the Right Thing, but can give you a brief idea of what to do
# You may add options, like -name "list" -surname "list" to parse the
# correct elements and insert them in the appropriated array.
for i in $@
do
# this ugly stuff inside brackets counts the number of elements
# so every new element added increases array counter.
NAME[${#NAME[@]}]=$i
SURNAME[${#SURNAME[@]}]=$i
done

# starts loop at element 0 and stops at last.
for ((i=0; i < ${#NAME[@]}; i++))
do
# echo element number, name and surname.
echo "Howdy #$i: ${NAME[$i]} ${SURNAME[$i]}"
done

# HTH, vfs

--------------cut here >8

Cheers,

vfs
 
Old 05-26-2002, 04:48 AM   #8
Noerr
Member
 
Registered: May 2002
Location: Dalec, HU
Distribution: Redhat 7.3
Posts: 696

Original Poster
Rep: Reputation: 30
My reallife example is actually in iptables. I have a bunch of scripts for portforwarding, firewall .. and is very usefull to use "for" with 2 variables
ie
I would map ports for remote control for entire lan so I found a workaroud with this catch
Let say you have remote control waiting on port 3000 and I want to have access for all lan. So i map ports on my linux gateway

1.2.3.4:3011 to 192.168.1.11:3000
1.2.3.4:3012 to 192.168.1.12:3000 ...
so you see i'll take a variable to be last two digits of the ip/port


i=(11 12 13 ..)

for port in i; do
iptables -t nat -A PREROUTING -p tcp -d my.linux.gw.ip --dport 30$i -j DNAT --to 192.168.1.$i:3000
done

but it bothers me a little because i'm bound to same digits.
Your suggestions really helped me. But the first example is quicker.

Thanks again
 
Old 05-27-2002, 07:55 AM   #9
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
I'm not getting what you mean exactly (I'm quite a moron, so don't bother ). You want to change the IP numbers to scan, is it?

If so, try this (as always, I'm stuck in winblows... ):

#!/bin/bash
[ $# -ne 2 ] && echo "usage: $0 first-ip last-ip" && exit

IP_LIST=`seq $1 $2`

for i in $IP_LIST
do
: # code here
done

HTH. If not, post again

vfs
 
Old 05-27-2002, 12:14 PM   #10
Noerr
Member
 
Registered: May 2002
Location: Dalec, HU
Distribution: Redhat 7.3
Posts: 696

Original Poster
Rep: Reputation: 30
no no, don't worry I got it all working! I was just saying that i use it for port mapping in my gateway (for remote control) ie
I want to remote control from my home computer to work, where I have 192. class ips, so I'll map ports for remote control on my gateway so that ie
linux box port 3010 would map to local lan 192.168.1.10:3000
but in order to use "for " loop I needed (before you suggested me) use one number for my variable (10) so I would write like this

i=(10 11 ..)
-s 1.2.3.4 --sport 30$i -d 192.168.1.$i --dport 3000

but now I can use two variables ie
i=(3010 3011 ..)
j=(10 11 ..) (or use totally different numbers)
-s 1.2.3.4 --sport 30$i -d 192.168.1.$j --dport 3000

later
 
Old 05-27-2002, 01:58 PM   #11
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Oh, ok...

Cheers,

vfs
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash statement os2 Programming 2 03-20-2005 11:13 PM
how to close 'else' statement block in bash script servnov Linux - General 9 11-12-2004 04:53 PM
bash if statement question xscousr Programming 3 09-02-2003 12:58 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 10:35 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM


All times are GMT -5. The time now is 01:10 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration