[Solved] Testing Data Type of User Input

hi all
i'm new in shell scripting and now i'm working on project and i wanna make a function take input from user and test it's datatype then if correct write it to file

i wounder what is the best way to do this ?
can i use awk ?

What have your tried so far? Because this looks suspiciously like homework/classwork and for such questions there is a special forum.

Further, regardless of being homework or not, it generally helps if you describe the environment you are working with: which shell are you using, on which UNIX/Linux systems does this have to run, what are the data types you expect and want to test, etc., etc. ...

I suggest you first explain what you want to do in more detail, only then we can really help you at all.

bakunin

thanks bakunin for your response
i'm using UNIX solaries ksh
i'm work on DBMS project i finished create ,search ,truncate, delete , drop , .... and insert
but i've a bug in the insert function
it always see the input write .. for example if u give a alpha to numeric field ,it accept it !
i did the check like that
-->

x=`echo "$*\c" | wc -c`
y=`echo "$*\c" | tr -d "[:digit:]" | wc -c`

if [ $y -eq 0 ]
 then
 echo the i/p is numerical
 elif [ $y -eq $x ]
 then
 echo the i/p is alpha
 elif [ $y -lt $x ]
 then
 echo the i/p is alphanum
fi

-->

OK, this explains at least some of the missing parts. You still haven't said which data types you expect and how they look like. In fact this is the first thing you should do: ask yourself what a "datatype" is and what it looks like. Write down the rules for the data types you want to check.

For instance, mentioned "numeric", but: will this be an integer (no commas) or a floating point value (can contain commas), will it be unsigned (no negative values) or signed (negative and positive values possible), will it have certain ranges, which are allowed (for instance, 8bit so-called "short short int" can hold values 0-255 when unsigned, 0- +/-127 when signed)

Then, there might be "intrinsic rules": if you enter a name (basically a string) you might want to allow "-" but not characters like "&", "%", etc.. Further, a rule might be that the first letter in each word has to be uppercase, all others lowercase (because "John Doe" is correct, "JOhn dOE" is not). Your type-checker should reflect that.

I hope this helps.

bakunin

1 Like

Hi.

See also isnum.bash for a few bash functions to test for numeric and sub-set integer.

Best wishes ... cheers, drl

1 Like

Thanks bakunin and drl the problem solved :slight_smile: