Page 1 of 1

Syntax error in C shell script

Posted: Mon 11 Jun 2012, 07:50
by tytower
The following file is giving a syntax error on line 10

Code: Select all

#!/bin/csh
# Environment path variables for the Qt package:
if ( ! $?QT4DIR ) then
    # It's best to use the generic directory to avoid
    # compiling in a version-containing path:
    if ( -d /usr/lib/qt ) then
        setenv QT4DIR /usr/lib/qt
    else
        # Find the newest Qt directory and set $QT4DIR to that:
        foreach qtd ( /usr/lib/qt-* )  ********************************
            if ( -d $qtd ) then
                setenv QT4DIR $qtd
            endif
        end
    endif
endif
set path = ( $path $QT4DIR/bin )
if ( $?CPLUS_INCLUDE_PATH ) then
    setenv CPLUS_INCLUDE_PATH $QT4DIR/include:$CPLUS_INCLUDE_PATH
else
    setenv CPLUS_INCLUDE_PATH $QT4DIR/include
endif
I don't understand how that line works or even why it is being seen as the previous condition has been met . Can anyone explain it for me?
Especially the " qt-*" bit- How does it get a list from that?

The qt4 directory is set OK to /usr/lib/qt

Posted: Mon 11 Jun 2012, 08:36
by amigo
You don't quote the error, so we can't help much. Also, the script uses csh which is not very common anymore. You might wish to convert the script to use bash instead -of course you need to understand exactly what the script is supposed to do...
It looks as if it is a script for setting up the qt4 environment -probably being run from /etc/profile.d ??? If so, there are also bash/shell versions of that script around.

Posted: Mon 11 Jun 2012, 10:05
by tytower
Yep thanks amigo --all true

I want to understand why line 10 does not work.
foreach qtd ( /usr/lib/qt-* )

In particular the " qt-* " piece
the * of course is a match any character
the qtd is a variable to hold the directory list element which is part of a list supplied by /usr/lib/qt-*

I dont know what the "-" does or if it is correct

The error message is as stated - Syntax error in line 10

Re: Syntax error in C shell script

Posted: Mon 11 Jun 2012, 11:21
by jafadmin
Line 9 should be commented ..

Code: Select all

#!/bin/csh
# Environment path variables for the Qt package:
if ( ! $?QT4DIR ) then
    # It's best to use the generic directory to avoid
    # compiling in a version-containing path:
    if ( -d /usr/lib/qt ) then
        setenv QT4DIR /usr/lib/qt
    else
    #  Find the newest Qt directory and set $QT4DIR to that:
        foreach qtd ( /usr/lib/qt-* )  
            if ( -d $qtd ) then
                setenv QT4DIR $qtd
            endif
        end
    endif
endif
set path = ( $path $QT4DIR/bin )
if ( $?CPLUS_INCLUDE_PATH ) then
    setenv CPLUS_INCLUDE_PATH $QT4DIR/include:$CPLUS_INCLUDE_PATH
else
    setenv CPLUS_INCLUDE_PATH $QT4DIR/include
endif

Posted: Mon 11 Jun 2012, 17:51
by Keef
It looks like that section of code is checking for a generic qt directory, or if there isn't one, use one with a version number, eg qt-4.3 or whatever.
I'm sure it could be written in many different ways.

Posted: Mon 11 Jun 2012, 20:55
by tytower
Yes line 9 is commented . It was my mistake getting the code block to post right

Keef I agree thats what it is trying to do but it does not parse properly . Can you offer any correction or other approach ?

Here is the bash script for the same thing-It works fine

Code: Select all

#!/bin/sh
# Environment variables for the Qt package.
#
# It's best to use the generic directory to avoid
# compiling in a version-containing path:
if [ -d /usr/lib/qt ]; then
  QT4DIR=/usr/lib/qt
else
  # Find the newest Qt directory and set $QT4DIR to that:
  for qtd in /usr/lib/qt-* ; do
    if [ -d $qtd ]; then
      QT4DIR=$qtd
    fi
  done
fi
if [ ! "$CPLUS_INCLUDE_PATH" = "" ]; then
  CPLUS_INCLUDE_PATH=$QT4DIR/include:$CPLUS_INCLUDE_PATH
else
  CPLUS_INCLUDE_PATH=$QT4DIR/include
fi
PATH="$PATH:$QT4DIR/bin"
export QT4DIR
export CPLUS_INCLUDE_PATH

Posted: Mon 11 Jun 2012, 21:29
by Keef
I don't have qt, but it tried the first part of the script with empty folders (qt first, then qt-4)

Code: Select all

#!/bin/sh 
# Environment variables for the Qt package. 
# 
# It's best to use the generic directory to avoid 
# compiling in a version-containing path: 
if [ -d /usr/lib/qt ]; then 
  QT4DIR=/usr/lib/qt 
else 
  # Find the newest Qt directory and set $QT4DIR to that: 
  for qtd in /usr/lib/qt-* ; do 
    if [ -d $qtd ]; then 
      QT4DIR=$qtd 
    fi 
  done 
fi 
####################
echo "QT4DIR=" $QT4DIR
It came up with '/usr/lib/qt' then '/usr/lib/qt-4' as expected.

In fact if I run the whole script and then 'echo' PATH and CPLUS_INCLUDE_PATH
they look to be correct.

Code: Select all

QT4DIR= /usr/lib/qt-4321
PATH: /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R7/bin:/root/my-applications/bin:/usr/games:/usr/lib/qt-4321/bin
CPLUS_INCLUDE_PATH:  /usr/lib/qt-4321/include

Posted: Mon 11 Jun 2012, 21:54
by tytower
Yes the bash script runs with no errors and gets it right
The csh script however is the one giving the error.

I notice on my machine in /etc/profile.d/ I have both versions of the script
I think an extra has been brought in from KeePassx or KeePass2 which I was trying to install

I also notice each time I open the console I get the error message in the console so it must run all the profile.d scripts each time it opens .

That is telling me I don't need qt4.csh in there anyway as it is getting set up by qt4.sh . So I removed it and am now not getting the error messages

Would still like to know why line 10 of qt4.csh gives an error as it looks OK to me too.

Posted: Mon 11 Jun 2012, 22:06
by Keef
I think it may just be that csh is just a symlink to bash, and it doesn't like the different format.
'#!/bin/csh' won't run the script on Wary 5.3, but changing it to #!/bin/sh gives

Code: Select all

 line 10: syntax error near unexpected token `('
Can't think of anything else.

Posted: Tue 12 Jun 2012, 03:50
by tytower
I can get it to pass through by removing the brackets but that gives me an unexpected end of file message?