0

I'd really like to understand the underlying reasons why some scripts work from the command line but not when double-clicked.

Is there an example of a script that does something simple, like echoing a variable or something like that, which would reproducibly work in the command line, but then fail to work when double-clicked?

To be clear, I mean it should run in both instances, but when double-clicked it should fail to perform its task correctly, and it should do this on any recent Ubuntu installation. I'm hoping that by being able to reproduce the behaviour I can understand it and fix it myself in the future.

1
  • This is an offshoot of this question here: askubuntu.com/questions/872129/… That question ultimately wasn't answered, even though the problem I had was solved, so I've marked it as answered because I couldn't keep troubleshooting the script once I solved the underlying problem. Jan 28, 2017 at 23:10

2 Answers 2

4

Easiest example:

  • create a file "test" and put

    #!/bin/bash
    echo "Hello World!"
    

    in it

  • make it executable with chmod 700 test
  • do ./test on commandline and it will output

    Hello world!
    

From Nautilus:

  • default: doubleclick and it will open as a text file
  • when set to "run text files" from Nautilus preferences it will flicker a couple of times and then does nothing.

But this is intended behaviour. For Nautilus you would need to create a script that does a "pop up" with the text "Hello world!" in it.

Example:

#!/usr/bin/python
import os
os.system('zenity --info --text="Hello world!!"')

would show ...

enter image description here

By the way: this also works from command line when you have a desktop. On a tty it would show an error "Failed to connect to Mir".

and fix it myself in the future

I doubt though there is something to fix. Executing in command line and executing in Nautilus are 2 different things.

2
  • Okay, maybe "echo" was a bad example. That would obviously fail because there is no command line to show the user the output. Maybe echoing to a file in a way that fails for the shell script but not for the command line? The script I had was LD_PRELOAD='/usr/$LIB/libstdc++.so.6' DISPLAY=:0 steam and nobody will tell me why it worked in the command line but failed as a script. It also worked correctly when run as a script from the command line. I would like to be able to solve this situation in the future, and I just have no useful information as to how. Jan 29, 2017 at 22:48
  • "I doubt though there is something to fix. Executing in command line and executing in Nautilus are 2 different things. " The point is that if I have a script that works from the command line but not from Nautilus, that's a problem I need to solve, and I would like to have the tools to understand how to do that. I don't want to have to enter the command line to run a script that I might be running everyday, and if the script is run automatically, then doing it from the command line just isn't an option. Jan 29, 2017 at 22:52
0

Ok I think I've got the answer... at least this solves my problem and along the way I stumbled on this and thought I would come back and post an answer.

Shell scripts do not have the same $PATH variable that the terminal does. I can change that for the purpose of the script (in my case using Jupyter notebook as an example) by doing this:

#! /usr/bin/env sh
#this adds the location where jupyter command is found
export PATH="/home/username/anaconda3/bin:$PATH" 
jupyter notebook

and that will work.

Additionally some programs may need the terminal to interact with so you could do something like:

#! /usr/bin/env sh
#this adds the location where jupyter command is found
export PATH="/home/username/anaconda3/bin:$PATH" 
xterm -e "jupyter notebook" # brings up a terminal window

And that will bring up a terminal window to display output and receive input.

Edit: I changed for completeness.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .