Number or Character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number or Character
# 1  
Old 02-07-2003
Number or Character

How to find that a given variable is a number or character?


e.g.

echo "Enter the number"
read var

If "$var" is a number then display "Number has been entered"
else display "Character has been entered".

Thanking you in advance
# 2  
Old 02-07-2003
Anything that the user enters will be one or more characters. If you want to determine if that string of characters represents a number, you need to define things much more. Which of these should be rejected?
0001
1.345
-50
0xFFFF
And the list could go on and on. We also need to know what shell you are using. Because we don't know these things we are likely to give you an answer that doesn't solve your problem. So then we need several posts before we can focus on your problem. Please help us help you by providing more information about your problem.

But I guess that I'll get the ball rolling. I will assume that you are using ksh. And I'll assume that you want integers in base 10:

Code:
#! /usr/bin/ksh
echo Enter an integer-- \\c
while read var ; do
       if [[ $var = ?(+|-)+([0-9]) ]] ; then
             echo $var is an integer
       else
             echo $var is not an integer
       fi
       echo Enter an integer -- \\c
done
exit 0

# 3  
Old 02-07-2003
Bourne shell solution

For those using the Bourne shell rather than the Korn shell, you will usually go through expr.
Code:
#!/bin/sh

var="$1"
if [ "x$var" = x ]; then
    echo No input
elif [ `expr "$var" : '[0-9][0-9]*$'` -gt 0 -o  `expr "$var" : '[-+][0-9][0-9]*$'` -gt 0 ]; then
    echo $var is an integer
else
    echo $var is not an integer
fi

I believe Perderabo's solution in ksh has the advantage of not calling an external program to do some of the work.
# 4  
Old 02-10-2003
MySQL

Thank you very much Perderabo and criglerj.

I wanted to check integer (base 10)

In next posts I will try to be more specific
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace variable number of character

hi All, My requirement is to replace variable number of in between character with X The first 6 and last 4 characters will remain same and rest characters will be masked with X Example - input String 12345678912345 output 123456XXXX2345 input string 123456 ... (1 Reply)
Discussion started by: Pratik4891
1 Replies

2. Shell Programming and Scripting

Get column number with the same character length

so, i want to tail about the last 3000 lines of a log file and find the column that has the same number of characters across all 3000 lines (or most of the 3000 lines) tail -3000 logfile | while read line do ColumnCharCount=$(for eachwordorwhatever in "${echo $line}" do ... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. UNIX for Dummies Questions & Answers

Find certain number of character in vi

Hello, Experts, I have a file with the first and second column connected together, and i want to use vi to seperate them (put a space in between). Is there any command in vi would put a space after the 7th letter? Thanks! example: 0.981101.517 2.944101.517 4.907101.517 (10 Replies)
Discussion started by: wingsy1212
10 Replies

4. Shell Programming and Scripting

new line after specific number character

Hi All, I have input file like this: input1: ( 1083479)=T 158V 1798, T 391V 1896,T 1138V 2273,T 1547V 2477,T 2249V 2917,T 3278V 3234,T 4152V 3495,T 5500V 3631, ( 1083501)=T 181V 1851, T 459V 1954,T 810V 2141,T 1188V 2372,T 1638V 2696,T 2731V 3124,T 4799V 3640,... (5 Replies)
Discussion started by: attila
5 Replies

5. Shell Programming and Scripting

check number or character of a file

Hi, Please see the below information. 1. Read files one by one and run the script. 2. Check if the filename is CHARTER OR NUMBER 3. Run the case statement. I have files in the folder. i will get 300 files in a single day. Abc_111111111111.csv 101010_kkk_bbbb.csv... (6 Replies)
Discussion started by: onesuri
6 Replies

6. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

7. Shell Programming and Scripting

check number of character

hi, I would like to calculate number of character for a number, for exemple : 1200 --> there are 4 characters , 120001 -> 5 characters (4 Replies)
Discussion started by: francis_tom
4 Replies

8. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

9. UNIX for Dummies Questions & Answers

Sed character number specification

I have the following sed command: sed '/7361105/s/^\(..............................................................................................................................................................................\)....\(.*\)/\16776\2/' arch.txt > arch.tmp && mv arch.tmp arch.txt ... (4 Replies)
Discussion started by: mvalonso
4 Replies

10. UNIX for Dummies Questions & Answers

defining a variable as a number or character?

I am writing a script that needs to accept numbers into a variable by prompting and using the code read num case $num in 000) break ;; *) I am fairly new to unix and am loving the versatility of the language but need a little shove in the right... (1 Reply)
Discussion started by: noobian
1 Replies
Login or Register to Ask a Question