find-first-fail: -s start value for searching.

This commit is contained in:
Ole Tange 2020-09-13 19:25:23 +02:00
parent ba8eee5953
commit 0d32feb0cc

View file

@ -10,7 +10,7 @@ find-first-fail - find the lowest argument that makes a command fail
=head1 SYNOPSIS =head1 SYNOPSIS
B<find-first-fail> [-2] [-q] I<command> B<find-first-fail> [-2] [-q] [-s I<start>] I<command>
=head1 DESCRIPTION =head1 DESCRIPTION
@ -39,6 +39,10 @@ arguments: I<from> I<to>.
Quiet. Ignore output from I<command>. Quiet. Ignore output from I<command>.
=item B<-s I<start>>
Start searching from the value I<start>. Normally searching will start from the value 1.
=back =back
@ -164,20 +168,22 @@ find-first-fail() {
quiet="" quiet=""
opt2=false opt2=false
verbose=false verbose=false
start=1
# Parse and remove options # Parse and remove options
while getopts "2vq" options; do while getopts "2vqs:" options; do
case "${options}" in case "${options}" in
(2) opt2=true;; (2) opt2=true;;
(q) quiet=">/dev/null 2>/dev/null";; (q) quiet=">/dev/null 2>/dev/null";;
(v) verbose=true;; (v) verbose=true;;
(s) start="$2";;
(-) break;; (-) break;;
esac esac
done done
shift $(( OPTIND - 1)) shift $(( OPTIND - 1))
# If function(1) = false: run 'not function()' instead # If function(1) = false: run 'not function()' instead
if _run 1 1 "$@" ; then if _run "$start" "$start" "$@" ; then
not='' not=''
else else
not='!' not='!'
@ -186,8 +192,8 @@ find-first-fail() {
# exponential search for the first value that is false # exponential search for the first value that is false
# low = previous value (function($low) == true) # low = previous value (function($low) == true)
# high = low * 2 (function($high) == false) # high = low * 2 (function($high) == false)
high=1 high=$start
while _run 1 $high "$@" ; do while _run $start $high "$@" ; do
low=$high low=$high
high=$(( $high*2 )) high=$(( $high*2 ))
if [ $high -gt 4611686018427387900 ] ; then if [ $high -gt 4611686018427387900 ] ; then