Prev: Parsing "for" clause
Next: read multiple fields separated by : and fields separated by , in loop stored in $var
From: Stephane CHAZELAS on 13 Mar 2010 06:39 2010-03-13, 10:42(+00), SM: [...] > I came across this kind of hack: > > is_int() { > printf "%d" $1 > /dev/null 2>&1 missing quotes above. > return $? redundant and missing quotes. > } > > read s > if is_int "$s"; then > echo "$s is an integer." > else > echo "$s is not an integer." > fi It's a lot shell dependant Some shells will fail on 1+1, RANDOM, 1e3, 1.22, 9999999999999999999999999999999999999999999999... some will not. Most will say that the empty string (or any sequence of blanks) is a valid number (0), some will say that 089 is invalid, some not. Most will accept 0x12... Some shells will even be locale dependant: $ ksh -c 'printf %d\\n 1.1' && echo ok 1 ok $ LC_NUMERIC=fr_FR ksh -c 'printf %d\\n 1.1' && echo ok ksh[1]: printf: 1.1: arithmetic syntax error ksh[1]: printf: warning: invalid argument of type d (to my knowledge only ksh93 is affected by that) Now, it dependant what you want to check the string for. If it's so that it can be embedded in a shell arithmetic expression, then that approach may make sense (but will not be completely fool-proof). -- Stéphane
From: SM on 13 Mar 2010 07:23 2010-03-13, Stephane CHAZELAS skribis: > 2010-03-13, 10:42(+00), SM: > [...] >> I came across this kind of hack: >> >> is_int() { >> printf "%d" $1 > /dev/null 2>&1 > > missing quotes above. > >> return $? > > redundant and missing quotes. > >> } >> >> read s >> if is_int "$s"; then >> echo "$s is an integer." >> else >> echo "$s is not an integer." >> fi > > It's a lot shell dependant > I acknowledge the said method's inadequecies. It's a hack and it works as wanted on one of my scripts where I have to check if the first character of a string is a digit. -- kasmra :wq
From: Francis Moreau on 13 Mar 2010 08:16 On Mar 13, 12:39 pm, Stephane CHAZELAS <stephane_chaze...(a)yahoo.fr> wrote: > 2010-03-13, 10:42(+00), SM: > [...] > > > I came across this kind of hack: > > > is_int() { > > printf "%d" $1 > /dev/null 2>&1 > > missing quotes above. > > > return $? > > redundant and missing quotes. > why are quotes missing ?
From: Francis Moreau on 13 Mar 2010 08:47 On Mar 13, 8:48 am, Decare <dec...(a)yeah.net> wrote: > Is there any to determine whether a string > is a integer or not? For example, > not sure which integer formats you want to support but: expr $s \* 0 2>&1 >/dev/null might do it.
From: Stephane CHAZELAS on 13 Mar 2010 13:11
2010-03-13, 05:16(-08), Francis Moreau: > On Mar 13, 12:39 pm, Stephane CHAZELAS <stephane_chaze...(a)yahoo.fr> > wrote: >> 2010-03-13, 10:42(+00), SM: >> [...] >> >> > I came across this kind of hack: >> >> > is_int() { >> > printf "%d" $1 > /dev/null 2>&1 >> >> missing quotes above. >> >> > return $? >> >> redundant and missing quotes. >> > > why are quotes missing ? Leaving a variable expansion unquoted, in the shell, means asking for the expansion to be split (according to complicated rules involving the $IFS variable) and each resulting word to be subject to filename generation. For instance if the first positional parameter contains " 1 * 2" and with the default value of IFS, printf will not be passed one argument containing " 1 * 2" but one containing "1", then as many as there are non-dot files in the current directory, and then 2. If all the files in the current directory have integer names, then printf will succeed. the perl equivalent of: echo $1 would be something like: $, = " "; print map glob, split " ", $ARGV[0]; $ ls bar foo $ perl -le '$,=" ";print map glob, split " ", $ARGV[0]' ' 1 * 2' 1 bar foo 2 $ sh -c 'echo $1' sh ' 1 * 2' 1 bar foo 2 To have the equivalent of print $ARGV[0], you need echo "$1" -- Stéphane |