How to Check given string is number in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Check given string is number in shell script?
# 1  
Old 06-11-2009
How to Check given string is number in shell script?

Hi,

Can anyone help me out to check whether the input argument is number?

Example:

REQUEST_ID="123456"

I need to check the REQUEST_ID value is number or string.

Thanks in Advance

Regards
BS
# 2  
Old 06-11-2009
Code:
 
echo "12345S" | awk '$0 ~/[^0-9]/ { print "non number" }'

# 3  
Old 06-11-2009
Thanks a lot..

Its working fine...
# 4  
Old 06-11-2009
You can also do such checks entirely within your shell script. The following works for ksh93 and also for bash if you enable extglob.
Code:
# uncomment if using bash
# shopt -s extglob

REQUEST_ID="123456"

case $REQUEST_ID in
   ( +([0-9]) )   echo "REQUEST_ID is all numbers" ;;
             *)   echo "REQUEST_ID is not all numbers" ;;
esac

# 5  
Old 06-12-2009
How to assign this output into variable

I have tried the below option

X=`echo "$REQUEST_ID" | awk '$0 ~/[^0-9]/ { print TRUE }'`

echo $X

its printing empty string.

Regards
BS
# 6  
Old 06-12-2009
Code:
a=`echo $REQUEST_ID| tr -d "[0-9]`
if [[ -z $a ]]; then it would be number
[

]

-----Post Update-----

Quote:
Originally Posted by balajiora
How to assign this output into variable

I have tried the below option

X=`echo "$REQUEST_ID" | awk '$0 ~/[^0-9]/ { print TRUE }'`

echo $X

its printing empty string.

Regards
BS
put quotes i.e. " around TRUE
# 7  
Old 06-12-2009
Thanks for your quick reply... I have fixed the problem

Here the working code snippet

REQUEST_ID="1234"
X=`echo "$REQUEST_ID" | awk '$0 ~/[^0-9]/ { print "NOT_NUMBER" }'`
echo "Request Id: $REQUEST_ID"
echo "TEST :::$X"

if [ "$REQUEST_ID" != "" ] && [ "$X" != "NOT_NUMBER" ]; then
echo "Its number......"
else
echo "Not an number"
fi

Regards
BS
This User Gave Thanks to balajiora For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Linux Shell: how to check a string whether meets some conditions

Hi, guys. In Linux Shell script, how can I check a string whether meets some conditions. e.g.: If a string str must start with a underscore or a alphabet, and it must contains at least one lowercase, one uppercase, one numeric and one punctuation, and its length must be more than 8 characters... (2 Replies)
Discussion started by: franksunnn
2 Replies

3. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

4. UNIX for Dummies Questions & Answers

converting string to number in shell script

Hi, I am having a problem in converting a string to number so I can preform arithmetic operations. timeTot=0 timeTmp=$(cat idsOutput | grep 'Run time' | cut -c 36-39) timeTot=$ #This is line 28 echo "total RunTime=" $timeTot this is the error msg: ./ids2.sh: line 28: 0+1.35: syntax... (8 Replies)
Discussion started by: turki_00
8 Replies

5. Shell Programming and Scripting

Linux Shell Script to check for string inside a file?

This is what I have so far grep -lir "some text" * I need to check all files on the system for this text, if the text is found I need to change the file permissions so only ROOT has read and write access. How can this be done? (3 Replies)
Discussion started by: mapleleafs89
3 Replies

6. Shell Programming and Scripting

Check parameter is number or string

Hey I'm new in linux, I'm looking for a code to check whether the parameter is a number or a string. I have already tried this code: eerste=$(echo $1 | grep "^*$">aux) if But it doesn't work.:confused: Thanks (2 Replies)
Discussion started by: Eclecticaa
2 Replies

7. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

8. Shell Programming and Scripting

How to check whether a string is number or not

Hi , I am facing a problem .. which looks simple... but took 2 days of mine.. even now it is not solved completely.. I have one variable..., want to know whether that variable contains number... canbe +ve or -ve ... Values +35 --- number -43 --- number 45A -- non number... (12 Replies)
Discussion started by: shihabvk
12 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

10. Shell Programming and Scripting

shell to number and to string

Hello Does the unix korn shell provide a function to convert between number and string data-types regards Hrishy (1 Reply)
Discussion started by: xiamin
1 Replies
Login or Register to Ask a Question