From: malte@techfak.uni-bielefeld.de
To: rc@archone.tamu.edu
Subject: shift, for, return values and getopts
Date: Mon, 30 Sep 1991 08:10:30 -0500 [thread overview]
Message-ID: <9109301310.AA16144@dahlie.techfak.uni-bielefeld.de> (raw)
If I define shift like this
fn shift {
n=1
if(~ $1 [1-9]* ) { n=$1; builtin shift }
for( i in $* ){
*=$$i; builtin shift $n; $i=$*
}
}
it doesn't work because $* gets evaluated in every loop, so $* changes
its value while executing. Is there a reason for this? I'd expected
that for( i in $* ) is evaluated only once to avoid using a temporary
variable, like
fn shift {
n=1
if(~ $1 [1-9]* ) { n=$1; builtin shift }
args=$* { for( i in $args ){
*=$$i; builtin shift $n; $i=$*
}
}
}
wich does work.
Now something different: I think it is most useful to have status set
to the return codes of a backquote substitution, e.g.
x=`{/bin/false}
echo $status
should give you "1".
If this breaks something (what could it be?), I'd propose to allow
something like
x=`{ /bin/false; return $status} # status=(1)
or even
x=`{ ls | /bin/false; exit $status} # status=(0 1)
At the moment, I'm busy writing a rc equivalent to sh's read and it would
simplify things alot.
---
And now my solution for getopts (not getopt, consult your man pages)
fn getopts {
if( ~ $#* 0 1 2 ){ return 1 } # no more arguments to process
# building a list of possible options
OPTSTRING=`` ($ifs ':') { echo $^1 | sed '1s|.|& |g' };
OPTSTRING='-'^$OPTSTRING;
# possible options are "--", "-o" "-oxx" "-o xx"
switch($^3){
# if $^3 is "--" force abortion of getopts
case -- ; return 1
# $^3 an option like "-o", possibly with argument
case -? ; OPTARG=($^3 $^4)
OPTIND=2
# $^3 an option with argument "-oxx"
case -??* ; OPTARG=(`{echo $^3 | sed '1s|\(..\)\(.*\)|\1 \2|'})
OPTIND=1
# not a switch, assume no more arguments
case * ; return 1
}
# check if option is in the list of legal options
if( ~ $OPTARG(1) $OPTSTRING ){
# set the varaible which contains the option
$2=`` ($ifs '-') { echo $OPTARG(1) }
# check if option demands an argument
if( ~ $^1 *$$^2:* ){
OPTARG=$OPTARG(2) # should be "shift OPTARG"
if( ~ $#OPTARG 0 ){
echo 'too few arguments to -'$$2 >[2=1]
exit 1
}
} else {
OPTARG=() OPTIND=1
}
} else {
echo invalid argument $OPTARG(1) >[2=1]
OPTARG='-?'
# at this point the return code could be set to 1,
# if this meets with the behaviour of your sh.
# SunOS returns 0.
}
return 0
}
This meets the syntax and sematics of sh getopts with one exception each:
In contrary to sh, the $* parameter is not optional.
It is neccesary to call "shift $OPTIND" after each call to getopts, in
contrary to sh getopts, where it suffices to shift after the last call to
getopts:
(rc) while( getopts 'st:ri:ng' opt $* ){
switch( $opt ){
case t ; text=$OPTARG;
case ...
}
shift $OPTIND
}
(sh) while getopts 'st:ri:ng' opt
do
case $opt in
...
esac
done
shift `expr $OPTIND - 1`
Suggestions and improvements are highly welcome!
_______________________________________________________________________________
malte@techfak.uni-bielefeld.de
send mail reply to: Universitaet Bielefeld, Technische Fakultaet
z. Hd. Malte Uhl
Postfach 8640
4800 Bielefeld 1
next reply other threads:[~1991-09-30 13:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-09-30 13:10 malte [this message]
1991-09-30 14:08 Byron Rakitzis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9109301310.AA16144@dahlie.techfak.uni-bielefeld.de \
--to=malte@techfak.uni-bielefeld.de \
--cc=rc@archone.tamu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).