LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   custom shell script interpreter is not called on my vserver (https://www.linuxquestions.org/questions/linux-server-73/custom-shell-script-interpreter-is-not-called-on-my-vserver-729476/)

empgodot 05-30-2009 04:13 AM

custom shell script interpreter is not called on my vserver
 
Hi

I'm trying to write a simple shell script that compiles .cpp-files and runs the resulting program on the fly.
However on my vserver the #!/usr/local/bin/cppscript prefix in my test.cpp file seems to be ignored and sh is used for executing the test.cpp:

Quote:

$ ./test.cpp
./test.cpp: line 5: syntax error near unexpected token `('
./test.cpp: line 5: `int main(int argnum, char **args)'
This happens only on my vserver, on my laptop it works just as expected, which makes me think it could be related to the vserver kernel or its setup.
When I run the interpreter directly as follows, it also works fine:
/usr/local/bin/cppscript test.cpp
or just
cppscript test.cpp

Another strange thing is when I remove cppscript from /usr/local/bin I do get the "bad interpreter" error.

kernel versions are:
on the vserver: Linux h1469143.stratoserver.net 2.6.18-028stab060.2 #1 SMP Tue Jan 13 10:24:09 MSK 2009 i686 GNU/Linux
on my laptop: Linux tigger 2.6.27-11-generic #1 SMP Wed Apr 1 20:57:48 UTC 2009 i686 GNU/Linux
both linuxes are ubuntus.
permissions are set to 755 for both cppscript and test.cpp on both machines.

here's the test.cpp:
Code:

#!/usr/local/bin/cppscript

#include <iostream>

int main(int argnum, char **args)
{
  std::cout << "Hallo Welt! (2.0)" << std::endl;
  for(int i = 0; i < argnum; ++i)
    std::cout << "argument " << i << ": " << args[i] << std::endl;
}

and here's the cppscript for compiling and running on the fly:
Code:

#!/bin/sh
echo running cppscript

# possibly removing ./ before script name
SCRIPT=`echo "$1"|sed "s/\\.\\///"`
FILENAME="/var/tmp/cppscript_$SCRIPT_`stat -c %Y_%s $1`"
SRC=$FILENAME.cpp
cat $1|grep -v \#! >> $SRC

# compile
if [ ! -e $FILENAME ]
then
        echo compiling...
        g++ -o $FILENAME $SRC
fi
shift

# collect parameters
while [ $# != 0 ] 
do
        PARAMS="$PARAMS \"$1\""
        shift
done

# run with parameters
echo $PARAMS|xargs $FILENAME

Any ideas what is happening here?

doc.nice 05-31-2009 07:32 AM

just a guess:
maybe your sample cpp script is in utf-8 encoding, but your vserver is not using utf8? this would mean the first character is not "#" (because of the double-byte encoding) and thus not read as shebang to call your interpreter?

empgodot 05-31-2009 07:40 AM

I don't think that's it, because when I change the interpreter to some other interpreter like #!/usr/bin/php it gets executed with that interpreter and when I change it to something nonexisting like #!/usr/local/bin/cppscript2 I get a "bad interpreter" error.


All times are GMT -5. The time now is 10:25 PM.