Running Groovy script from the command line - unix

When I did which groovy, I got the below output:
/usr/local/bin/groovy
So I went ahead and created a helloworld.groovy with the below content
#!/usr/local/bin/groovy
println "hello world"
After that I did chmod +x helloworld.groovy and attempted to run the file with ./hellworld.groovy and sadly, I got this error ./helloworld.groovy: line 2: print: command not found
I could get rid of the error by changing to
#!/usr/bin/env groovy
println "hello world"
Why would the first method cause the error?

You need to run the script like this:
groovy helloworld.groovy

#!groovy
println("hello world!")
$ chmod +x script.groovy
$ ./script.groovy

It will work on Linux kernel 2.6.28 (confirmed on 4.9.x). It won't work on FreeBSD and other Unix flavors.
Your /usr/local/bin/groovy is a shell script wrapping the Java runtime running Groovy.
See the Interpreter Scripts section of EXECVE(2) and EXECVE(2).

#!/bin/sh
sed '1,2d' "$0"|$(which groovy) /dev/stdin; exit;
println("hello");

Related

What does `python3 ()` do?

While trying to execute a timeit command on the command line using the python command line interface I accidentally put .function() on the outside of the command like so:
$ python3 -m timeit '<code>'.function()
Rather than the timeit command being executed, I was prompted as such:
function>
Thinking I had entered the python repl I tried to quit with q. Yes, I'm aware quit() is the correct way to do this. Having returned to the command line, I noticed the error and corrected it like so:
$ python3 -m timeit `<code>.function()`
I expected this code to execute correctly, but instead I received the following error:
python3:7: command not found: q
After discussing it with some colleagues, it was suggested that I check which python was being used:
$ which python3
python3 () {
q
}
This was not what I was expecting! Normally the result would be /usr/local/bin/python3. Through some trial and error I was able to determine that the minimal case to reproduce this is:
$ python3 ()
function> q
$
Now that the context is out of the way, I have two questions about the behaviour I witnessed:
1. What exactly does python3 () do?
2. How do I return execution to its original state in the same terminal window? I'm aware I can open a new terminal window and the original state exists in that window.
The syntax foo () is used in POSIX-compliant shells (such as bash, dash, and zsh) to define a function. Your entire snippet defines a function called python3 and executes the command q when it's ran. You can bypass shell functions and aliases using the command command: command -p python3 myfile.py
To remove the function from the current shell process, you can use unset -f python3. If it keeps coming back after starting new shells, then it's likely defined in one of you shell initialization files.

Error while running a .sh script via QProcess

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}

Raspberry PI: PHP call python script with sudo

i'm running Nginx on my Raspberry PI with PHP 5.4.14 (fpm-fcgi). It all works well. But in one script i make a shell call using shell_exec.
echo shell_exec("sudo python " . $file);
it works well if i use the console with
php test.php
but if i use the browser to call this file the php-file will be executed but i don't get any output by the shell_exec (not even an error message).
So i hope you could help me fix this problem.
The called python file makes use of the GPIO so sudo is required, right?
Here is my python code:
#!/usr/bin/env python
try:
import RPi.GPIO as GPIO
except RuntimeError:
print "Error Importing GPIO. Did your forgot the superuser privilieges?"
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(3, GPIO.OUT)
GPIO.output(3, not GPIO.input(3))
print "New LED state is: ", GPIO.input(3)
#GPIO.cleanup()
for future reference:
I found a - i my eyes - not really nice solution:
sudo echo "apache ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Found here.
Mm, i have try with this two simple example:
/Scripts/test.py
#!/usr/bin/env python
print (str('hello world'))
/var/www/test.php
<?php
echo shell_exec('/Scripts/test.py');
?>
via a browser, show me "hello world"
Perhaps try this:
chmod a+x your_py_file.py
Hope that help..

Pydev Eclipse console does not support curses.setupterm

I cannot run a script in Eclipse that works perfectly in a terminal.
It seems that Eclipse console cannot support some functions. I am looking for a workaround to be able to debug the script using Pydev.
Is it possible to set PyDev to use for example /usr/bin/gnome-terminal instead of the Eclipse native console ?
Otherwise it there a way to define a wrapper as a python interpreter for PyDev that will launch an terminal external to Eclipse (I've tried but failed on that).
Thank you
Nga
Right now, curses-based apps really don't run well inside Eclipse/PyDev, so, you must really launch it externally. To debug you can use the remote debugger: http://pydev.org/manual_adv_remote_debugger.html
If you use Aptana Studio, there's a terminal view which should emulate a terminal better... try running python (i.e.: running your program) from inside that view. If it does work properly there, maybe I could check a way to better integrate there and launch directly in that view.
Thank you for your reply. I have finally defined kind of a wrapper as a bash script calling python in a xterm. Pydev is checking some configuration by calling eclipse/plugins/org.python.pydev_2.4.0.2012020116/PySrc/interpreterInfo.py so the script first echo the format expected by PyDev. here is the script "
#!/bin/bash
# dummy return for Eclipse Pydev - respect interpreter info format
echo "EXECUTABLE:/home/user/python_custom/python_xterm|
|/home/user/eclipse/plugins/org.python.pydev_2.4.0.2012020116/PySrc
|/usr/lib/python2.5
|/usr/lib/python2.5/plat-linux2
|/usr/lib/python2.5/lib-tk
|/usr/lib/python2.5/lib-dynload
|/usr/local/lib/python2.5/site-packages
|/usr/lib/python2.5/site-packages
|/usr/lib/python2.5/site-packages/Numeric
|/usr/lib/python2.5/site-packages/PIL
|/usr/lib/python2.5/site-packages/gst-0.10
|/var/lib/python-support/python2.5
|/usr/lib/python2.5/site-packages/gtk-2.0
|/var/lib/python-support/python2.5/gtk-2.0
|/var/lib/python-support/python2.5/HTMLgen
|/var/lib/python-support/python2.5/pyinotify
|/usr/lib/python2.5/site-packages/wx-2.6-gtk2-unicode
|/usr/lib/site-python
#
$
|__builtin__
|__main__
|_ast
|_codecs
|_sre
|_symtable
|_types
|errno
|exceptions
|gc
|imp
|marshal
|posix
|pwd
|signal
|sys
|thread
|xxsubtype
|zipimport
"
# activate scrollbar -sb with 6000 lines
# allow logging -l with filename log_$NOW
xterm -g 150x100+0+0 -sb -sl 6000 -si -hold -e "python $*"
that does the job, and I can use Pydev and its debugger

Cygwin: Unable to find Java path.

I am using cygwin on windows 7 env and it is unable to locate java, error message i get is that
ERROR: /cygdrive/c/Program
Files/Java/jdk1.6.0_22 does not exist!
wired path is if I do echo $JAVA_HOME then it shows me
$ echo $JAVA_HOME
/cygdrive/c/Program Files/Java/jdk1.6.0_22
not sure what is happening here, any suggestions?
The problem is that the pathname contains spaces. You need to escape the spaces as described here:
http://www.cygwin.com/faq/faq.using.html#faq.using.filename-spaces
Adding this line to your .bashrc should do it:
export JAVA_HOME='/cygdrive/c/Program Files/Java/jdk1.6.0_22'
Edit: You could try running this script which I found in this blog post:
case "`uname`" in
CYGWIN*) cygwin=true ;;
esac
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
CLASSPATH=`cygpath --windows --path "$CLASSPATH"`
fi
Use the old school way:
export JAVA_HOME=/cygdrive/c/Progra~1/Java/jdk1.6.0_22
It worked for me.

Resources