bash: testing if string is a number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: testing if string is a number
# 1  
Old 07-19-2006
bash: testing if string is a number

How do you test if a string is a number?

Trying to do something like this:

x="AS"
if( x is not a number ); then
x=0
fi

Because I want to do number arithmetic with x.
# 2  
Old 07-19-2006
ok I guess it's an error if it's not numeric.

Why not :

typeset -i x before assigning it?
# 3  
Old 07-19-2006
Below I write a code with tow output the first one is to numeric arguments and the second if it isnīt numeric
Code:
#!/bin/bash
if echo $1 | grep "^[0-9]*$">aux
then
  echo "La cadena es un valor numerico."
else
  echo "La cadena no es un valor numerico."
fi
rm aux

Enjoy that, and please correct my english Smilie
# 4  
Old 07-20-2006
If only you had searched the forums.... Smilie

How to check whether a string is number or not
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. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

3. Shell Programming and Scripting

Testing the length of a string

Hello, Unix-Forums! Is there a command that can check how long a user-entered string is? Please don't give me a code, just the name of the command (playing around yourself is much more fun than just pasting code) edit: I'm sorry, first hit of the forum search gave me the answer. (1 Reply)
Discussion started by: intelinside
1 Replies

4. Shell Programming and Scripting

Incrementing number in bash

I have the following code and getting the error ./raytrac.bash: line 231: ((: 0++: syntax error: operand expected (error token is "+") iarg = 0 iarg=0 narg=$# # Number of arguments passed. echo "narg = $narg" argsArr=("$@") # Set... (1 Reply)
Discussion started by: kristinu
1 Replies

5. UNIX for Dummies Questions & Answers

Testing for non-zero length string

Hello, can someone please explain to me why this happens: myserver#echo "$nothing" myserver#if ; then echo "nothing is a zero length string"; fi nothing is a zero length string myserver#if ; then echo "nothing is also a non-zero length string, apparently"; fi nothing is also a non-zero... (5 Replies)
Discussion started by: longjon
5 Replies

6. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

7. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

8. Shell Programming and Scripting

how to format a number in bash

Say I have a number x=123, but I want to it be x=000123, because I need to use it in as a file name. thanks! (2 Replies)
Discussion started by: aerosols
2 Replies

9. Shell Programming and Scripting

Number/string format in bash

I would like to change the format of an integer type number adding zeros to the left of it in a script in bash. For example number=1 echo $number 00001 Thanks (3 Replies)
Discussion started by: josegr
3 Replies

10. Shell Programming and Scripting

Testing the last character in a string

Hi In the shell scripted I'm trying to write! I would like to test the last character in a string. The string is a path/directory and I want to see if the last character is a '/'. The string (path/directory) is inputted by a user. If the '/' character isn't present then I want to be able to... (11 Replies)
Discussion started by: dbrundrett
11 Replies
Login or Register to Ask a Question