[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

bash function variable questions

Posted on 2006-04-27
15
Medium Priority
?
538 Views
Last Modified: 2012-06-22
Ok here is the problem

declare -i number=10

number=10

fuction addtonumber{
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

echo $number #number =10
addtonumber
echo $number #number=10   <------ THIS IS THE PROBLEM IT SHOULD BE 11 AFTER THE FUNCTION ADDS ONE TO IT

My actual situation is a little more complicates as I am piping standard input from sort into the function.
I don't really want to pass parameter to the functions - actually I tried that and I had a little trouble with it

Any suggetions?

Thanks
jculkincys







0
Comment
Question by:jculkincys
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 2

Expert Comment

by:guruyaya
ID: 16553600
first, notice this litle mistake
let numbet+=1
              ^

You`ve made this error here, you could have made it in your script.

Yet, if it`s not that, could be that you need to write  export number, after declaring it, though it worked for me without this change.
Btw - Have you declared #!/bin/bash?

0
 
LVL 2

Author Comment

by:jculkincys
ID: 16553660
Hello guruyaya

Yes I have declared #!/bin/bash

I doubled checked and I didn't have an actual spelling mistake in my script - tha t would have been nice though

I will try export

Do you think this could be an issue of Bash versions acting differently?

I am using BASH 2.05

jculkincys
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16553725
Hmm export doesn't seem to work
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 2

Author Comment

by:jculkincys
ID: 16553809


fuction addtonumber{
declare -i number=10
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

addtonumber
echo $number #number=10  <---- this produces the error "unbound variable"

I was under the impression that unless you specify "local" all variable are declared globally

This is leading me to belive that its a problem with my version of bash
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16553957
I typed very bad in that example

here is that example that I tested

#!/bin/bash
fuction addtonumber{
declare -i number=10
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

addtonumber
echo $number #number=10    #<----in Bash 3.1.11 I get number =11 in Bash 2.05 I get 11 as well so its not a version problem

Things that make you go Hmmmmmm......

0
 
LVL 2

Author Comment

by:jculkincys
ID: 16554108
Alright I finally recreated the problem in a useful manner for others

#!/bin/bash

declare -i number=10

function addtonumber {
echo "Value after entering function: $number"
pushd /bin
let number+=1
popd
echo "Value after addition in function: $number"
}

echo "Value before function: $number"
echo "TEST" | addtonumber
echo "Value after functin: $number"  #<----- number will =10 but I want it to be 11
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1600 total points
ID: 16554507
a function in bash (most ksh too) is something like a subshell with its own namespace, hence you cannot manipulate other (in particular global) variables there

you have do do something like:

function incr {
let x=$1+1
echo $x
}
x=`incr $x`;
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16554525
BTW,
x=`expr $x '+' 1`

# works in any shell ;-)
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16555164
I am not sure I understand
I think its something to do with my piping to the fuction because the following script works correctly (and it does not pipe to the function)

#!/bin/bash

declare -i number=10

function addtonumber {
echo "Value after entering function: $number"
let number+=1
echo "Value after addition in function: $number"
}

echo "Value before function: $number"
addtonumber
echo "Value after functin: $number"  #<-----number will be 11



It seems from this example that a function change the value of a global variable
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16555266
the only difference to your initial question is that you miss

number=10

could that be the problem?
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16555300
I just tested it the last script including number=10 and had no luck
0
 
LVL 16

Expert Comment

by:xDamox
ID: 16555391
Hi,

This works fine:


#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number
let "number += 1"
echo "Inside: " $number
}

echo $number
addtonumber
echo $number


The output form the above I get:

10
Inside:  10
Inside:  11
11

:D I noticed your let was not in quotes :)
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16555543
Thanks for joining xDamonX

Here is where I have the problem though

#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number
let "number += 1"
echo "Inside: " $number
}

echo $number
echo "Test" | addtonumber  # <------ Pipe to function creates the problem
echo $number



The output form the above I get:

10
Inside:  10
Inside:  11
10


I know the pipe plays no role in example function but it does in my script

0
 
LVL 16

Assisted Solution

by:xDamox
xDamox earned 400 total points
ID: 16556679
Hi,

How come your using pipe you can have the following:





#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number " going to add $1 to $number"
let "number += $1"
echo "Inside: " $number
echo "The number sent over is: " $1
}

echo $number
addtonumber 1  # send one over to the function
echo $number


0
 
LVL 2

Author Comment

by:jculkincys
ID: 16557662
I'm sorry xDamonx I don't think I described the problem well enough

I really didn't need to do the pipe in the example but by real script needed it for something else
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question