[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Adding missing bash features for safely re-usable bash c
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Adding missing bash features for safely re-usable bash code |
Date: |
Mon, 8 Apr 2013 14:10:11 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Mon, Apr 08, 2013 at 05:51:01PM +0000, adrelanos wrote:
> What about
> functions which are supposed to return different strings depending on
> if/else?
If you are using the stdout capturing paradigm, nothing prevents you
from doing so inside an if statement:
foo() {
if something; then
echo "true"
return 0
else
echo "false"
return 0
fi
echo "internal error; how did I get here?" >&2
return 1
}
However, my personal preference for functions returning values (and this
is not limited to strings; the same applies to integers larger than 255 or
smaller than 0, or floating point values, or arrays) is to hard-code
the variable "r" in both the caller and the callee:
rand() {
local max=$((32768 / $1 * $1))
while (( (r=$RANDOM) >= max )); do :; done
r=$(( r % $1 ))
}
caller() {
local r
rand 17
if ((r < 10)); then ....
fi
}
This gives the callee a place to store its return value, without requiring
a subshell, and without requiring the re-parsing of an ASCII stream.
If "r" is declared local in the caller, then its scope is limited to the
caller and the callee. Otherwise, it's global. I get to choose.
Of course this means I can't use the variable "r" for any other purpose.
Not that I was going to. If you don't like that, you can use "___r" or
whatever.
Re: [Help-bash] starting a central bash script repository, Pierre Gaston, 2013/04/08
- [Help-bash] Adding missing bash features for safely re-usable bash code, adrelanos, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Greg Wooledge, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, adrelanos, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, DJ Mills, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, adrelanos, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code,
Greg Wooledge <=
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Eric Blake, 2013/04/08
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Pierre Gaston, 2013/04/09
- Re: [Help-bash] Adding missing bash features for safely re-usable bash code, John Kearney, 2013/04/09
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, adrelanos, 2013/04/08
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Greg Wooledge, 2013/04/08
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, John Kearney, 2013/04/08
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Greg Wooledge, 2013/04/09
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, Pierre Gaston, 2013/04/08
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, adrelanos, 2013/04/08
Re: [Help-bash] Adding missing bash features for safely re-usable bash code, John Kearney, 2013/04/08