LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 05-03-2010, 01:40 AM   #1
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
bash functions - problem with return value


I am trying to get pretty much the same thing as a:
du -bc | grep total | cut -f1

but using a recursive function in bash (learning exercise).

Trying to figure out what I did wrong.

Here is what I've written:
Code:
#!/bin/bash

#du -bc | grep total | cut -f1

function calcul
{
#    echo $1
    local sum=0

    sum=$(($sum+$(ls -l $1 | cut -d" " -f5)))

    cd $1
    
    for f in $( ls -1 ) ; do                    
        if [ -f $f ] ; then                    
             sum=$(($sum+$(ls -l $f | cut -d" " -f5)))
    
#            echo $f
#            echo $sum
        else
            sum=$(($sum+$(ls -l $f | cut -d" " -f5)))
            calcul $f
            sumrec=$?
            sum=$(($sum+$sumrec))
        fi
    done

    cd ..
    echo $sum
    return $sum
    
}

calcul .
sumfinal=$?
echo $sumfinal
The output of this script is:
Quote:
4
4106
9273
57
Aka, when returning from the function, it does not display the correct value...

I can't seem to figure out what I did wrong... any suggestions are welcomed.
 
Old 05-03-2010, 04:39 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The return command is limited to 255 as this command prompt session demonstrates
Code:
c@CW8:~$ function fun {
> return $1
> }
c@CW8:~$ fun 255; echo $?
255
c@CW8:~$ fun 256; echo $?
0
 
Old 05-03-2010, 08:44 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Some recommendations:

What if you encounter a symlink ? You probably should take that into account because you could go into infinite recursion.

I would use 'stat -c %s' to determine file size not 'ls'.

Don't use return, just have a variable that holds the total and add it the total.

It's neater if you do 'let sum+=sumrec'.

I hate recursion, I recommend you don't use it in real life unless you have no other choice.
 
Old 05-03-2010, 09:03 AM   #4
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Original Poster
Rep: Reputation: 30
Thank you both for your replies.

I understand now, and I fixed it by doing an echo $sum instead of return $sum, and recalling the function by sum=$(($sum+$(calcul $f))).

And yes, I know it is bad practice... I would just use du, but unfortunately this was my task... thankfully solved now, and I fully understood my error
 
Old 05-03-2010, 09:41 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Don't forget to mark the thread as SOLVED
 
Old 05-03-2010, 12:12 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
What are you *really* trying to do? I have a strong suspicion that this could be done trivially and far more robustly with "find".
 
  


Reply



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
[C] Wrapping around functions with different return types Alien_Hominid Programming 10 02-02-2010 10:33 AM
Problem using functions with bash isdigit Linux - Newbie 3 12-24-2009 07:21 PM
bash - problem with a return value Hobbletoe Programming 1 07-23-2009 10:45 AM
return statement in functions pantera Programming 2 12-06-2004 06:21 PM
How to use return values from system functions? kpachopoulos Programming 2 07-30-2004 03:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration