|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
If I use the result of a function call in a condtional statement,
is it possible to return one of three possible values or is it just true or false or a 0 or 1 status? I want to test if a number is greater than, equal to or less than another number. Thus, the function should be something like: if myfunction $num $another_num # return value is probably only 0 or 1 then echo $num is greater than else echo $num is less than fi But I need to know if $num and $another_num are equal too. Can I just have the function return a value (other than the status) that could be tested? How could I do that? Thx for your . |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2006-10-28, paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement, > is it possible to return one of three possible values or is it just > true or false or a 0 or 1 status? > > I want to test if a number is greater than, equal to or less than > another number. > Thus, the function should be something like: > > if myfunction $num $another_num # return value is probably only 0 or > 1 > then > echo $num is greater than > else > echo $num is less than > fi > > But I need to know if $num and $another_num are equal too. > > Can I just have the function return a value (other than the status) > that could be tested? > How could I do that? Thx for your . A function can return any value from 0 to 255. myfunction $num $another_num case $? in 0) echo success; : ... ;; 1) echo returned 1; : .... ;; .... 255) : .... ;; esac -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement, > is it possible to return one of three possible values or is it just > true or false or a 0 or 1 status? > > I want to test if a number is greater than, equal to or less than > another number. > Thus, the function should be something like: > > if myfunction $num $another_num # return value is probably only 0 or > 1 > then > echo $num is greater than > else > echo $num is less than > fi > > But I need to know if $num and $another_num are equal too. > > Can I just have the function return a value (other than the status) > that could be tested? > How could I do that? Thx for your . > You could of course return values of 0, 1, and 2 from the function, then test the return code through variable $? in a case statement. fn() { ... return $anyresult } case $? in ... esac But the intention of the exit code is rather to indicate success (code 0) or failure (code >0), and conceptually the shell functions are more like commands than like mathematical functions. Values from functions are usually returned through the "print interface"... fn() { ... print $anyresult } result=$( fn arg1 arg2 ) if [ $result -eq ... ] Or use a case statement... case $( fn arg1 arg2 ) in ... esac Janis |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement, > is it possible to return one of three possible values or is it just > true or false or a 0 or 1 status? > > I want to test if a number is greater than, equal to or less than > another number. > Thus, the function should be something like: > > if myfunction $num $another_num # return value is probably only 0 or > 1 > then > echo $num is greater than > else > echo $num is less than > fi > > But I need to know if $num and $another_num are equal too. > > Can I just have the function return a value (other than the status) > that could be tested? > How could I do that? Thx for your . > I don't know if this would work, but I'd type it in and test it. if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ; Or is this one of those thing that is NOT portable anywhere except inside my head? Dave |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Dave Kelly wrote:
> paintedjazz@gmail.com wrote: > >> If I use the result of a function call in a condtional statement, >> is it possible to return one of three possible values or is it just >> true or false or a 0 or 1 status? >> >> I want to test if a number is greater than, equal to or less than >> another number. >> Thus, the function should be something like: >> >> if myfunction $num $another_num # return value is probably only 0 or >> 1 >> then >> echo $num is greater than >> else >> echo $num is less than >> fi >> >> But I need to know if $num and $another_num are equal too. >> >> Can I just have the function return a value (other than the status) >> that could be tested? >> How could I do that? Thx for your . >> > > I don't know if this would work, but I'd type it in and test it. > > if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ; > > Or is this one of those thing that is NOT portable anywhere except > inside my head? Don't know which shell supports your syntax. In ksh93 you can write... x=$(( (a < b)? -1 : (a == b)? 0 : 1 )) Janis > > Dave |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote:
> Dave Kelly wrote: >> paintedjazz@gmail.com wrote: >> >>> If I use the result of a function call in a condtional statement, >>> is it possible to return one of three possible values or is it just >>> true or false or a 0 or 1 status? >>> >>> I want to test if a number is greater than, equal to or less than >>> another number. >>> Thus, the function should be something like: >>> >>> if myfunction $num $another_num # return value is probably only 0 or >>> 1 >>> then >>> echo $num is greater than >>> else >>> echo $num is less than >>> fi >>> >>> But I need to know if $num and $another_num are equal too. >>> >>> Can I just have the function return a value (other than the status) >>> that could be tested? >>> How could I do that? Thx for your . >>> >> >> I don't know if this would work, but I'd type it in and test it. >> >> if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ; >> >> Or is this one of those thing that is NOT portable anywhere except >> inside my head? > > Don't know which shell supports your syntax. In ksh93 you can write... > > x=$(( (a < b)? -1 : (a == b)? 0 : 1 )) I don't know which shell supports it either. I wrote it using an example from my K&R C language book. The OP does not have to use, minus one, zero, one. He can use any value that will fit in 'a' or 'b'. Does this take on the same type as in C code? And does this fill the need of the OP? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote: > paintedjazz@gmail.com wrote: > > If I use the result of a function call in a condtional statement, > > is it possible to return one of three possible values or is it just > > true or false or a 0 or 1 status? > > > > I want to test if a number is greater than, equal to or less than > > another number. > > Thus, the function should be something like: > > > > if myfunction $num $another_num # return value is probably only 0 or > > 1 > > then > > echo $num is greater than > > else > > echo $num is less than > > fi > > > > But I need to know if $num and $another_num are equal too. > > > > Can I just have the function return a value (other than the status) > > that could be tested? > > How could I do that? Thx for your . > > > > You could of course return values of 0, 1, and 2 from the function, > then test the return code through variable $? in a case statement. > > fn() { ... > return $anyresult > } > > case $? in > ... > esac > > But the intention of the exit code is rather to indicate success > (code 0) or failure (code >0), and conceptually the shell functions > are more like commands than like mathematical functions. Values from > functions are usually returned through the "print interface"... > > fn() { ... > print $anyresult > } > > result=$( fn arg1 arg2 ) > if [ $result -eq ... ] > > Or use a case statement... > > case $( fn arg1 arg2 ) in > ... > esac > > > Janis I'm not sure what the"print interface" is. Do you mean you just echo or printf a string to stdout and that will end up as the result of the function? I do like this way of doing it though. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
paintedjazz@gmail.com wrote:
> Janis Papanagnou wrote: > >>paintedjazz@gmail.com wrote: >> >>>If I use the result of a function call in a condtional statement, >>>is it possible to return one of three possible values or is it just >>>true or false or a 0 or 1 status? >>> >>>I want to test if a number is greater than, equal to or less than >>>another number. >>>Thus, the function should be something like: >>> >>>if myfunction $num $another_num # return value is probably only 0 or >>>1 >>>then >>> echo $num is greater than >>>else >>> echo $num is less than >>>fi >>> >>>But I need to know if $num and $another_num are equal too. >>> >>>Can I just have the function return a value (other than the status) >>>that could be tested? >>>How could I do that? Thx for your . >>> >> >>You could of course return values of 0, 1, and 2 from the function, >>then test the return code through variable $? in a case statement. >> >> fn() { ... >> return $anyresult >> } >> >> case $? in >> ... >> esac >> >>But the intention of the exit code is rather to indicate success >>(code 0) or failure (code >0), and conceptually the shell functions >>are more like commands than like mathematical functions. Values from >>functions are usually returned through the "print interface"... >> >> fn() { ... >> print $anyresult >> } >> >> result=$( fn arg1 arg2 ) >> if [ $result -eq ... ] >> >>Or use a case statement... >> >> case $( fn arg1 arg2 ) in >> ... >> esac >> >> >>Janis > > > I'm not sure what the"print interface" is. Do you mean you just echo > or printf a string to stdout and that will end up as the result of the > function? I do like this way of doing it though. > Yes, exactly. Sorry for having been unclear. And I shouldn't have used the ksh specific print; you should indeed use printf (not echo). Janis |
|
![]() |
| Outils de la discussion | |
|
|