Check whether input is numeric


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check whether input is numeric
# 1  
Old 12-10-2013
Check whether input is numeric

Hello there, find below for my code first:

Code:
$pdp_asaba=`cat /tmp/temp_total | grep asaba | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_asaba=0
fi
$pdp_abuja=`cat /tmp/temp_total | grep abuja | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_abuja=0
fi
$pdp_ojota=`cat /tmp/temp_total | grep ojota | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_ojota=0
fi

What i want is that if the output of cat is not a numeric number, it should give it a value of ZERO but if it is a number then it should continue...

But with the above code i keep getting some "Command Not Found" error:

+ [[ ! 880035 =~ [0-9] ]]
++ cat /tmp/temp_total
++ grep abuja
++ sed 's/[^0-9]*//g'
+ =925605
pdp_total.sh: line 23: =925605: command not found


I know the conditional statement is incomplete, will appreciate your inputs.
# 2  
Old 12-10-2013
$variablename is replaced with the variables value when your script is executed. If you wish to assign a value to a variable do not put a $ sign in front:
Code:
pdp_asaba=`cat /tmp/temp_total | grep asaba | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_asaba=0
fi
pdp_abuja=`cat /tmp/temp_total | grep abuja | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_abuja=0
fi
pdp_ojota=`cat /tmp/temp_total | grep ojota | sed 's/[^0-9]*//g'`
if [[ ! $pdp =~ [0-9] ]]
then pdp_ojota=0
fi

This User Gave Thanks to cero For This Post:
# 3  
Old 12-10-2013
Many thanks Cero!!!

Script now working the way it should!!

God bless
# 4  
Old 12-10-2013
You are assingning pdp_asaba, pdp_abuja, and pdp_ojota, but you are testing always the same $pdp variable, that may be set outside the snippet posted. Is that what you want to do?
And, your entire cat | grep | sed pipe organ could be replaced by e.g.
Code:
sed -n '/asaba/ s/[^0-9]*//gp' /tmp/temp_total

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Put the numeric validation in user input when value is 5.1.2.3

Hi I need to put the validation in batch script when user will enter the build number it should be numeric.I can put the validation for numeric values but there is .(dot) in number so it would not take it as numeric. Is it possible we can store it in variable and remove the .(dot) from the... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

2. Shell Programming and Scripting

How to check for a Numeric Value?

Using shell, I have a variable, how can I check that variable for a numeric value such as "41.0"? My program needs to do one things if the numeric value is found, and another if something else such as a string of letter is found. is there a specific character that denotes a numeral? The... (2 Replies)
Discussion started by: chagan02
2 Replies

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

4. Programming

check the given string is numeric or not.

Hi, how to check the given string is numeric or not , without converting ( using strtol...). for ex: if string is C01 - non-numeric data if string is 001 - numeric data TIA (11 Replies)
Discussion started by: knowledge_gain
11 Replies

5. Shell Programming and Scripting

validating a input file for numeric and character

i have a input file like this 001|rahim|bajaj|20090102 while reading the file i need to check whether the first column is a number second column is a name is there any methodology to check for the same thanks in advance (2 Replies)
Discussion started by: trichyselva
2 Replies

6. Shell Programming and Scripting

check whether it is a non-numeric character

Below is the abstract of the script which is working fine. if ] then error_process "Invalid month format." return 1 fi I am doing validation for month and it errors if the value is > 12 or < 0. In addition, I want to add another condition to error if it... (2 Replies)
Discussion started by: sony_dada
2 Replies

7. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies

8. Shell Programming and Scripting

Check for numeric inputs

Hi All, How do i modify the below script such that if the input is numeric, it will give the numeric digit, else it will ouput "0" echo "xxx" | awk '/^+$/' (6 Replies)
Discussion started by: Raynon
6 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies
Login or Register to Ask a Question