Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


LinkBack Search this Thread
Old 02-20-2009, 12:54 PM   #1
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.

IPs= 192.168.x.x 192.168.x.y etc etc

for ip in $1
echo $ip
It just print the first one not more.

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



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

Rep: Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911
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
for ip in "$@"
  echo $ip
Also in a variable assigment you cannot put spaces immediately after the equal sign:
IPs=$(awk blah blah blah)

Last edited by colucix; 02-20-2009 at 01:01 PM.
Old 02-20-2009, 01:08 PM   #3
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

Old 02-21-2009, 01:02 PM   #4
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,520
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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".

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

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'"




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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 01:17 AM.

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