UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old December 4th, 2004, 03:25 PM
Nevermore84 Nevermore84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 Nevermore84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
need KSH help! passing variables to functions

okay, I have the following code. (korn shell script in unix).

function abc
{
print $1
}

#main starts here

for((i=1; i<$#; i++))
do
abc $i
done
#end of program

#the $# variable is a count of the number of vars #passed to main from the command line.

#here, $i is the first command line variable. I want to #pass that variable to function abc. then i want the
#value of $2, then value of $3, etc.

when i do this, it actually passes $i rather than the value of $i. i have tried every type of syntax modification to the function call, but can't seem to make this work. any help would be GREATLY appreciated. thank you!

Reply With Quote
  #2  
Old December 5th, 2004, 06:34 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
I don't think you understand how variables work. If you do:

i="hello"
echo $i

The echo statement is print "hello". If you put a statement like
hello="good-bye"
in front of it, that won't change anything

Reply With Quote
  #3  
Old December 5th, 2004, 07:41 PM
Nevermore84 Nevermore84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 Nevermore84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I understand that. im just not sure how i would pass the value of $1 for example (first iteration of loop), to function abc.... i realize that abc $i will not work, and i understand why, but i don't know what to use in its place.

Reply With Quote
  #4  
Old December 5th, 2004, 08:39 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
I would do something like this:

function abc
{
print $1
}

#main starts here

while(($#))
do
abc $1
shift
done

Reply With Quote
  #5  
Old December 6th, 2004, 09:55 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Smile The computer is allways right

Quote:
Originally Posted by Nevermore84
okay, I have the following code. (korn shell script in unix).

function abc
{
print $1
}

#main starts here

for((i=1; i<$#; i++))
do
abc $i
done
#end of program

#the $# variable is a count of the number of vars #passed to main from the command line.

#here, $i is the first command line variable. I want to #pass that variable to function abc. then i want the
#value of $2, then value of $3, etc.

when i do this, it actually passes $i rather than the value of $i. i have tried every type of syntax modification to the function call, but can't seem to make this work. any help would be GREATLY appreciated. thank you!


You have instructed the system to display numbers 1, 2, 3, etc and it has done so.

Write

eval abc \$$i

instead of

abc $i

Regards zlutovsky

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > KSH help! passing variables to functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway