LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-03-2005, 04:35 AM   #1
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 94

Rep: Reputation: 17
N00b: Simple Question about Bash Func and Vars:


Hi!
I'm kind of a n00b at bash scripting (read: long time since my last bash script), and have a (probably) simple Q:

I've made a script with some functions but I'm unable to catch their return value...
Example:
------------------------------------------------------
getNumInst() {
ps auxww | grep azureus | while read OUTP1
do
let "i+=1"
nrInst=$i
done
return $nrInst
}

instances=$(getNumInst)
echo "Instances of Azureus: $instances"
-------------------------------------------------------
Before you ask why I do this as a function: I've also tried it as a simple while loop, but with the same result.

I guess its got something to do with integer values in strings, or something...
But the strange thing is that if I put an echo "Count: $nrInt" before "done", it will show the current value of $nrInst. But if I put it after done, there's no value (as in "" or null). I've also tried "declare nrInst" and "declare -i i" without a positive result...
So what am I doing wrong? Can someone please help me with this?

PS! I'm trying to write this because every now and then Azureus shuts down, and I don't want to check it and restart it every time it happens...
 
Old 04-03-2005, 04:39 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Would this do?

[ j@fulltiltboogie ][ 04-03 05:39 ][ ~ ]
:nrInstances=`ps auxww | grep kdeinit | wc -l`

[ j@fulltiltboogie ][ 04-03 05:40 ][ ~ ]
:echo $nrInstances
24

Not sure what's wrong with the first example and this seemed easier than figuring it out.
 
Old 04-03-2005, 04:58 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
In your original script, replace "return" by "echo" and it works.
Return if for returning a numerical status: 0=OK, other=error code.
 
Old 04-03-2005, 05:13 AM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Code:
getNumInst() {
    ps auxww | grep azureus | while read OUTP1
    do
        let "i+=1"
        nrInst=$i
    done
    return $nrInst
}

instances=$(getNumInst)
echo "Instances of Azureus: $instances"
This is wrong. This looks like C. Not bash. This is not the way functions return values in bash.

In bash the returning value is stored in the special variable "$?"

SO instead of this:

Code:
instances=$(getNumInst)
echo "Instances of Azureus: $instances"
Try this:
Code:
getNumInst
instances=$?
echo "Instances of Azureus: $instances"
Also when I declare a function I put the reserved word function in the beginning:
function getNumInst() {.......................

but this is optional i think....

Last edited by perfect_circle; 04-03-2005 at 05:15 AM.
 
Old 04-03-2005, 05:15 AM   #5
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 94

Original Poster
Rep: Reputation: 17
Thanks for the quick reply!
Oops! I read about that too... Lol I've been up too late I think!

I'll try that new solution!


Last edited by TylerD75; 04-03-2005 at 05:19 AM.
 
Old 04-03-2005, 05:22 AM   #6
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 94

Original Poster
Rep: Reputation: 17
Thanks M8! That did solve my problem! And thanks to the first reply as well! It did simplify my solution!
 
Old 04-03-2005, 06:03 AM   #7
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 94

Original Poster
Rep: Reputation: 17
Another related question:
Is it at all possible to return a string from a function?

Update:
Never mind that last Q, Jlliagre solved it for me!!
Thanks again!

Last edited by TylerD75; 04-03-2005 at 06:05 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between function declaration: func() and func(void) koyi Programming 4 11-19-2005 10:19 AM
Simple N00b Question snowboard4fod Linux - Newbie 9 10-08-2005 01:18 AM
A Simple n00b question - assigning your system a name TuxFreak Fedora 9 12-02-2004 04:40 PM
bash env vars alias's & .bashrc micxz Linux - General 8 10-08-2003 02:09 PM
simple samba n00b question gn000we Linux - Networking 1 03-19-2003 01:14 PM


All times are GMT -5. The time now is 10:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration