Operating System - HP-UX
1756040 Members
4524 Online
108839 Solutions
New Discussion юеВ

`if' is not matched - message in shell script

 
SOLVED
Go to solution
Christian Vollmar
New Member

`if' is not matched - message in shell script

Hi gurus,

I am quite new to hp-ux and have the following problem:
I try using a simple if then else fi loop in a shell script under HP-UX 10.20
when I copy and past the code in a shell window everything works:

# if [ -f /home/vanadm/checkspace ]
> then
> echo yes
> else
> echo no
> fi
no

but when I try to run it as shell script (the code above saved in /home/vanadm/iftest) I get two errors:

1.
if the first script line contains a: #!/sbin/sh or #!/usr/bin/sh
I get:
# /home/vanadm/iftest
script interpreter "/usr/bin/sh" not found
sh: /home/vanadm/iftest: not found.


2.
if I remove the #!/xxx line the script is executed and returns:
# /home/vanadm/iftest
/home/vanadm/iftest: Syntax error at line 1 : `if' is not matched.

I am root, all files are rwxrwxrwx, and /usr/bin/sh _is_there_ ! ;-)
This `if' is not matched. drives me crazy. I tried the if [ xxx ] and the if test xxx syntax with no difference
I tried using multiple lines as above and a one-liner (if ; then ; else ; fi) - with no difference too

What specific Posix-Shell problem do I miss? (however, the problem remains the same using csh)

Thanks for any help

Christian Vollma
If it ain't broken, don't fix it
19 REPLIES 19
Ian Dennison_1
Honored Contributor

Re: `if' is not matched - message in shell script

Christian,

Can you put the entire listing of the script in a posting?

We can then have a look at the actual code. My personal style for testing conditions such as file existing is to use double square brackets - however I do not know if that affects the result you get.

Share and Enjoy! Ian
Building a dumber user
john korterman
Honored Contributor

Re: `if' is not matched - message in shell script

Hi Christian,
the unmatched if-error indicates that the shell cannot "see" the rest of the if- sentence, in particular not the "fi". It probably derives from your cut and paste where you probably paste some garbage into the script as well. Could you please try and write it manually in a fresh file?

regards,
John K.
it would be nice if you always got a second chance
Tom Geudens
Honored Contributor

Re: `if' is not matched - message in shell script

Hi Christian,
That is indeed strange ... the same script seems to work for me.
I've attached it. Can you execute it with
sh -x iftest.sh
and post the output ?

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
UNIX Engr
Advisor

Re: `if' is not matched - message in shell script

Try using #!/bin/sh as your first line. The specific code your referenced looks good enough.

Craig
Christian Vollmar
New Member

Re: `if' is not matched - message in shell script

Hi Ian,

whow, thats a response time!

The complete code is stored in /home/vanadm/iftest:

if [ -f /home/vanadm/checkspace ]
then
echo yes
else
echo no
fi

I have just tried double brackets an things changed to

' is not expected.rror at line 2 : `

(No mistyping, this is an exact paste from the shell window!)
There still seems to be a syntax problem!?!!

The problem does not depend on the condition I test, I tried several tests.

regs n thnx

Christian
If it ain't broken, don't fix it
Christian Vollmar
New Member

Re: `if' is not matched - message in shell script

Hi Tom & John

I have just used Toms script in a fresh file (and my editor shows that there are no foreign characters or other garbage)
Here is the result:

# sh -x iftest.sh
+ [ -f /home/sys/checkspace ]
iftest.sh: test: A ] character is missing.
+
iftest.sh[4]: ^M: not found.
+ echo no
no
#

Any more ideas?
If it ain't broken, don't fix it
Tom Geudens
Honored Contributor
Solution

Re: `if' is not matched - message in shell script

Hi Christian,
You need to ftp the script to your server as ASCII, not binary. The ^M is a typical conversion error.

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Yogeeraj_1
Honored Contributor

Re: `if' is not matched - message in shell script

hi,

try to do a ux2dos on the script and try again.

dos2ux outfile


regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
john korterman
Honored Contributor

Re: `if' is not matched - message in shell script

Hi again,
can you confirm that you actually typed the code manually?
Sorry, I have to ask, but your last response hints that you did not.

regards,
John K.
it would be nice if you always got a second chance