[SOLVED -- how to open a terminal before launching a Nemo script] Why can I not run a "Nemo" script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

[SOLVED -- how to open a terminal before launching a Nemo script] Why can I not run a "Nemo" script

Post by fbachofner »

I have some shell scripts which I would like to invoke from a right-click in Nemo.

To this end I have placed them into ~/.local/share/nemo/scripts and ensured they are set to executable

They show up in the Nemo context menu for "scripts," but when I click on them within that context menu, NOTHING seems to happen.

As an example, I have a "hello_world.sh" test script

Code: Select all

#!/bin/bash
clear
echo
echo "hello world"
Attempting to run this from the Nemo script context menu does zilch.

Meanwhile, invoking from terminal (e.g. " ~/.local/share/nemo/scripts/hello_world.sh " works just fine.

Insights would be appreciated.
Last edited by fbachofner on Mon Sep 02, 2024 7:06 pm, edited 1 time in total.
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
User avatar
smurphos
Level 18
Level 18
Posts: 8655
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: Why can I not run a "Nemo" script

Post by smurphos »

fbachofner wrote: Mon Sep 02, 2024 1:58 am Insights would be appreciated.
Your script is working, but it is working in the background, not in a terminal so there is nowhere for echo to print "hello-world".
You can define where you want to echo to print in the script (e.g. a text file) or use alternative tools for display on the GUI such as notify-send or zenity.

E.g. try this..

Code: Select all

#!/bin/bash
notify-send "hello world"
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi @smurphos
smurphos wrote: Mon Sep 02, 2024 2:53 am Your script is working, but it is working in the background, not in a terminal so there is nowhere for echo to print "hello-world".
OK, this makes sense and jibes with a quick test I performed with another user's Zenity-based script (I found while waiting for your response) which did work for me, as the rest of your advice suggested.

Unfortunately, my ultra-simple hello_world.sh example is not fully what I am after.

I have a number of bash scripts which are significantly more complex and take a lot of user input to then automate a series of "complex" actions. One example (simplified for discussion here):
  • takes multiple "read" inputs and places these into variables
  • concatenates these
  • creates a directory from the concatenated variable
  • creates "standardized" subdirectories within it
  • creates a filename utilizing one or two of the "read" variables
  • this file (based on a template) utilizing variable expansion is largely prefilled
  • this file is opened for futher editing
Doing this level of stuff in Zenity strikes me as a major pain in the rear and unnecessary.

So: it there a way for a script invoked from that Nemo context menu to launch in the terminal?
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi @smurphos

For what it's worth . . .

Based on your comment I looked at "system monitor" AGAIN and it indeed showed a number of instances of one of my "complex" scripts I had invoked repeatedly (without apparent effect) from the Nemo script context menu. You were absolutely right it was running; the instances were waiting for user input. I didn't notice because of how the process list was sorted. D'Oh!

Meanwhile, the simple hello_world.sh which I had looked for earlier in the system monitor wasn't there (since it just echoed some text and then immediately terminated). When I just now added an user-input requirement to it, the script then also showed up "stuck" in the process list.
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
deepakdeshp
Level 21
Level 21
Posts: 12614
Joined: Sun Aug 09, 2015 10:00 am

Re: Why can I not run a "Nemo" script

Post by deepakdeshp »

If you query chatgpt with
how to write nemo script in Mint 22 cinnamon with user input you will get an example. For user input you have to use utility zenity.

Good luck
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 22 Cinnamon 0U/8gb
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi All
fbachofner wrote: Mon Sep 02, 2024 3:40 am So: it there a way for a script invoked from that Nemo context menu to launch in the terminal?
I thought it through and there is a "messy" way to do this:
  • move the original script (i.e. hello_world.sh ) into a directory other than the Nemo scripts folder (this is to de-clutter the scripts context menu and make sure only the correct Nemo script will ever be invoked)
  • make a "helper" script -- named something like "start_hello_world"
  • the "helper" script must contain (at minimum):

    Code: Select all

    #!/bin/bash
    gnome-terminal -x bash -c "/path/to/my/scripts/hello_world.sh; exec bash"
  • make sure the "helper" script is executable
  • when invoked, the "helper" script launches a terminal window with a parameter to start the desired "real" script (hello_world.sh) within that terminal
  • this seems to work very well (not all permutations have been tested yet, however)
I actually prefer having my "real" script(s) outside of the Nemo script folder but was hoping to do that with symlinks rather than a whole host of "helper" scripts in the Nemo scripts folder.

This leaves the question: is there a more elegant solution?
Last edited by fbachofner on Mon Sep 02, 2024 5:32 am, edited 2 times in total.
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi @deepakdeshp
deepakdeshp wrote: Mon Sep 02, 2024 4:40 am For user input you have to use utility zenity.
Thanks.

I was writing a detailed solution at the same time you posted yours.

You will see from it Zenity is NOT required. My workaround is pretty darned good, but I will continue to look for something slightly more elegant.

If you want to be efficient, avoiding Zenity is somewhat critical where there are lots of inputs required within a script.
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi All:
fbachofner wrote: Mon Sep 02, 2024 4:57 am

Code: Select all

#!/bin/bash
gnome-terminal -x bash -c "/path/to/my/scripts/hello_world.sh; exec bash"
Apparently the "-x" option is deprecated

A more "future-proof" variant is as follows:

Code: Select all

#!/bin/bash

# original option
# gnome-terminal -x bash -c "/path/to/my/scripts/hello_world.sh; exec bash"

# new option
gnome-terminal -- bash -c "/path/to/my/scripts/hello_world.sh; exec bash -i"
I could not immediately determine any operational difference(s) between the deprecated variant and the updated one.
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
User avatar
smurphos
Level 18
Level 18
Posts: 8655
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: Why can I not run a "Nemo" script

Post by smurphos »

fbachofner wrote: Mon Sep 02, 2024 4:57 am This leaves the question: is there a more elegant solution?
You could have your scripts check their own operating environment, and if not in a TTY relaunch themselves in gnome-terminal.

E.g. at your script entry point....

Code: Select all

#!/bin/bash
# in a TTY?
if [ ! -t 1 ]; then
  gnome-terminal -- bash -c "$0; exec bash"
  exit
fi
# rest of script
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

Hi @smurphos
smurphos wrote: Mon Sep 02, 2024 8:10 am
fbachofner wrote: Mon Sep 02, 2024 4:57 am This leaves the question: is there a more elegant solution?
You could have your scripts check their own operating environment, and if not in a TTY relaunch themselves in gnome-terminal.

Fabulous! Thanks for the code, too -- works great!

When invoking my script from a "helper" script it was trivial to auto-close the terminal when the main script was done by invoking it (the main script) thusly:

Code: Select all

gnome-terminal -- bash -c "/path/to/my/scripts/hello_world.sh"
With your method (requiring but a single script -- which can be symlinked into the Nemo scripts folder BTW), it is also helpful to add some "exit" code for the script to terminate the terminal when the scripts actions are complete.

This can be done like so:

Code: Select all

kill -9 $PPID
inserted at the logical end of the script which "will send a SIGKILL signal to the to the script's parent process (the bash instance linked to the Terminal)" and close the terminal presuming it is the only BASH instance linked to that terminal, as discussed by user "kos" in this AskUbuntu thread
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
fbachofner
Level 3
Level 3
Posts: 113
Joined: Sun Aug 08, 2010 6:49 pm
Location: Seaside, CA

Re: Why can I not run a "Nemo" script

Post by fbachofner »

I am re-titling this thread as SOLVED with additional detail in the subject for easier searching.

Thank you all for your insights!
various self-built desktop machines and a MiniForums U550 mini-PC
each runs Fedora (current) with Cinnamon
i7 self-built server --> Fedora Server (current) (with Cinnamon, for the rare GUI login)
various VPSes --> Fedora Server (headless)
Post Reply

Return to “Scripts & Bash”