Using grep inside a test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep inside a test
# 1  
Old 11-09-2009
Using grep inside a test

Hi,
I want to use grep inside a test statement, but I am getting an error message.

Two variables

testvarNum=5
testvarNonNum=x

echo $testvarNum | grep [0-9][0-9]*

The result of this is as follows:
5

However, when I try the following (i.e. to test if the variable is numeric or non-numeric):
[ echo $testvarNum | grep [0-9][0-9]* ]

I get an error as follows:

grep: ]: No such file or directory
-bash: [: missing `]'


Why is this? Probably I'm making a simple error.

Thanks for any help
# 2  
Old 11-09-2009
Code:
$
$ var=5
$ echo $var
5
$ echo $var | grep [0-9][0-9]*
5
$ if [ `echo $var | grep [0-9][0-9]*` ]; then   echo "in if"; else   echo "in else"; fi
in if
$
$ var="x"
$ echo $var
x
$ echo $var | grep [0-9][0-9]*
$
$ if [ `echo $var | grep [0-9][0-9]*` ]; then   echo "in if"; else   echo "in else"; fi
in else
$
$

tyler_durden
# 3  
Old 11-09-2009
A "dirty" method
Code:
read -p "Enter something : " VAR
if [ $VAR -eq $VAR ] 2>/dev/null
then echo "Is number"
else echo "Is not number"
fi

(the 2>/dev/nul is just there to hide the garbage Smilie)
# 4  
Old 11-10-2009
Thanks for the help guys, that sorted it out!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it correct? if test grep EOF $a ...

read a if test grep EOF $a then echo yes file else echo no fi (1 Reply)
Discussion started by: iamsumibisht
1 Replies

2. Shell Programming and Scripting

Using grep with test and without using [[ ]]

As an exercise, I'm trying to re-write this code without the compound square brackets, using grep and test. Need to know what to do about the "equal-tilde". #!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if +$ ]]; then if ; then echo "INT is zero." else if ; then... (17 Replies)
Discussion started by: Xubuntu56
17 Replies

3. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 Replies

4. Shell Programming and Scripting

If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to... (5 Replies)
Discussion started by: jameswatson3
5 Replies

5. Shell Programming and Scripting

Help with grep inside an if loop

Hello All, I have been reading posts on here for a while, but this is my first post. I have a document in which many sentences appear, and I am piping it through an exterior script which will tag each word in the document with its part of speech (not part of my script, just background). The... (3 Replies)
Discussion started by: daf189
3 Replies

6. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

7. Shell Programming and Scripting

grep functions, how to test if succeeded

Hello ...again. I am stuck on this part, I have a loop with processes an operations file. and calls different functions depending on what is in loop, which processes a database file... #so far my add function works as intended add() { ...blah blah; } # delete is kinda working... (13 Replies)
Discussion started by: gcampton
13 Replies

8. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

9. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

Expand inside a grep

par='one" -e "two' I want to expand the variable (a little bit more complex) in a grep construction like this (obviously not working): ls |grep -e "${par}" i tried different methods with eval with no result Is there any way to archive this? Thx in advance mates! (3 Replies)
Discussion started by: Klashxx
3 Replies
Login or Register to Ask a Question