check for numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check for numbers
# 1  
Old 12-26-2009
Java check for numbers

Hi,

I have a requirement to accet only numbers. Piece of code beolow.

waitTime() {
echo "Enter amount of time to wait (in sec)."
read wait_time
sleep $wait_time
}
the above function should accept only numbers otherwise, it has t throw an error.

Please help.
# 2  
Old 12-27-2009
Code:
read_num(){
        echo "please enter a number"
        read num
        isNum=$(echo $num | awk -v num=$num '{if( num ~ /^[1-9][0-9]*$/ ) {print "0"} else {print "1"}}')
        if [[ $isNum -eq "0" ]]; then
                echo "sleep for $num seconds"
        else
                read_num
        fi
}

# 3  
Old 12-27-2009
Quote:
Originally Posted by Satyak
Hi,

I have a requirement to accet only numbers. Piece of code beolow.

waitTime() {
echo "Enter amount of time to wait (in sec)."
read wait_time
sleep $wait_time
}
the above function should accept only numbers otherwise, it has t throw an error.

There's no need for an external command:

Code:
## USAGE: is_num NUM
## RESULT: in $?: 0 if NUM is a positive integer; 1 if not
is_num()
{
  case $1 in
    *[!0-9]*) return 1 ;;
  esac
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string contains substring surrounded by numbers

Hi, I have a process that generates strings. I would like to check each string and search for substring which contains the letter 'E' surrounded by numbers (both sides of the letter 'E'). few examples: AA4E7012A2 - contains E surrounded by numbers FE18274012 - does not contain E... (3 Replies)
Discussion started by: yanive
3 Replies

2. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

4. Shell Programming and Scripting

Check if a string starts with certain values and ends with numbers

This is very basic. Yet Iam struggling to get the right pattern for my check. Apologize in advance to ask a very lame question. I have to validate if a value of the variable starts with "efgh" and followed by 6 numbers. Var1="efgh234567" The condition Iam trying to achieve is similar to... (6 Replies)
Discussion started by: deepakwins
6 Replies

5. Shell Programming and Scripting

Request to check:remove entries with duplicate numbers in first row

Hi I have a file 1 xyz 456 1 xyz 456 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 4 ghf 658 I want the output 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 (3 Replies)
Discussion started by: manigrover
3 Replies

6. Shell Programming and Scripting

Check whether there is only numbers

Hi everyone, I must make a script which will be executed as follows : ./script.sh Directory/n.car where there can be as many numbers as we may want. I'd like to : - ignore the Directory ; Meaning I just need n.car - Check the name of the file : has it got some Alphabetic... (7 Replies)
Discussion started by: Lyth_o
7 Replies

7. Shell Programming and Scripting

Shell script to check numbers!

Hello All, I have 3 types of files. The names of which starts with P,I,M like P********* D********* M********* now I need to do some operations witht hese files.. so if file name starts with P or p then do the operation for P file... fi else (20 Replies)
Discussion started by: smarty86
20 Replies

8. Shell Programming and Scripting

Regular expressions to check numbers with currency

Hi All I am struggling for the last one week on how to write a regular expression to search a number with currency (such as $ 1, 245, 000.00, Rs. 1, 00, 000.00 etc) but in vain . Please help me. Thanks in advance. -----Post Update----- Hi All I am struggling for the last one... (7 Replies)
Discussion started by: my_Perl
7 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. Shell Programming and Scripting

Shell script to check the unique numbers in huge data

Friends, I have to write a shell script,the description is---- i Have to check the uniqueness of the numbers in a file. A file is containing 200thousand tickets and a ticket have 15 numbers in asecending order.And there is a strip that is having 6 tickets that means 90 numbers.I... (7 Replies)
Discussion started by: namishtiwari
7 Replies
Login or Register to Ask a Question