hey guys, can anyone tell me what's wrong with my code?
im trying to test if the input ($no) is a number:

read no
echo $no | egrep - c "^[0-9]*$" | wc -l > y

y now should be 1 if the user enters a number but i get an empty string

echo 545 | grep -c ^[0-9]*$ | wc -l
is working you have put a space before -c

ah! sorry error on copy-pate!
if u try that it works, but prints " 1", not "1"
and when u put it in y u get blank string

Hi,
best way is (works only for integers)

declare -i no
read no
if [ $no = 0 ]; then
 echo "user enter 0, nothing or not number"
else
 echo "here you go"
fi

or if you want to use regexp

read no
if [[ "$no" =~ /^[.0-9]{1,}$/ ]]; then
 echo "or something like this"
fi

if you like my reply, click here ;-) : https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=pheeror%40gmail%2ecom&item_name=pheeror&no_shipping=0&no_note=1&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8

hm..well i haven't test it yet but i believe u :P
can anyone tell me whats wrong with my code tho? actually, how can i fix it? is there a trim function?

cheers

You're redirecting the output to a file called y, not into a variable $y. And do you need wc? You have the number of lines from using egrep -c.

echo "123" | awk ' /^[0-9]+$/ { print "a number" }'

thanks for the replies but i still have a problem with it

#!/usr/bin/sh
read no
echo $no | egrep -c '^[0-9]+$' > $y
echo "-->$y---"

when i give a number, this prints 1 and then -->---. It doesn't make sense. I'm redirecting the output to y but it goes to the screen. any ideas?

awk is a great solution and thanks but i'm not allowed to use it

now you redirect output of

echo $no | egrep -c '^[0-9]+$'

to file with name (value of) $y
what you want is

y=`echo $no | egrep -c '^[0-9]+$'`

or

y=$(echo $no | egrep -c '^[0-9]+$')

but you have to read some basics of bash scripting

that was so dumb of me :P thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.