Read only number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read only number
# 1  
Old 05-03-2010
Read only number

Hi

I don't know, how read only number and when is on enter letter so I want write "Wrong enter "
Thank you and Sorry for my English
# 2  
Old 05-03-2010
one way is to chck each lelemnt of the string to see if it is a numeric character

Code:
while :
do
  echo "enter a number: \c"
  read number
  len=${#number}
  testvar=$(echo "$number" | tr -dc '[:digit:]')   # remove non-numeric chars from $number
  if [[ $len -ne ${#testvar} ]] ; then  # if you had all numeric chars they would be the same length
      echo "$number is not a number"  # error message
  else
      break      #exit the loop
  fi
done 
#... more code goes here to work with the number

# 3  
Old 05-04-2010
Quote:
Originally Posted by hroch
... I don't know, how read only number and when is on enter letter so I want write "Wrong enter "
...
I'd say you may want to define what you mean by a "number" exactly. Is it just a positive integer ? Then jim's suggestion or the plain old regex /^\d+$/ or /^[0-9]+$/ or /^[0-9][0-9]*$/ should work with most languages/commands.

Do you allow negative numbers ? e.g. -123
Do you allow floating-point numbers ? e.g. 0.345, -123.456
How about preceding "+" sign or decimal point "." ? e.g. +12, .998
Or scientific notations ? e.g. 1e12

Depending on your requirement, you could go for more elaborate code in a scripting language or the shell.

tyler_durden
# 4  
Old 05-04-2010
"Number" is positive integer
# 5  
Old 05-04-2010
Quote:
Originally Posted by hroch
"Number" is positive integer
In that case, jim's script is good enough.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a number from a file?

hello guys, I'm struggled to get a number from a very long text file. NAtoms= 33 NActive= 30 NUniq= 23 SFac= 1.00D+00 NAtFMM= 60 NAOKFM=F Big=F Integral buffers will be 131072 words long. Raffenetti 2 integral format. The number 33 is what I wanted, always follows NAtoms=... (5 Replies)
Discussion started by: liuzhencc
5 Replies

2. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

3. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

5. Shell Programming and Scripting

How to read n number of lines from a file

Hiii I am very new to shell scripting.This is my data file a.txt: 56 45.78 1000 11.23 76.89 45 34.56 23 3400 100 .......... Now i am must use shell scripting to read n number of lines from the file & from ts n number of lines i need to find greatest number among them & so on for... (44 Replies)
Discussion started by: varsha
44 Replies

6. Shell Programming and Scripting

Read the specified line number from file

Hi Guys, I am new to unix. Actually i want help in writing an single command where i can actually read specific line number in file where the line number will be passed to command as parameter. ex. 1 a 2 b 3 c 4 d And to my command i pass as 2. so i should get output as 2 b ... (15 Replies)
Discussion started by: kam786sim
15 Replies

7. UNIX for Dummies Questions & Answers

read a number

- A=90-100 - B=80-89 - C=70-79 - D=60-69 - F=0-59. echo -n "What test score did you get? (0-100)" read score1 echo “you got a $score1” if . then echo “you got a F” else echo --------------------- ) echo you got... (2 Replies)
Discussion started by: JudoMan
2 Replies

8. Shell Programming and Scripting

How to get the line number from while read

I have a file TXTPROCESS.TXT.20071129 in which line 1 contains the file name and rest of the records are data. The file data looks like this: TXTPROCESS.TXT.20071129 DIVD20071129 INTR20071129 BALN20071129 From line 2 onwards the first 4 characters defines individual process. What I... (2 Replies)
Discussion started by: skymirror
2 Replies

9. Shell Programming and Scripting

read number of arguments in c shell

I am writing script in c shell and using this script to read the command line arguments, but it is not working. Pl. someone let me know what is the problem. #!/bin/csh -f if ($#argv <> 2) then echo "you must give exactly two parameters" else set name1 = $argv ... (1 Reply)
Discussion started by: skumar11
1 Replies

10. Shell Programming and Scripting

read n number of inputs

Hello, I think its a sinple query but somehow i m stucked up here... I am trying to enter n number of inputs from the user and write them in an input file ie row wise... I tried standard commands like $echo "enter the inputs for the file" $read var1 var2 var3 var4 test1 test2... (14 Replies)
Discussion started by: er_aparna
14 Replies
Login or Register to Ask a Question