[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "printf %d ''" should diagnose the empty string
From: |
Lawrence Velázquez |
Subject: |
Re: "printf %d ''" should diagnose the empty string |
Date: |
Mon, 10 Feb 2025 14:42:38 -0500 |
On Mon, Feb 10, 2025, at 2:24 PM, Phi Debian wrote:
> On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey <chet.ramey@case.edu> 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 sign
>> [-+]*) return 0;; # OK
>> *[!0-9]*) return 1;; # non-digit
>> *) return 0;; # OK
>> esac
>> }
>>
>> It obviously doesn't handle 0x constants, but could be changed to.
>>
>
> Would you accept this one then, should be general enough, faster too, and
> explicit, really mean what is accepted as num literal
>
> $ echo $BASH_VERSION
> 5.2.21(1)-release
> $ function isanum
> { (($1)) || (($1==0)) && return 0
> return 1
> } >/dev/null 2>&1
This accepts many valid expressions, not just literals.
% cat /tmp/isnum.bash
chet() {
case $1 in
[-+] | '') return 1 ;;
[-+]*[!0-9]*) return 1 ;;
[-+]*) return 0 ;;
*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
phi() {
(($1)) || (($1==0))
}
chet '1+2'
printf 'chet 1+2 -> %s\n' "$?"
phi '1+2'
printf 'phi 1+2 -> %s\n' "$?"
% bash /tmp/isnum.bash
chet 1+2 -> 1
phi 1+2 -> 0
--
vq
- Re: "printf %d ''" should diagnose the empty string, (continued)
- Re: "printf %d ''" should diagnose the empty string, Lawrence Velázquez, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Zachary Santer, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Chet Ramey, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Zachary Santer, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Chet Ramey, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string,
Lawrence Velázquez <=
- Re: "printf %d ''" should diagnose the empty string, Zachary Santer, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Greg Wooledge, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, microsuxx, 2025/02/10
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/11
- Re: "printf %d ''" should diagnose the empty string, microsuxx, 2025/02/11
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/11
- Re: "printf %d ''" should diagnose the empty string, microsuxx, 2025/02/11
- Re: "printf %d ''" should diagnose the empty string, microsuxx, 2025/02/11
- Re: "printf %d ''" should diagnose the empty string, Phi Debian, 2025/02/11