ksh numerical RegX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh numerical RegX
# 1  
Old 01-31-2007
ksh numerical RegX

I need to distinguish between numerical characters in a script:

Code:
echo "Enter a number."
read num
if (( $num = [a-Z]*[a-Z] )) ; then
  exit 0
fi

this RegX does not work.

Any suggestions?
# 2  
Old 01-31-2007
Use case (man case).
# 3  
Old 01-31-2007
good call.
# 4  
Old 01-31-2007
Code:
#! /usr/bin/ksh

needval=1
while ((needval)) ; do
        read val?'Enter a number - '
        if [[ $val = ?(+|-)+([0-9]) ]] ; then
                needval=0
        else
                echo $val is not an integer
        fi
done

echo val = $val
exit 0

# 5  
Old 01-31-2007
#!/usr/bin/ksh

echo "Enter a number."
read NUM

((NUM2=${NUM}+1-1))
if [ "${NUM}" == "{NUM2} ]
then
echo "${NUM} is an integer."
else
echo "${NUM} is NO integer."
fi

or if "valstr" (usually in /var/sadm/bin) is available on your system.

#!/usr/bin/ksh

echo "Enter a number."
read NUM

valstr -r "^[0-9][0-9]*$" "${NUM}"
if [ ${?) -eq 0 ]
then
echo "${NUM} is an integer."
else
echo "${NUM} is NO integer."
fi

or

#!/usr/bin/ksh

echo "Enter a number."
read NUM

echo ${NUM} | grep "^[0-9][0-9]*$" > /dev/null
if [ ${?) -eq 0 ]
then
echo "${NUM} is an integer."
else
echo "${NUM} is NO integer."
fi
# 6  
Old 02-01-2007
thanks for the replies, but I ended up using 'case' yesterday
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

need to extract terminal from this process -perl regx

Hi All, i ve a process, user4 31779 2836 0 01:43 pts/6 00:00:00 sh /home/user/DATE/SUT_SCR/c.sh like this i'll get so many process when in run ps -ef | grep pts | grep c.sh i need to extract terminal id from this string. i.e pts/6, or sometimes pts/22 same way i need to do for... (3 Replies)
Discussion started by: asak
3 Replies

3. Shell Programming and Scripting

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 : echo "read this" read this if ]`" ] ; then echo "True - only alpha and numeric" else echo "False - there are NON alpha and numeric stuff here!" fi... (7 Replies)
Discussion started by: gregarion
7 Replies

4. Shell Programming and Scripting

compare files by numerical value

Hi everyone, I would love to have a script that does the following: I have one file that looks like that: ATOM 1 BB SER 1 1 -31.958 -25.125 -11.061 1.00 0.00 ATOM 3 BB GLY 1 2 -32.079 -26.085 -14.466 1.00 0.00 ATOM 4 BB VAL 1 3 ... (1 Reply)
Discussion started by: s-layer
1 Replies

5. Shell Programming and Scripting

How to generate random strings from regx?

Hi, Guz! I'm working on a scripts compiler which needs a function to generate random strings. I think REGX may be a good solution to restrict the string format. Before DIYing I'd like asking for any existing libs or codes. Any help will be appreciated! (7 Replies)
Discussion started by: wqqafnd
7 Replies

6. Shell Programming and Scripting

Reading Numerical Binary Data using KSH

Hi, I've searched and couldn't find anyone else with this problem. Is there anyway (preferably using ksh - but other script languages would do) that I can read in binary float data into a text file. The data (arrays from various stages of radar processing) comes in various formats, but mainly... (3 Replies)
Discussion started by: Jonny2Vests
3 Replies

7. UNIX for Dummies Questions & Answers

Can grep do numerical comparisons?

Say for example I have a list of numbers.. 5 10 13 48 1 could I use grep to show only those numbers that are above 10? For various reasons I can only use grep... not awk or sed etc. (7 Replies)
Discussion started by: Uss_Defiant
7 Replies

8. UNIX for Dummies Questions & Answers

how to get the value of a numerical expression

Hi, All, I want to calculate a specific value of a Gaussian distribution, say the mean is a=3, variance is b=5, the indepentent variable is x=2, how could I get the y which is the Gaussian distribution value of x. Thanks, Jenny (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

9. Shell Programming and Scripting

Numerical Decision

I'm tryning to do something like this, I have this file: spaces12tabgoodbye spaces3tabhello I want to copy to another file the lines that have the number above 10... I thought using sort -rn but I don't know how to discard the lines that have the number below 10. Any idea? Thanks (3 Replies)
Discussion started by: pmpx
3 Replies

10. Shell Programming and Scripting

Convert text to numerical

Hi folks, I am trying to convert one character to a numeric format, for example : " 1" to number 1 :cool: Ups forgot to tell you .. !!! I am trying to do it with kornshell !!! ;) (4 Replies)
Discussion started by: nzq71k
4 Replies
Login or Register to Ask a Question