script ends > terminal closes - I want to stop close -- How? [SOLVED]

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

script ends > terminal closes - I want to stop close -- How? [SOLVED]

Post by markfilipak »

Overview: To keep the Terminal window open in order to read/copy the results, I added this line:
read -n 1 -s -r -p "Press any key to close."
so that the entire script is:

Code: Select all

#!/bin/bash

echo Click on any part of any window to see its launch command.
ps --no-headers -o cmd $(xprop _NET_WM_PID | cut --delimiter=' ' --fields=3)
read -n 1 -s -r -p "Press any key to close."

I've tried sooooo many variations. Nothing works. The script starts fine, the crosshairs appear and I can drag it about, but the terminal session immediately closes when I click on another open window. I can't figure out how to keep it open so I can see the results. ps $(xprop _NET_WM_PID | cut -d' ' -f3) works fine when run in a preexisting terminal session.

Here is the menu item's launcher: x-terminal-emulator -e /home/mark/crosshairs.sh.

Here is the text of crosshairs.sh:

Code: Select all

#!/bin/bash

echo Click on any part of any window.
ps $(xprop _NET_WM_PID | cut -d' ' -f3)
Thank you!
Mark.
Last edited by LockBot on Mon Jan 02, 2023 11:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
wallaroo
Level 3
Level 3
Posts: 156
Joined: Thu Feb 09, 2012 3:42 am
Location: Australia

Re: script ends > terminal closes - I want to stop close -- How?

Post by wallaroo »

Try just adding 'bash' at the end of the script.

Code: Select all

#!/bin/bash

echo Click on any part of any window.
ps $(xprop _NET_WM_PID | cut -d' ' -f3)
bash
It should keep the terminal open.
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

wallaroo wrote: Sat Jul 02, 2022 11:53 pm Try just adding 'bash' at the end of the script.

Code: Select all

#!/bin/bash

echo Click on any part of any window.
ps $(xprop _NET_WM_PID | cut -d' ' -f3)
bash
It should keep the terminal open.
That works, but it doesn't. Let me explain. It does keep the terminal session going and the window open, but the text generated by ps $(xprop _NET_WM_PID | cut -d' ' -f3) never appears.
User avatar
smurphos
Level 18
Level 18
Posts: 8501
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: script ends > terminal closes - I want to stop close -- How?

Post by smurphos »

If you are running it from a menu entry skip the terminal and use zenity to display the ouput.

Code: Select all

#!/bin/bash

OUTPUT=$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut -d' ' -f3))
zenity --info --no-wrap --title="Command Line" --text="$OUTPUT"
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
wallaroo
Level 3
Level 3
Posts: 156
Joined: Thu Feb 09, 2012 3:42 am
Location: Australia

Re: script ends > terminal closes - I want to stop close -- How?

Post by wallaroo »

markfilipak wrote: Sun Jul 03, 2022 12:31 am That works, but it doesn't. Let me explain. It does keep the terminal session going and the window open, but the text generated by ps $(xprop _NET_WM_PID | cut -d' ' -f3) never appears.
That's odd - I certainly tested it before posting and it works fine for me - on both Mate and Cinnamon. :?
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

smurphos wrote: Sun Jul 03, 2022 1:04 am If you are running it from a menu entry skip the terminal and use zenity to display the ouput.

Code: Select all

#!/bin/bash

OUTPUT=$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut -d' ' -f3))
zenity --info --no-wrap --title="Command Line" --text="$OUTPUT"
Thanks so much, smurphos. Here's what I'm going to use:

Code: Select all

#!/bin/bash

echo Click on any part of any window to see its launch command.
zenity --info --no-wrap --title="" --text="$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut --delimiter=' ' --fields=3))"
$ looks/acts like it's some sort of operator. Can you enlighten me or link me?

Thanks again.

PS: Actually, I discovered I could put

Code: Select all

zenity --info --no-wrap --title="" --text="$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut --delimiter=' ' --fields=3))"
directly into the menu's "Launcher Properties" > "Command" so that no script is needed.
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

Well, this is really curious.

If I put /home/mark/crosshairs.sh into "Launcher Properties" > "Command" with this script:

Code: Select all

#!/bin/bash

echo Click on any part of any window to see its launch command.
zenity --info --no-wrap --title="" --text="$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut --delimiter=' ' --fields=3))"
I get this:
/usr/lib/firefox/firefox --profile /media/sf_LinuxFFox/ckls0yge.default-release -- the target window's full command line.

But if I put

Code: Select all

zenity --info --no-wrap --title="" --text="$(ps --no-headers -o cmd $(xprop _NET_WM_PID | cut --delimiter=' ' --fields=3))"
directly into "Launcher Properties" > "Command" with no script, I get this:
/usr/lib/firefox/firefox -- just the target window's application name (without the rest of the command line).
vanadium
Level 4
Level 4
Posts: 325
Joined: Sun Dec 27, 2015 1:07 pm

Re: script ends > terminal closes - I want to stop close -- How?

Post by vanadium »

wallaroo wrote: Sun Jul 03, 2022 1:29 am That's odd - I certainly tested it before posting and it works fine for me - on both Mate and Cinnamon. :?
Works also fine in my test.

Instead of "bash" you could use "read -n 1 -s -r -p "Press any key to continue"" to keep the terminal open until you press any key.
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

vanadium wrote: Sun Jul 03, 2022 5:44 am
wallaroo wrote: Sun Jul 03, 2022 1:29 am That's odd - I certainly tested it before posting and it works fine for me - on both Mate and Cinnamon. :?
Works also fine in my test.
I don't know what you mean by "Works also fine". Adding "bash" as a last line does keep the terminal session alive and the window open, but it breaks the function of the script. I don't know what happens to the output (i.e. the target window's command string), but it doesn't appear in the terminal session. The only thing that appears is the echo string followed by a new command prompt.
Instead of "bash" you could use "read -n 1 -s -r -p "Press any key to continue"" to keep the terminal open until you press any key.
Thank you. I'll give that a try. Maybe it will even allow selecting the displayed text and copying it without closing the window. :-)

PS: Yup! It works. I like it -- doesn't rely on zenity. Thanks!
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

Thank you, All, for excellent help. I'm signing off now. Peace, Prosperity, and Health!
vanadium
Level 4
Level 4
Posts: 325
Joined: Sun Dec 27, 2015 1:07 pm

Re: script ends > terminal closes - I want to stop close -- How?

Post by vanadium »

markfilipak wrote: Mon Jul 04, 2022 12:21 am I don't know what you mean by "Works also fine". Adding "bash" as a last line does keep the terminal session alive and the window open, but it breaks the function of the script.
It does not break the script for me: the script runs, after clicking the information is displayed and then a terminal prompt appears (because of the "bash" command). Maybe you have something in your .bashrc that clears the screen?
markfilipak
Level 6
Level 6
Posts: 1173
Joined: Sun Mar 10, 2013 8:08 pm
Location: Mansfield, Ohio

Re: script ends > terminal closes - I want to stop close -- How?

Post by markfilipak »

vanadium wrote: Mon Jul 04, 2022 4:04 am
markfilipak wrote: Mon Jul 04, 2022 12:21 am I don't know what you mean by "Works also fine". Adding "bash" as a last line does keep the terminal session alive and the window open, but it breaks the function of the script.
It does not break the script for me: the script runs, after clicking the information is displayed...
The information meaning: The command line that launched the window you clicked? Right?

No, the screen is not being cleared. It stills displays
"Click on any part of any window to see its launch command."
and then displays a prompt on the next line.
Locked

Return to “Beginner Questions”