Share variable between functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Share variable between functions
# 1  
Old 01-20-2014
Share variable between functions

Hi,

gtkdialog gui seperate buttons use seperate functions in their GUI. A variable (newFolderName) is being set in one function (funcA) and it needs to be called in another function (funcB). I thought using global variable (like in python) concept should work however it does not.

How else is it possible to do share variables between functions. ?

Example

Code:
funcA() {
	newFolderName=v01
}

funcB() {
	mkdir newFolderName
}

funcA and funcB are called as action to button

Code:
<button>
	<label>Confirm</label>
	<action>funcConfirmDisplayText</action>
</button>

<button>
	<label>Start</label>
	<action>funcStart</action>
</button>


Last edited by Corona688; 01-20-2014 at 01:06 PM..
# 2  
Old 01-20-2014
Quote:
Originally Posted by singhai.nish
Hi,

gtkdialog gui seperate buttons use seperate functions in their GUI. A variable (newFolderName) is being set in one function (funcA) and it needs to be called in another function (funcB). I thought using global variable (like in python) concept should work however it does not.

How else is it possible to do share variables between functions. ?

Example

funcA() {
newFolderName=v01
}

funcB() {
mkdir newFolderName
}

funcA and funcB are called as action to button

<button>
<label>Confirm</label>
<action>funcConfirmDisplayText</action>
</button>

<button>
<label>Start</label>
<action>funcStart</action>
</button>
change to mkdir $newFolderName
# 3  
Old 01-20-2014
Quote:
Originally Posted by Akshay Hegde
change to mkdir $newFolderName

Oops that is already true ( it is actually mkdir $newFolderName )
# 4  
Old 01-25-2014
You can always pass a variable to a function:

Code:
funcA(){
  echo "Hello $1"
  funcB v01-$1
}
funcB(){
  mkdir $1
}
funcA Singhai

Will echo "Hello Singhai" and create a folder named "v01-Singhai"

When you define a variable prior to functions, that value will be used.
However, if you define a variable with the same name within a function, that value will remain inside the function.

Not sure if 'special' shopt settings are required or if its regular behaviour.

Last edited by sea; 01-25-2014 at 10:40 PM..
# 5  
Old 01-26-2014
Longhand on OSX 10.7.5, default bash terminal...

Not sure if this is what you are after:-
Code:
Last login: Sun Jan 26 09:38:30 on ttys000
AMIGA:barrywalker~> FuncA() { text="This is function A with a variable..."; }
AMIGA:barrywalker~> FuncB() { eval $1; echo "$text"; }
AMIGA:barrywalker~> FuncB FuncA
This is function A with a variable...
AMIGA:barrywalker~> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Source functions from variable

so i found a way to do this. #!/bin/bash MYF=$(/home/jason/myfunctions) source <(printf '%s\n' "${MYF}") echo "${var1}" /home/jason/myfunctions is a script that outputs information..i.e. functions...etc that needs to be sourced. I have no control over /home/jason/myfunctions. i can... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Source functions from variable

I have a functions file containing functions: file.functions: myTest () { echo "MYPWD=$(echo $(pwd))" echo "MYLOGNAME=${LOGNAME}" } myCar () { echo "MYDATE=$(date)" echo "MYUPTIME=$(uptime)" } i know i can source this file in another script using something similar to this: source... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

Local variable in functions (gawk)

Hi Everybody :) I need your help, because i know a local variable in a function for example k, it is different of other variable(with the same name k) this a global variable. Is that right? dgawk> run Starting program: 3238860128818202 3 4 7 11 12 13 17 22 23 32 35 37 41 48 49 55 63 ... (5 Replies)
Discussion started by: solaris21
5 Replies

4. Shell Programming and Scripting

Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background. Here is my example $ cat sample_program.sh #!/bin/ksh exec_func () { sleep 1 v=`expr $1 + 100` print $v } export OUT_ARR date for i in 1 2 do OUT_ARR=`exec_func $i` &... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

5. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

6. Shell Programming and Scripting

Help with data reformat if share share content

Input data: read1_data1 read1_data1 read2_data1 read3_data1 read4_data1 read4_data1 read4_data1 read5_data1 . . Desired output result: read1_data1 read1_data2 read2_data1 read3_data1 read4_data1 (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. UNIX for Dummies Questions & Answers

Help with functions

Hi, I am exploring with defining functions in my BASH shell scripts. However, I am bit confused about how to pass parameters to my functions. I was under the impression that you must do something like the following: Define a function called "sample_function": function sample_function {... (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies

9. Shell Programming and Scripting

functions in

hi could anybody please suggest me how to put a function memory for particular user. say i am a user rao. want have a function foo in memory . i have done this .typed the function function in the shell it worked for the session.but next time i do login its not there . i can i have a... (6 Replies)
Discussion started by: Raom
6 Replies

10. Shell Programming and Scripting

Regarding functions

Hi, I have a function or script like this. show() { echo "Hi" } | tee -a log show This creates a logfile and prints Hi in it. Now when I try to do the same for sql like this: show() { sqlplus -s scott/tiger<<! select * from details; ! } | tee -a log show Then it gives me a... (2 Replies)
Discussion started by: sendhilmani
2 Replies
Login or Register to Ask a Question