LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2012, 02:28 PM   #1
bellz4claire
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Question Rotating variables within a function?


I have a function where the variables will need to change depending on which choice the user makes.

Code:
#!/bin/bash

myfunction(){
echo "$variable1"
echo "$variable2"
echo "$variable3"

}
1st option - variable1="one", variable2="two", variable3="three"

2nd option - variable1="four", variable2="five", variable3="six"

3rd option - variable1="seven", variable2="eight", variable3="nine"

...and the choice needs to be within a case statement:

Code:
read choice
case $choice in
  1) myfunction (with option 1)
  2) myfunction (with option 2)
  3) myfunction (with option 3)
esac
Is this possible?

Last edited by bellz4claire; 09-29-2012 at 02:30 PM.
 
Old 09-29-2012, 03:04 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by bellz4claire View Post
Is this possible?
Sure, see if this hint helps:
Code:
myfunction(){ echo "You've got $# args and the value of \$1 is \"$1\"."; }
while read choice; do
case $choice in
  1|2|3) myfunction $choice;;
  *) break;;
esac; done
 
1 members found this post helpful.
Old 09-30-2012, 12:01 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I think you need to explain what you need in a bit more detail. I'm not sure I understand it. How about giving us some more context so we can understand your purpose?

If I follow you though, can you define the variables just before you call the function?

Code:
case $choice in

	1) variable1="one" variable2="two" variable3="three" 
	   myfunction
	;;
	2) variable1="four" variable2="five" variable3="six"
	   myfunction
	;;
	...etc...
esac
Or a similar selection process could be built into the function itself instead, and then you'd call it with different arguments to have it give you the output you want.
 
1 members found this post helpful.
Old 10-11-2012, 01:22 AM   #4
bellz4claire
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
I think you need to explain what you need in a bit more detail. I'm not sure I understand it. How about giving us some more context so we can understand your purpose?

If I follow you though, can you define the variables just before you call the function?

Code:
case $choice in

	1) variable1="one" variable2="two" variable3="three" 
	   myfunction
	;;
	2) variable1="four" variable2="five" variable3="six"
	   myfunction
	;;
	...etc...
esac
Or a similar selection process could be built into the function itself instead, and then you'd call it with different arguments to have it give you the output you want.
That worked perfectly, thank you.
 
  


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
[SOLVED] Use Variables as a function dellafaille Linux - Newbie 3 06-16-2012 07:20 AM
returning different types of variables from a function knobby67 Programming 3 02-08-2008 09:53 PM
JavaScript: function only taking constants, no variables. :/ RHLinuxGUY Programming 8 01-19-2007 03:49 AM
accessing variables inside a function bahadur Programming 1 03-27-2005 05:41 AM
Static Variables in C function swifty99uk Programming 4 04-14-2004 11:50 AM


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