Variables and functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables and functions
# 1  
Old 05-21-2007
Variables and functions

I have a script that ultimately will FTP data to certain directories based on a character in the filename. I am creating a function within my script to handle the FTP call. Are the variables that are setup in the original script accessible to the function? If not, is there a way to allow them to be referenced? I want to echo the results of the ftp process to a log and this variable was setup in the calling portion of the script.

If the answer is no do I need to pass all these as arguments to the function?

Thx
# 2  
Old 05-21-2007
Variables define in main part of your script are accessible inside functions (in read/write mode).
You can also define variables locales to you function.

See the following example :
Code:
my_function ()
{
   echo "my_function: begin"
   local work_var="work_var from my_function"
   echo "  main_var=$main_var"
   echo "  work_var=$work_var"
   main_var="new value from my_function"
   echo "my_function: end"
}
work_var="work main value"
main_var="value from main"

echo "main: Before my_function call"
echo "  work_var=$work_var"
echo "  main_var=$main_var"

my_function

echo "main: After my_function call"
echo "  work_var=$work_var"
echo "  main_var=$main_var"

Output:
Code:
main: Before my_function call
  work_var=work main value
  main_var=value from main
my_function: begin
  main_var=value from main
  work_var=work_var from my_function
my_function: end
main: After my_function call
  work_var=work main value
  main_var=new value from my_function

Jean-Pierre.
# 3  
Old 05-21-2007
Thanks, this will seem like a silly question but I am new to this whole scripting business. If the function is contained in the same file as the calling script the function would need to be defined before the actual call correct? I assume that the shell reads the script one line at a time.

ty
# 4  
Old 05-21-2007
You're right, a function must be declared before the call.

For example:
Code:
echo "Function call before definition..."
my_function

my_function()
{
   echo Inside function
}

echo "Function call after definition..."
my_function

Output:
Code:
Function call before definition...
demo.sh: line 2: my_function: command not found
Function call after definition...
Inside function

Jean-Pierre.
# 5  
Old 05-21-2007
Thx aigles, this gets me started
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

4. 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

5. Shell Programming and Scripting

functions and variables in bash

I have a bash script with some functions as below and am wondering if I can use the variables declared in setup in the other functions and in the rest of the bash script. setup(){ none=0; low=1; medium=2; high=3; debug=4 var1="red" var2="fred" } create_basemap() { ... (7 Replies)
Discussion started by: kristinu
7 Replies

6. Shell Programming and Scripting

variables and functions

can somebody telll me what my values are not being displayed in the function func1() {oracle@im4s012nz1_DUMMY}$ cat x1.ksh #!/bin/ksh getpwd() ( set -x . /home/oracle/dba/bin/CyberArk/CyberArk_GetPass.ksh ORA_USER=$DB_UID ORA_PWD=$DB_PWD echo "Here I am 1... (6 Replies)
Discussion started by: BeefStu
6 Replies

7. Shell Programming and Scripting

[bash] reassigning referenced variables in functions

Hello all, Problem. ---------- I'm trying to reassign a referenced variable passed to a 'local' variable in a function but the local variable refuses to be assigned the content of the referenced variable. Any ideas would be appreciated. Objective. ----------- Eliminate all $VAR... (1 Reply)
Discussion started by: ASGR
1 Replies

8. Programming

Trouble with Date Variables and Functions in PL/SQL

Hi, In the course of my script i have to compare SYSDATE with the 15th of the current month: if it is greater than i should set a variable date to 15th of the next month if less than i should set it to the 15th of the current month. In other words the question is how to set a date variable... (2 Replies)
Discussion started by: fmina
2 Replies

9. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

10. Shell Programming and Scripting

awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix. Insight would be appreciated money=$1 rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table) rate2=$(awk -F"\t"... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies
Login or Register to Ask a Question