LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-20-2007, 12:17 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
sh script: problem with return value in function


This script is supposed to return the difference in time between two files,
simulator.o and discretizer.o. Instead, the it prints out a bunch of blank lines. It is as though the function I have defined is not properly returning a value. Any help is greatly appreciated! Thanks, Jameson

Code:
function get_time_diff() {
  disc_time=`stat --format=%Y $file2`
  sim_time=`stat --format=%Y $file1`
  raw_time=`expr $sim_time - $disc_time`

  # in case the files were specifed backwards, make sure time is positive.
  if [ `expr $raw_time` -lt 0 ]; then
    raw_time=`expr 0 - $raw_time`
  fi

  if [ $1="h" ]; then
    return `expr $raw_time / 3600`
  elif [ $1="m" ]; then
    return `expr $raw_time / 60`
  elif [ $1="s" ]; then
    return $raw_time
  else
    return `expr $raw_time / 60`
  fi
}

unit=$1
dir_list=`ls -ad */`

for directory in $dir_list ; do
  if [ -f $directory/simulator.o ]; then
    file1=$directory/simulator.o
    if [ -f $directory/decomposer.o -o -f $directory/discretizer.o ]; then
      if [ -f $directory/decomposer.o ]; then
        file2=$directory/decomposer.o
      elif [ -f $directory/discretizer.o ]; then
        file2=$directory/discretizer.o
      else
        echo "No decomposer.o or discretizer.o found in dir." >&2
      fi
      echo `get_time_diff $unit`
    fi
  else
    echo "No simulator.o found in this dir." >&2
  fi
done
 
Old 06-20-2007, 12:47 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
The best way to "return" data from a shell script function is to have that function send the data to standard output, rather than using the bash return statement, because the return code which is sent back by the return statement is a relatively small integer which shows success or failure. Indeed, you show that you expect the result from your function to come via standard output when you say:

Code:
echo `get_time_diff $unit`
The problem is that your function doesn't send its output to standard output; it tries to return that value via the return statement. Instead of this:

Code:
return $raw_time
say

Code:
echo $raw_time
and instead of

Code:
return `expr $raw_time / 60`
say

Code:
expr $raw_time / 60
And so on.

Hope this helps.
 
  


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
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 06:54 PM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 02:05 PM
getting the return value from a thread start function cynthia_thomas Programming 1 02-18-2006 09:23 AM
using return in recursive function hubabuba Programming 9 10-10-2005 10:59 AM
can a function return a string? hubabuba Programming 13 03-06-2005 03:51 PM


All times are GMT -5. The time now is 04:25 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