LinuxQuestions.org
Register a domain and help support LQ
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 02-20-2009, 12:54 PM   #1
CarLost
Member
 
Registered: Jun 2004
Location: Sentado en mi trasero en Chile
Distribution: ArchLinux
Posts: 45

Rep: Reputation: 16
bash: list vars and function


Hi everyone!!!

Sorry for the noob question but.. I'm stuck with this.

I'm trying to pass a list of IPs address into a function, but I can't pass trough.

Code:
IPs= 192.168.x.x 192.168.x.y etc etc

tEST()
{
for ip in $1
echo $ip
}
tEST $IPs
It just print the first one not more.

I don't know how to pass the entire list to the function...

HELP

thanks

PS: The IPs list is get by working on a file..
Code:
IPs= $(awk -F \; '{print $7}' $list | sed s/,/./g | sed 1d | sed 's/.$//' )
 
Old 02-20-2009, 12:59 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Apart the missing do/done statements in the for loop, you loop over $1 which is the first argument. Try to loop over all the arguments passed to the tEST function as in
Code:
for ip in "$@"
do
  echo $ip
done
Also in a variable assigment you cannot put spaces immediately after the equal sign:
Code:
IPs=$(awk blah blah blah)

Last edited by colucix; 02-20-2009 at 01:01 PM.
 
Old 02-20-2009, 01:08 PM   #3
CarLost
Member
 
Registered: Jun 2004
Location: Sentado en mi trasero en Chile
Distribution: ArchLinux
Posts: 45

Original Poster
Rep: Reputation: 16
Thanks a lot....

About the do and done, sorry just write to fast to remember transcribe this....

And the space..... the same

The fact is I miss the "$@" thing... I'm fully grateful

Thanks a lot.... This is what I miss

Bye
 
Old 02-21-2009, 01:02 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hello CarLost

It's not just that you can't put whitespace (tabs, spaces and newlines) after an assigmnent =, you can only put a single "word".

Example:
x=1 2 3 4
This means "assign 1 to variable x then run command 2 with arguments 3 and 4. Weird, huh? Why did they design the shell to work like that?

If you do want to assign a value that includes whitespace to a variable you need to quote it;
x='1 2 3 4'

This is powerful; for example it allows you to assign carriage return to a variable, like this
x='
'

BTW, if you want to see what's in a variable it's helpful to use something like this
echo "'$x'"

Putting it in " quotes allows the shell to "expand" $x, replacing it with its value. Try
echo '$x'

Putting ' quotes inside the " quotes lets you see any whitespace at the beginning and especially end of the value. Try
x=' 1234 '
echo $x
echo "$x"
echo "'$x'"

Best

Charles
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case statements and vars for bash script geech Programming 1 01-09-2009 04:49 PM
Perl with bash environment vars rose_bud4201 Programming 6 06-21-2006 12:19 PM
Bash Commands List by Function? TBennettcc Linux - Newbie 6 01-06-2006 02:08 AM
N00b: Simple Question about Bash Func and Vars: TylerD75 Programming 6 04-03-2005 06:03 AM
bash env vars alias's & .bashrc micxz Linux - General 8 10-08-2003 02:09 PM


All times are GMT -5. The time now is 10:57 PM.

Main Menu
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