functions and variables in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting functions and variables in bash
# 1  
Old 09-11-2012
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.

Code:
setup(){

  none=0; low=1; medium=2; high=3; debug=4

  var1="red"
  var2="fred"

}

create_basemap() {
   
 xminBse=$(( $xmin - 2.0 ))   
 xmaxBse=$(( $xmax + 2.0 ))   
 zminBse=$(( $zmin - 2.0 ))     
 zmaxBse=$(( $zmax + 2.0 )) 
  
 xpos=0   
 ypos=0   
 xpos=$xmin   
 ypos=$(( $zmax + 1.5 ))   
 title="Sound speed, c, Mm/min"   
 xDesc="Distance, x, Mm"   
 yDesc="Depth, z, Mm" 
  
 # Scale attributes   xpos=$(( $wtimap + 0.5 ))    
 ypos=$(( $htimap / 2.0 ))   
 sclen=$htimap   scwid=0.4   

 # Create the grid   
 xyz2grd $f.xzc -G$f.grd -I$vdx/$vdz -R$xmin/$xmax/$zmin/$zmax   # Create the color palette   makecpt -C$color -T$cmin/$cmax/0.005 -Z > $f.cpt   

 # Create the basemap    
 wtbmap=$(( $wtimap + 6 ))   
 htbmap=$(( $htimap + 5 ))   
 psbasemap -JX$wtbmap/$htbmap -R0/$wtbmap/0/$htbmap -B0 -K > $f.ps
}

create_contour() {
                           
 textPosx=`echo $xminBse | awk '{print $1 + 1.2}'`   
 textPosy=`echo $zmaxBse | awk '{print $1 - 1.2}'`   
 txt="$textPosx $textPosy 12 0.0 0 BL $fullname"   
 echo $txt | pstext -JX$wtbmap/$htbmap -R$xminBse/$xmaxBse/$zminBse/$zmaxBse -Wwhite -O -K >> $f.ps   
 # Display the image    
 grdimage $f.grd -JX$wtimap/$htimap -R$xmin/$xmax/$zmin/$zmax -C$f.cpt  -B$ax:"$xDesc":/$az:"$yDesc"::."$title":WSne -X2.5 -Y2 -O -K >> $f.ps   

 # Can use units on the contours if needed   
 units="Mm/min"   
 units=""   

 # Annotate contour lines every ${labeldistx} cm   
 contLabelDist=`echo $wtimap | awk '{print $1/2}'`   
 contLabelDist="d${contLabelDist}c"   

 # Include contour lines with annotations   
 grdcontour $f.grd -C$ctc -JX -A$cta+v+u$units -G$contLabelDist -B -R -O -K >> $f.ps  

}


Last edited by kristinu; 09-11-2012 at 04:46 PM..
# 2  
Old 09-11-2012
Yes you can, as long as you don't use "local" for the variables (and of course if you call "setup" before other uses of the variable).
# 3  
Old 09-11-2012
That's good. So now I consider the following toy problem. I have two bash scripts as follows

Code:
cat colors.bash

#!/bin/bash  

colors() {   
  red="red"  
  blue="blue"   
  green="green" 
} 

car() {
  color_car="$red"
} 

pen() {
  pen_color="$blue"
}

Code:
cat property.bash  

source colors.bash  
my_property() {
  echo "$color_car"
}

I want to have the function my_property returning red. My problem is that I do not want to call the function colors inside car before setting
Code:
color_car="$red"


Last edited by kristinu; 09-11-2012 at 06:45 PM..
# 4  
Old 09-11-2012
Don't call that function, then.
# 5  
Old 09-11-2012
Ok, so I just call colors in my_property, which seems to work

Code:
source colors.bash  
my_property() {
  colors
  car
  echo $color_car
 }

---------- Post updated at 04:53 PM ---------- Previous update was at 04:44 PM ----------

My problem is this:

Suppose I have lots of properties which all call colors

Code:
pen
car
box
ball
plate

and then I start calling pen and car several times in my_property, this would mean that all colors would be redefined everytime which is a waste

Here is an example

Code:
cat colors.bash

#!/bin/bash  

colors() {   
  red="red"  
  blue="blue"   
  green="green" 
} 

car() {
  echo $red
} 

pen() {
   echo $blue
}

ball() {
    echo $blue
 }
 
plate() {
   echo $blue
  }

Code:
cat property.bash

#!/bin/bash  

my_property() {
  colors

  car
  car
  car
  plate
  echo $red
 
}


Last edited by kristinu; 09-11-2012 at 07:04 PM..
# 6  
Old 09-11-2012
As the last line in colors.bash you can call the function colors.
# 7  
Old 09-11-2012
Even if the functions are declared before calling colors?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash functions sequence ?

OK, I know function has to be defined first - in sequence - before it can be used. So the script has to be build "bottoms -up style, if you pardon my expression. I am running into a problem reusing function and breaking the sequence. It would be nice to be able to see the function... (10 Replies)
Discussion started by: annacreek
10 Replies

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

3. Shell Programming and Scripting

Calling Bash Functions in the BG

I'm trying to call some functions in the background so that I can multitask in this script. Not working so hot. The functions I call don't ever seem to get called. I'm doing it the exact same way in another script and it's working like a champ so I'm very confused. Here's a pretty simple repro: ... (7 Replies)
Discussion started by: stonkers
7 Replies

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

5. Shell Programming and Scripting

bash functions arguments

This script is called fuu; #!/bin/bash speak() { case $1 in 1)echo one ;; 2)echo two ;; 3)echo three ;; esac } speak exit 0 when i run fuu 2 i expect "two" like... (2 Replies)
Discussion started by: Tártaro
2 Replies

6. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 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. 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

10. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: dfb500
4 Replies
Login or Register to Ask a Question