Visit Our UNIX and Linux User Community


Checking input is Numerical


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking input is Numerical
# 1  
Old 01-18-2010
Checking input is Numerical

Hey guys, i was looking for some examples of how can i check if the use input is just using numerical. i came across an example using tr :

Code:
echo "read this"
read this

if [ -z "`echo "$this" | tr -d [[:digit:]]`" ] ;

then
echo "True - only alpha and numeric"
else
echo "False - there are NON alpha and numeric stuff here!"
fi

Could somebody explain to me how the code works and why -z and -d must be used.
# 2  
Old 01-18-2010
if you had searched the forum you could find the script below by Franklin which is better than the above one:-


Code:
#!/bin/bash

shopt -s extglob

INPUT="123"

case $INPUT in
   ( +([[:digit:]])  ) echo "$INPUT is all numbers" ;;
   ( +([[:alpha:]])  ) echo "$INPUT is all characters" ;;
   ( +([[:alnum:]])  ) echo "$INPUT is alphanumeric" ;;
   (                *) echo "$INPUT is empty or something unknown" ;;
esac

exit 0

for the tr -d & if [ -z ]..
just see your system manual using below.

Code:
man if
man tr

# 3  
Old 01-18-2010
From man sh:
Code:
            -z string     True if the length of string is zero.

And from man tr:
Code:
       -d, --delete
              delete characters in SET1, do not translate

Smilie
# 4  
Old 01-18-2010
its the -d command which i do not get it. a syntax for a normal tr command would be

Code:
tr [set1][set2]

so how does it work in the code above. basically, the input of $this is being deleted only if it is a numerical number since we specified [[:digit:]]?. am i getting it right?


hey admad, thanks for your solution. could i ask, why must we surround the option with +()?

---------- Post updated at 03:47 AM ---------- Previous update was at 03:41 AM ----------

There another problem which i am facing. I would like to key in a a input which gets to read decimals and numerical numbers like 23.23 or 24. but i would also like to do a check to make sure that no characters like "ad.23" or "aec" is being inputted.

Do you guys have a solution to this?
# 5  
Old 01-18-2010
Read this:-

Code:
Some versions of sed, ed, and ex support escaped versions of the extended Regular Expressions
described above, as do the GNU utilities.
POSIX Character Classes. [:class:]
This is an alternate method of specifying a range of characters to match.
 
[:alnum:] matches alphabetic or numeric characters. This is equivalent to A-Za-z0-9.  
[:alpha:] matches alphabetic characters. This is equivalent to A-Za-z.  
[:blank:] matches a space or a tab.  
[:cntrl:] matches control characters.  
[:digit:] matches (decimal) digits. This is equivalent to 0-9.  
[:graph:] (graphic printable characters). Matches characters in the range of ASCII 33 - 126. This
is the same as [:print:], below, but excluding the space character.
 
[:lower:] matches lowercase alphabetic characters. This is equivalent to a-z.  
[:print:] (printable characters). Matches characters in the range of ASCII 32 - 126. This is the
same as [:graph:], above, but adding the space character.
 
[:space:] matches whitespace characters (space and horizontal tab).  
[:upper:] matches uppercase alphabetic characters. This is equivalent to A-Z.  
[:xdigit:] matches hexadecimal digits. This is equivalent to 0-9A-Fa-f.
POSIX character classes generally require quoting or double brackets ([[ ]]).
bash$ grep [[:digit:]] test.file
abc=723
These character classes may even be used with globbing, to a limited extent.

bash$ ls -l ?[[:digit:]][[:digit:]]?
-rw-rw-r--    1 bozo  bozo         0 Aug 21 14:47 a33b

# 6  
Old 01-18-2010
Quote:
Originally Posted by gregarion
its the -d command which i do not get it. a syntax for a normal tr command would be

Code:
tr [set1][set2]

so how does it work in the code above. basically, the input of $this is being deleted only if it is a numerical number since we specified [[:digit:]]?. am i getting it right?
If you do:
Code:
man tr

you'll find that tr -d [set1] means delete the characters in set 1.
The code above is in fact broken:
1. they are testing for numerical values not alphanumerical
2. They left out single quotes so the command was not working right:

Try this:
Code:
echo "read this"
read this
if [ -z $(echo "$this" | tr -d '[[:digit:]]') ] ;
then
  echo "True - only numeric or the string was empty"
else
  echo "False - there is NON numeric stuff here!"
fi

But you do not need an external command, e.g.:
Code:
echo "read this"
read this
case $this in
  *[^0-9]*|"") echo "False - there is NON numeric stuff here or string is empty"    ;;
  *)           echo "True - only numeric"  ;;
esac


Last edited by Scrutinizer; 01-20-2010 at 03:14 AM..
# 7  
Old 01-19-2010
Hey Scrutinizer, just let me check how this code works

Code:
echo "$this" |

*this echo out the content inside $this and pipes it into the next set of command


Code:
tr -d '[[:digit:]]')

*this command will check if the data from the $this is in digits , and if it is, will delete them. if not, it will not delete them , thus the data will not be 0 and this  the message that there is a non-numeric stuff is inside.

did i get it right?

Previous Thread | Next Thread
Test Your Knowledge in Computers #221
Difficulty: Easy
Apple's OS X (macOS) installed base had grown to 80 million users as reported at the WWDC 2014.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies

2. UNIX for Dummies Questions & Answers

Best Alternative for checking input parameter contains required value or not

Any good way to check if code has the required output # /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts net.ipv4.icmp_echo_ignore_broadcasts = 1 /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts | grep "= 1" net.ipv4.icmp_echo_ignore_broadcasts = 1 What I can think of is above, and it... (16 Replies)
Discussion started by: alvinoo
16 Replies

3. UNIX for Dummies Questions & Answers

Validate numerical

friends that way I can validate that the value is numeric taken instance var = 12re if var = numeric then a echo "Numerical else echo "not mumerico" fi edit by bakunin: please use CODE-tags for code, data and terminal output (yourself). (9 Replies)
Discussion started by: tricampeon81
9 Replies

4. Shell Programming and Scripting

Checking the file depending on the input variable

Hi I have a requirement of taking time as input variable outside the script.depending on the time it will check the file output .like ./script.sh <30 min> so script.sh should run every 5 minutes ie.6 times to check the output file.Can any one please help here. (7 Replies)
Discussion started by: netdbaind
7 Replies

5. Shell Programming and Scripting

TCSH user input error checking

This was taken down recently because it appeared to be homework, but it isn't. It's for a script I am working on at work. Thanks for the help. How do you check that user inputs (arguments 1 and 2) are both numbers and are at least 5 digits in length? (2 Replies)
Discussion started by: thibodc
2 Replies

6. Shell Programming and Scripting

checking users input to file

ok im sorta stuck on this, The user enters a car model and it has to check it in a text file in the current directory. If the car model is not in the file, user has to enter another model This is what i have so far read -p "Enter a car model: " car1 grep -w $car1=$(cat carMakes.txt)... (3 Replies)
Discussion started by: gangsta
3 Replies

7. Programming

Checking columns in SQL, comparing user input and sizes.

I'm writing a KSH shell script that's using SQL though DB2. If I have a table defined and populated db2 "create table tb(num int,letter char(4))" db2 "insert into tb values(111,a) db2 "insert into tb values(112,b) db2 "insert into tb values(111,c) How can I check if a letter user... (0 Replies)
Discussion started by: busdude
0 Replies

8. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

9. Shell Programming and Scripting

Checking input for being numeric and integers

Hi, I'm trying to check to see that the arguments given to my script are both numeric and positive integers. I'm using tcsh. I figured out the positive part, but I am having trouble with the arguments being numeric and integers I have no idea where to get started with the checking them actually... (1 Reply)
Discussion started by: mistykz
1 Replies

10. UNIX for Dummies Questions & Answers

checking wether an input is using letters of the alphabet

afternoon forums. I need to get a way of testing as to wether an inputed character is part of the english alphabet. i have come up with the following code but its not working at all. until '] do echo This is not a Letter done any help would be beneficial to me. (1 Reply)
Discussion started by: strasner
1 Replies

Featured Tech Videos