Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 11:46 PM microsuxx wrote: > a bit unrelated .. cheap numbers filter .. > > gimme_num( ) { declare -n b=BASH_REMATCH\[1] ; local o m=[0-9,.] s=$n i t ; > t="(-?$m+)" ; unset -v o ; for n ; do unset -v i ; while [[ $n =~ $t ]] ; > do n=${n/"$b"} i= o+=$b$s ; done ; [[ -v i ]

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Greg Wooledge
On Mon, Feb 10, 2025 at 23:13:33 +0100, Phi Debian wrote: > 029 is not a number Many people would disagree. Of course, it depends on the needs of the script that's being written -- that's why the wiki page offers so many different answers. In my experience, it's pretty rare for a script to *want

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread microsuxx
a bit unrelated .. cheap numbers filter .. gimme_num( ) { declare -n b=BASH_REMATCH\[1] ; local o m=[0-9,.] s=$n i t ; t="(-?$m+)" ; unset -v o ; for n ; do unset -v i ; while [[ $n =~ $t ]] ; do n=${n/"$b"} i= o+=$b$s ; done ; [[ -v i ]] && o+=$s ; done ; [[ -v o ]] && printf %s "$o" ; } gimme_n

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 11:10 PM Greg Wooledge wrote: > > hobbit:~$ phi 029 > bash: ((: 029: value too great for base (error token is "029") > bash: ((: 029: value too great for base (error token is "029") > > So we come full circle. > Nope phi() is not mine and doesn't remove the error message,

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
I admit the function was mis-named, should be isanumexpr As @chet noted this attempt was not too bad just need a little $1 check before pursuing the idea of letting the bash scanner do the job for complicated valid number (any base) I come up with this one function isanum { [[ "$1" =~ ^[+-]?[0-9

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Zachary Santer
On Mon, Feb 10, 2025 at 2:43 PM Lawrence Velázquez wrote: > > This accepts many valid expressions, not just literals. > > phi() { > (($1)) || (($1==0)) > } phi 'i[$( date >&2 )]' Whoopsie-daisy. https://mywiki.wooledge.org/BashPitfalls#read_num.3B_echo_.24.28.28nu

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Lawrence Velázquez
On Mon, Feb 10, 2025, at 2:24 PM, Phi Debian wrote: > On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey wrote > >> >> There isn't a reward for brevity or obfuscation; say what you mean: >> >> isnum2() >> { >> case "$1" in >> [-+] | '') return 1;; # empty or bare `-' or `+' >>

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Chet Ramey
On 2/10/25 2:24 PM, Phi Debian wrote: On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey > wrote There isn't a reward for brevity or obfuscation; say what you mean: isnum2() {         case "$1" in         [-+] | '')      return 1;;      # empty or b

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey wrote > > There isn't a reward for brevity or obfuscation; say what you mean: > > isnum2() > { > case "$1" in > [-+] | '') return 1;; # empty or bare `-' or `+' > [-+]*[!0-9]*) return 1;; # non-digit with leading

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 5:34 PM Zachary Santer wrote: > > int_regex='([+-]?)0*([[:digit:]]+)' > if [[ ${var} =~ ${int_regex} ]]; then > var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" > else > printf '%s\n' "Argument '${var}' is not a valid integer" >&2 > fi > The error message should be 'not a v

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Lawrence Velázquez
On Mon, Feb 10, 2025, at 1:05 PM, Andreas Schwab wrote: > On Feb 10 2025, Greg Wooledge wrote: > >> No, I've shown that it *is* working in older versions of bash, and I'm >> asking *why*. > > Does it? If it did, it should have printed -23, not -19. Yeah, it doesn't work. The pattern ?([-+]) matc

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Andreas Schwab
On Feb 10 2025, Greg Wooledge wrote: > No, I've shown that it *is* working in older versions of bash, and I'm > asking *why*. Does it? If it did, it should have printed -23, not -19. -- Andreas Schwab, SUSE Labs, sch...@suse.de GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EE

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 7:06 PM Andreas Schwab wrote: > On Feb 10 2025, Greg Wooledge wrote: > > > No, I've shown that it *is* working in older versions of bash, and I'm > > asking *why*. > > Does it? If it did, it should have printed -23, not -19. > > -- > Andreas Schwab, SUSE Labs, sch...@suse

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey wrote: > > There isn't a reward for brevity or obfuscation; say what you mean: > > isnum2() > { > case "$1" in > [-+] | '') return 1;; # empty or bare `-' or `+' > [-+]*[!0-9]*) return 1;; # non-digit with leading

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 6:44 PM Greg Wooledge wrote: > > > No, I've shown that it *is* working in older versions of bash, and I'm > asking *why*. > > As far as I can tell by reading the CHANGES file, it shouldn't work. > > But it does. > Jeez, sorry about that, my 'fast reading' parser skipped t

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Greg Wooledge
On Mon, Feb 10, 2025 at 17:32:24 +0100, Phi Debian wrote: > On Mon, Feb 10, 2025 at 3:43 PM Greg Wooledge wrote: > > > > > Can you please explain *how* this is working in older bash versions? > > > > I forgot to mention 'best on modern bash' :-), > as well as you forgot to mention > > 'The foll

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Zachary Santer
Once again left a dumb mistake in my bash snippet. On Mon, Feb 10, 2025 at 11:34 AM Zachary Santer wrote: > > And then this isn't even half as good: > int_regex='^([+-]?)0*([[:digit:]]+)$' > if [[ ${var} =~ ${int_regex} ]]; then > var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" > else > printf '%s

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Zachary Santer
On Mon, Feb 10, 2025 at 11:48 AM Chet Ramey wrote: > > On 2/10/25 11:34 AM, Zachary Santer wrote: > > On Mon, Feb 10, 2025 at 9:20 AM Phi Debian wrote: > >> > >> If 'best *general*' refer to the shortest line noise does > >> > >> $((${i/?([-+])/&10#})) > >> > >> Qualify for better than best ? :-)

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Chet Ramey
On 2/10/25 11:34 AM, Zachary Santer wrote: On Mon, Feb 10, 2025 at 9:20 AM Phi Debian wrote: If 'best *general*' refer to the shortest line noise does $((${i/?([-+])/&10#})) Qualify for better than best ? :-) And then this isn't even half as good: There isn't a reward for brevity or obfu

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Chet Ramey
On 2/7/25 10:21 PM, Martin D Kealey wrote: Hi Chet We seem to have very similar opinions about strong backwards compatibility in theory, and yet somehow we keep butting heads on how that pans out in practice. I'm concerned that the last ten years has seen a number of Linux distributions /st

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Zachary Santer
On Mon, Feb 10, 2025 at 9:20 AM Phi Debian wrote: > > If 'best *general*' refer to the shortest line noise does > > $((${i/?([-+])/&10#})) > > Qualify for better than best ? :-) And then this isn't even half as good: int_regex='([+-]?)0*([[:digit:]]+)' if [[ ${var} =~ ${int_regex} ]]; then var

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 3:43 PM Greg Wooledge wrote: > > Can you please explain *how* this is working in older bash versions? > I forgot to mention 'best on modern bash' :-), as well as you forgot to mention 'The following line noise is the best *general* answer we've found so far: with dyno ba

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Greg Wooledge
On Mon, Feb 10, 2025 at 15:20:55 +0100, Phi Debian wrote: > If 'best *general*' refer to the shortest line noise does > > $((${i/?([-+])/&10#})) > > Qualify for better than best ? :-) if so you may add in your web bible > don't forget to mention the inventor :-) Can you please explain *how* this

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Mon, Feb 10, 2025 at 1:15 PM Greg Wooledge wrote: Ha now we introduce signed (not obvious from original post) > The following line noise is the best *general* answer we've found > so far: > > $((${num%%[!+-]*}10#${num#[-+]})) If 'best *general*' refer to the shortest line noise does $

Re: filedescriptor leakage, potential crash

2025-02-10 Thread Chet Ramey
On 2/7/25 9:47 PM, Martin D Kealey wrote: Funny the bugs one finds when reading the source code to a program... I was looking at the usage of INPUT_REDIRECT and TRANSLATE_REDIRECT in execute_null_command() and thought “why compare against 8 values, when by re-ordering enum r_instruction in comman

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Greg Wooledge
On Mon, Feb 10, 2025 at 09:50:01 +0100, Phi Debian wrote: > On Sat, Feb 8, 2025 at 8:51 AM Robert Elz wrote: > > There are a gazillion different ways of getting rid of the octal > > conversion "problem", one I prefer in cases where I know the value > > is 2 digits, always, but might be 0n for 0<=n

Re: "printf %d ''" should diagnose the empty string

2025-02-10 Thread Phi Debian
On Sat, Feb 8, 2025 at 8:51 AM Robert Elz wrote: > > There are a gazillion different ways of getting rid of the octal > conversion "problem", one I prefer in cases where I know the value > is 2 digits, always, but might be 0n for 0<=n<=9, is $(( 1$x - 100 )) > > kre > > gazillion++ You say "I k