1,105,214 Community Members

In return value Problem

Member Avatar
Aamit
Posting Whiz
343 posts since Apr 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

check_time()
{
 fileName=$myFileName
newTime=$line
newtimeaccess="date --date \"$newTime 6 hour\" '+%Y-%m-%d %H:%M:%S'"
newtimeaccess=`echo $newtimeaccess | sh`
strnewtime=$(date -d "$newtimeaccess" +%s)
systime=$(date  +%s)
if [ $systime -le $strnewtime ]
then
        return "$fileName__0";
else
        return "$fileName__1";
fi
}

value=`check_time "$myFileName $line"`
##$myFileName -- file name and $line -- time of file

echo $value;

It giving error return: : numeric argument required
How to solve It

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
0
 

You can not return a string. You must return a numeric value. So changing "$fileName__0" to something like 0 would work.

Member Avatar
thekashyap
Practically a Posting Shark
809 posts since Feb 2007
Reputation Points: 193 [?]
Q&As Helped to Solve: 77 [?]
Skill Endorsements: 0 [?]
 
0
 

You can of course "return" a string like this.. which is not really returning and it forces you to ensure that there are no unwanted `echo`s in the function.. but it works.. :)

#!/bin/bash

function gimme_string() {
    echo "returned string"
}

my_string=`gimme_string`

echo "String I got is: $my_string"
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article