Operating System - HP-UX
1820539 Members
3680 Online
109626 Solutions
New Discussion юеВ

Re: `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
Yogeeraj_1
Honored Contributor

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

oops!
sorry should have said "dos2ux"

dos2ux iftest2
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Christian Vollmar
New Member

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

Hi Tom

the good message is: Is was the ftp-transfer!
the bad one ist that i didnt think about this first.
I never had problems with this before and the "tranfer mode auto" button in my ftp client always worked well - until yesterday

Thanks for opening my eyes ;-) This has saved my weekend - enjoy yours!

Is there a way to detect such problems in a file, cat and more showed the correct code?
If it ain't broken, don't fix it
Ian Dennison_1
Honored Contributor

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

OK if you want to strip out the control chars, you can also do a 'strings' command of the file.

strings chartest1 >chartest2

Does any conditional work? How about a numeric test?

export TEST=1
if (( $TEST == 1 ))
then
print "test is one"
else
print "test not one"
fi

I would be tempted to look at environment variables and the shell you are using. Can you tell us which shell you are logged in as, and what the output of the 'env' statement is?

Share and Enjoy! Ian
Building a dumber user
Robin Wakefield
Honored Contributor

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

Hi Christian,

Use the view command (readonly vi) - that will show you the ^M character.

Rgds, Robin.
Rodney Hills
Honored Contributor

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

If you used "vi" to edit the file it would show any "control" characters including ^M.

-- Rod Hills
There be dragons...
Darrell Allen
Honored Contributor

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

Hi Christian,

Sounds like you are editing your scripts / files on a PC then transferring them to UNIX. This is common for new UNIX users. If so, I suggest you bite the bullet and learn vi. It's not the world's most user-friendly text editor, especially if you're used to a word processor on your PC, however it's available on every "UNIX like" system whether you use a GUI or not.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
James R. Ferguson
Acclaimed Contributor

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

Hi Christian:

Yet another way to view control characters in a file is with 'cat -vt'. A carriage return shows as ^M and a tab character appears as ^I.

A useful reference is 'man ascii(5)'.

Regards!

...JRF...
Christian Vollmar
New Member

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

You got me!
Indeed I edited the files on an PC and transferred them to the HP - this worked fine for all the etc/*s and other config files I had to edit during the last weeks.
I even started with vi and use it every time when i am at the HP-box, but it runs a EEG-recording software 24/7 and is occupied by a technician most time, so I hav to use telnet. And vi causes problems with my telnet client (CRT for Win) - so I used a Win-Editor with built-in ftp-client.

Well at least I am now aware of the problem - strange that it didnt appear earlier ;-)

Thanks to all of you and enjoy your weekend

Christia
If it ain't broken, don't fix it
Rodney Hills
Honored Contributor

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

Many systems will ignore a trailing ^M, but I would recommend you go back and fix those /etc files anyway... Just in case...

my 2 cents

-- Rod Hills
There be dragons...
Tom Geudens
Honored Contributor

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

Hi Christian,
I noticed you're pretty new to the forums. May I point out the following link ?
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x022718276953d61190040090279cd0f9,00.html

This explains (amongst other things) about the points system.

Tom
A life ? Cool ! Where can I download one of those from ?