3 ms·
How can you check that argv[1] is numeric and greater than zero? I've settled on this, recently, for my own use: i=$((${1} + 0)); [ "$i" -le 0 ] && { echo n
by chasil 4y ago
How can you check that argv[1] is numeric and greater than zero?
I've settled on this, recently, for my own use:
i=$((${1} + 0)); [ "$i" -le 0 ] && { echo nope; exit; }
- znpy 4y agoi don't see the problem with what you wrote. also, you could use expr if you don't like fiddling with parentheses when dealing with numeric values.
- chasil 4y agoA fork() will be imposed with expr; using the shell's native math is faster. In any case, I have never seen "best practices" for (numeric) type coercion and confirmation. Everybody seems to come up with something on their own.
- ykonstant 4y agoIf I am reading the POSIX spec correctly, test 0 -lt "$1" 2>/dev/null || exit $?; should be enough to get what you want in a compatible shell.