bash function variable questions

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







LVL 2
jculkincysAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guruyayaCommented:
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?

jculkincysAuthor Commented:
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
jculkincysAuthor Commented:
Hmm export doesn't seem to work
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jculkincysAuthor Commented:


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
jculkincysAuthor Commented:
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......

jculkincysAuthor Commented:
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
ahoffmannCommented:
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`;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
BTW,
x=`expr $x '+' 1`

# works in any shell ;-)
jculkincysAuthor Commented:
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
ahoffmannCommented:
the only difference to your initial question is that you miss

number=10

could that be the problem?
jculkincysAuthor Commented:
I just tested it the last script including number=10 and had no luck
xDamoxCommented:
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 :)
jculkincysAuthor Commented:
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

xDamoxCommented:
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


jculkincysAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.