Comparison treating strings as zero integers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison treating strings as zero integers
# 8  
Old 09-16-2012
For the first two:
Input:
Code:
./math 3 p sd

Output:
Code:
Result is 3

With case it gives:
Code:
./math: Bad left number "3"
./math: Bad right number "sd"
Result is 3


Last edited by Franklin52; 09-17-2012 at 03:34 AM.. Reason: Please use code tags for data and code samples
# 9  
Old 09-16-2012
Isn't this working?
Quote:
Originally Posted by balajesuri
Code:
#! /bin/bash

if [[ `echo $1 | egrep "^[0-9]*$"` ]]
then
    echo "good number: $1"
else
    echo "bad number: $1"
fi

Perhaps it will work with these small adjustments:
Code:
#! /bin/sh
if [ -n "$(echo "$1" | egrep "^[0-9]+$")" ]; then
  echo "good number: $1"
else
  echo "bad number: $1"
fi

Be sure to copy it exactly.
egrep "^[0-9]+$" is only allowing integers - no spaces and no ",", ".", "+" or "-", you have to adjust that if you want to allow more.
# 10  
Old 09-16-2012
Quote:
Originally Posted by TierAngst
For the first two:
Input:
./math 3 p sd
Output:
Result is 3

With case it gives:
./math: Bad left number "3"
./math: Bad right number "sd"
Result is 3

Check this...

Code:
$ sh test.sh 5
good number: 5     #$1 is 5

$ sh test.sh fg
bad number: fg   #$1 is fg

$ sh test.sh 3 gfg 5
good number: 3    #$1 is 3

$ sh test.sh "3 gfg 5"
bad number: 3 gfg 5     #$1 is "3 gfg 5"

$ cat test.sh
#! /bin/bash

if [[ `echo $1 | egrep "^[0-9]*$"` ]]
then
    echo "good number: $1"
else
    echo "bad number: $1"
fi

# 11  
Old 09-16-2012
Try this :
Code:
#!/bin/bash

a='blah'
[ "$a" -eq 0 ] 2>/dev/null ; x="$?"     # Get error status to x variable
# As per man bash, if x greater than 1, $a is not an integer
if [ "$x" -gt 1 ]; then
        echo 'Bad integer'
fi

exit 0

# 12  
Old 09-16-2012
This worked (though I'm sure egrep would've been fine too):
Code:
if ! [[ `echo $1 | grep "^[0-9]*$"` ]]

Also, decided to put a counter in each of the improper argument tests so that I could stop the script only after each test finished. Is there a more efficient way of doing this?

Code:
let probs=0

#each test if then 
let probs++

#after all tests finish
if [[ probs -ne 0 ]]
then
exit
fi


eeek! and a new question... how do I make it so my script doesn't try to use any of bash's special characters in the arguments?

Code:
blah:~$ ./math *& p 2
[1] 9028
-bash: p: command not found
blah:~$ usage: ./math <number> <operation> <number>
./math 3 p 2
Result is 5
[1]+  Done                    ./math *

ctrl+c kills the goofy stuff after it but I don't want to do that whenever some weird crap pops up, want it totally idiot proof.

Last edited by TierAngst; 09-16-2012 at 05:48 PM..
# 13  
Old 09-16-2012
Quote:
Originally Posted by TierAngst
ctrl+c kills the goofy stuff after it but I don't want to do that whenever some weird crap pops up, want it totally idiot proof.
What you describe is a feature of the shell, giving 'idiots' access to a shell is the problem here. Perhaps you should consider some sort of menu-driven utility rather than shell access.
# 14  
Old 09-16-2012
Oh well. And I'm the idiot here, learning to program :-P

Thanks for the help everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Strings to integers?

Hi, I'm totally new at this, so help will be appreciated. I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for... (6 Replies)
Discussion started by: jhsinger
6 Replies

2. Shell Programming and Scripting

awk -- telling the difference between strings and integers

This should be a really easy question. My input file will have a few fields that are strings in the first line, which I will extract and save as variables. The rest of the fields on every line will be integers and floating point numbers. Can awk tell the difference somehow? That is, is there... (5 Replies)
Discussion started by: Parrakarry
5 Replies

3. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

4. Shell Programming and Scripting

Create a name of a file by comparison of strings

Hello everybody, I need to create a file name from string. The thing is: I have a directory with files e.g: test.123 test.234 test.345 test.987 The name should be made from the first and last name of file alphabetically and from the directory, they are in, eg. ... (2 Replies)
Discussion started by: satin1321
2 Replies

5. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

6. Shell Programming and Scripting

Case insensitive comparison of strings

Hi All, In one shell script I have In variable "i" I am getting a full path of a file. Now I want to compare something like -- upper(*Nav*)) I dont want to do like below because in each CASE statement I doing so many operations. Please guide me. Thanks in advance... (4 Replies)
Discussion started by: vishalaksha
4 Replies

7. Shell Programming and Scripting

Treating Strings with spaces

I have a file list.txt which has a list of file names with spaces between the file names like /emptydir/file 1 how do i browse through the list.txt displaying the filenames. Almost all the file names in list.txt have space between them.This file list.txt is formed by using the find statement to... (5 Replies)
Discussion started by: kinny
5 Replies

8. Shell Programming and Scripting

Comparison of two files which contains strings using Shell scripting or PERL

Hi, I need sample code to compare the two files line by line which contains text strings and to print the difference in the third file. Thanks in advance (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

9. Shell Programming and Scripting

comparison of strings in unix shell scripting

Hi STORAGE_TYPE=$1 echo "########### SQL SESSION STARTED ###########" VALUE=`sqlplus -S /nolog << THEEND connect tcupro/tcupro_dev@bdd1optn SET SERVEROUTPUT ON DECLARE V_STORAGE_TYPE varchar2(3); V_ERR_MSG varchar2(255) ; V_LOG_LEVEL varchar2(200); BEGIN... (1 Reply)
Discussion started by: piscean_n
1 Replies

10. Shell Programming and Scripting

Comparison of 2 strings

I need some help with a script which accepts multiple rows of input (analysis from a virtual robot's testrun) from the standard in, and compares it as a whole with another file featuring erroneous data from a virtual robot's test run. What I want it to be able to do is slice off all the erroneous... (2 Replies)
Discussion started by: Retribution
2 Replies
Login or Register to Ask a Question