[Solved] Testing Data Type of User Input


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Testing Data Type of User Input
# 1  
[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 ?

Moderator's Comments:
Mod Comment edit by bakunin: Please give your threads a meaningful title describing what your problem is. Naming a thread "question" is ...ahem... suboptimal. I have changed the title to something meaningful.

Last edited by bakunin; 01-18-2014 at 04:56 PM..
# 2  
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
# 3  
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
-->
Code:
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

-->

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags when posting code

Last edited by bakunin; 01-18-2014 at 09:33 PM..
# 4  
Quote:
Originally Posted by mohamed91
i'm using UNIX solaries ksh
[...]
it always see the input write .. for example if u give a alpha to numeric field ,it accept it !
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
This User Gave Thanks to bakunin For This Post:
# 5  
Hi.

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

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Thanks bakunin and drl the problem solved Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #685
Difficulty: Medium
Reverse Polish notation (RPN), also known as Polish programming notation.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

4. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

5. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

6. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

7. HP-UX

[Solved] processor type and bit information

Hi, I'm trying to download a compatible Oracle Client software for a HP-UX machine. I'd like to know if ... 1) HP-UX is 32 bit or 64 bit? 2) Processor type - Itanium or regular? when I execute uname -a I get HP-UX B.11.11 U 9000/800 728684161 unlimited-user license Based on the... (7 Replies)
Discussion started by: luft
7 Replies

8. Shell Programming and Scripting

Testing for TERM type

There are times where I want to use putty(vt100 TERM) and hummingbird (XTERM) is there a way to test what terminal connection I am using in my .profile so I can set my commands like stty erase correctly. (4 Replies)
Discussion started by: BeefStu
4 Replies

9. UNIX for Dummies Questions & Answers

Testing for correct user input from keyboard

What script can I use to catch errors in a shell script if user inputs alpha numeric characters instead on integers from the keyboard? (0 Replies)
Discussion started by: Pauline mugisha
0 Replies

10. Shell Programming and Scripting

Testing for no input.

Hi All Can anyone let me have a easy way to test if no input was given to a standard read. I would think of using the lenght of the variable, however this seems to be a long cut. Thx J (8 Replies)
Discussion started by: jhansrod
8 Replies

Featured Tech Videos