problem with if condition

hi, :slight_smile:
pls consider the following if statement

if [ -z $(echo $1|sed 's/[0123456789]//g') ]
then
........
else
.......

when i execute the script i am getting the following error

'(' unexpected

I am not able to find the mistake.
could anybody tell where i did mistake.

cheers
RRK

I can run your code in ksh without any problem:

#!/bin/ksh
if [ -z $(echo $1 | sed 's/[0-9]//g') ]; then
echo "Numeric"
else
echo "Non-Numeric"
fi

Output:

$./test 123
Numeric
$./test a123
Non-Numeric

Which shell you are using?

same kornshell!

I can only suggest you to check the version of ksh because i have tested your code at ksh93, if you are too at ksh93 then i'm aslo blank!!!

Regards,
Tayyab

Put a
set -o
in front of the "if" statement and post the results. The output from "set -o" will settle which shell is processing that "if" statement.

Try this instead and let's see:

if [ -z `echo $1|sed 's/[0123456789]//g'` ]

putting back quotes also giving the same error. :confused:

Can you show the trace of your script?

mmmm, I've tested tayyabq8,s script on AIX 5.2 and:

$ ./kk2.sh 123
./kk2.sh[2]: test: 0403-004 Specify a parameter with this command.
Non-Numeric

That's fixed this way:

if [[ -z $(echo $1 | sed 's/[0-9]//g') ]]; then
(...)

Have a look at:

from Perderabo.

I have tested the above on 5.3 AIX with no modifications and it works fine as it should as there is nothing wrong with it. Why the original does not work is a mystery!

Another way to test for numeric string

#!/bin/ksh
if [[ $1 = *([0-9]) ]]
then
   echo "Numeric"
else
   echo "Non-Numeric"
fi

Jean-Pierre.

It's also working fine in the bash shell too.

try by using double quotes instead of single quotes for the sed command pattern and post reply

If after changing
if [ -z $(echo $1|sed 's/[0123456789]//g') ]
to
if [ -z `echo $1|sed 's/[0123456789]//g'` ]
you are continuing to get
'(' unexpected
then you have misdiagnosed your problem. The error message and the "if" statement have nothing to do with one another. If you continue to believe otherwise, extract that "if" statement and produce a self-contained 3 or 4 line script that reproduces the error message. Post the short script and the error message and someone will help you fix it.