LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Script with curl works on terminal but not as menu launcher. (https://www.linuxquestions.org/questions/linux-general-1/shell-script-with-curl-works-on-terminal-but-not-as-menu-launcher-4175667112/)

portaro 01-04-2020 07:44 PM

Shell Script with curl works on terminal but not as menu launcher.
 
Hello friends Happy New Year.

I have a question because I have a problem to apply a simple script as a launcher click to execute in terminal and auto use curl that have wttr.in as target.

If I directly put this command on terminal he works →

Code:

$ bash -x /home/joao/.config/tempo.sh
This tempo.sh have this lines →

Code:

#!/usr/bin/env bash
curl  https://wttr.in/chaves

The problem when I try to do a launcher to execute the script he simply open the terminal and do nothing. Here is the launcher sintax →

Code:

[Desktop Entry]
Name=Tempo-Chaves
Exec=bash -c /home/joao/.config/tempo.sh
Comment=
Icon=
NoDisplay=false
Type=Application
Terminal=true
Categories=Utility;

I already try chage the Exec line with this →

Exec=/usr/bin/bash -c /home/joao/.config/tempo.sh

Exec=/usr/bin/env bash -c /home/joao/.config/tempo.sh

Exec=lxterminal -e "bash -c '/home/joao/.config/tempo.sh;$bash' "

...

So now i'm curious why script works if I directly use the command with path on terminal but not as a launcher.

Thanks.

scasey 01-04-2020 08:08 PM

Try providing the full path to curl in the script...

ondoho 01-05-2020 05:07 AM

is tempo.sh marked executable?
Code:

chmod +x tempo.sh

Turbocapitalist 01-05-2020 05:30 AM

Quote:

Originally Posted by ondoho (Post 6074805)
is tempo.sh marked executable?
Code:

chmod +x tempo.sh

That would be the first thing to check.

The second would be to have it use the interpreter directly instead of depending on an indirect reference from the environment:

Code:

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin

curl https://wttr.in/chaves

The $PATH part is optional but often a good idea for scripts which will be used in an automated manner.

Then invoke the script like this:

Code:

Exec=/home/joao/.config/tempo.sh
And one more thing, the placement of your script would be better /home/joao/bin/ instead. Everything has a place. If you need the output of curl to be within /home/joao/.config/ then you can save it there:

Code:

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin

curl https://wttr.in/chaves > /home/joao/.config/chaves


portaro 01-05-2020 09:16 AM

Quote:

Originally Posted by Turbocapitalist (Post 6074812)
That would be the first thing to check.

The second would be to have it use the interpreter directly instead of depending on an indirect reference from the environment:

Code:

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin

curl https://wttr.in/chaves

The $PATH part is optional but often a good idea for scripts which will be used in an automated manner.

Then invoke the script like this:

Code:

Exec=/home/joao/.config/tempo.sh
And one more thing, the placement of your script would be better /home/joao/bin/ instead. Everything has a place. If you need the output of curl to be within /home/joao/.config/ then you can save it there:

Code:

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin

curl https://wttr.in/chaves > /home/joao/.config/chaves


I try all your suggestions and dont work the comman /home/joao/.config/tempo.sh or /home/joao/bin/tempo.sh specified directly on terminal works but in a launcher only opens terminal and do nothing.

I edit the file like this →

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/bin:/bin
curl https://wttr.in/chaves

And I try to put the file on a /bin path on my current user and I also modify the launcher Exec command.

No work.

Of course I already check the permission execution mentioned on command → chmod +x ...

Thanks for help.

pan64 01-05-2020 09:18 AM

I would add some echo commands to that script, like echo started.
And also I would redirect its output into a logfile.
Adding set -xv at the beginning also may help (to find the reason).

michaelk 01-05-2020 10:13 AM

It does work, you just can't see it.

I assume when you click on the shortcut, a terminal window opens, appears to do nothing and then closes.

This is normal.

Quote:

curl https://wttr.in/chaves > /home/joao/.config/chaves
With this command the terminal will still open and appear to do nothing but the output will be written to a file. I would pick another location but it should still work.

If you want to see the output in the terminal window you need to pause your running script
Code:

#!/bin/bash

curl https://wttr.in/chaves
read -p "Press any key to continue"


portaro 01-05-2020 10:31 AM

I Tried the code script →

#!/bin/bash

curl https://wttr.in/chaves
read -p "Press any key to continue"

And still do nothing the "Press any key to continue" dont appear also and the terminal open and do nothing.

michaelk 01-05-2020 10:41 AM

Does the script still work from the command line but not the launcher?

As a frame of reference I am testing your script on Ubuntu version 19.

portaro 01-05-2020 10:59 AM

Quote:

Originally Posted by michaelk (Post 6074914)
Does the script still work from the command line but not the launcher?

As a frame of reference I am testing your script on Ubuntu version 19.

Yes in command line directly works well on launcher not.

pan64 01-05-2020 11:26 AM

how do you know that?

portaro 01-05-2020 11:38 AM

Quote:

Originally Posted by pan64 (Post 6074929)
how do you know that?

In terminal you target the script in my case now I have two paths :
/home/joao/.config/tempo.sh
/home/joao/bin/tempo.sh

the content of scripth (.sh file) is →

Code:

#!/bin/bash

curl https://wttr.in/chaves
read -p "Press any key to continue"

When I open a terminal like lxterminal and put the target script he runs →

$/home/joao/.config/tempo.sh

You can test this on your system copy the script, put on a file .txt like tempo.txt, rename the file to .sh like tempo.sh view permissions or pass the chmod command to the file execution permissions - chmod +x /path/to/your/file.sh.
Copy te file and put on a path of your current user /home/user/tempo.sh or ~ /tempo.sh and run it (~ - means your current user folder - home).

So because this I can see the script works when I run it specifiyng directly the path on terminal but for some reason when I do a a launcher to be executed in terminal he dont works.

pan64 01-05-2020 11:50 AM

Because the launcher does not use any terminal (you will not see the result in your terminal).
You need to save the downloaded content as it was suggested in post #4 and/or you can add debug info (post #6).
Also you can specify a terminal in the Exec line, something like this:
Code:

Exec=lxterminal -e "bash -c '/home/joao/.config/tempo.sh' "
but you ought to try this command in your terminal if works

portaro 01-05-2020 12:07 PM

Quote:

Originally Posted by pan64 (Post 6074940)
Because the launcher does not use any terminal (you will not see the result in your terminal).
You need to save the downloaded content as it was suggested in post #4 and/or you can add debug info (post #6).
Also you can specify a terminal in the Exec line, something like this:
Code:

Exec=lxterminal -e "bash -c '/home/joao/.config/tempo.sh' "
but you ought to try this command in your terminal if works

In fact I already try the Exec line that you post I put on the first post a similar launcher execution →

Exec=lxterminal -e "bash -c '/home/joao/.config/tempo.sh;$bash' "

Dont work on launcher open terminal only do nothing.

In terminal here the outputs →
$ "bash -c '/home/joao/.config/tempo.sh' "bash: bash -c '/home/joao/.config/tempo.sh' : Ficheiro ou directoria inexistente

$ bash -c '/home/joao/.config/tempo.sh'
Weather report: chaves ... → works fine

pan64 01-05-2020 01:31 PM

yes, what you entered is wrong. You need to modify it. That's why I told you to test it.


All times are GMT -5. The time now is 03:45 PM.