Welcome!
Important information
-- Required MX 15/16 Repository Changes
-- Information on torrent hosting changes
-- Information on MX15/16 GPG Keys
-- Spectre and Meltdown vulnerabilities

News
-- Introducing our new Website
-- MX Linux on social media: here

Current releases
-- MX-18.2 Point Release release info here
-- Migration Information to MX-18 here
-- antiX-17.4.1 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

Need some scripting help

Help for Current Versions of MX
Post Reply
User avatar
towwire
Forum Regular
Forum Regular
Posts: 502
Joined: Fri Oct 15, 2010 12:15 pm

Need some scripting help

#1

Post by towwire » Fri Mar 15, 2019 4:38 pm

Was not sure where to ask this, so move it if needed.

I'm trying to write a script that will open a terminal, run the cammand with options and keep the terminal open till you manual close it. This should be simular to when you do the updates. For now I just want the window to stay open or after the info is shown the last line said's "this terminal window can now be closed"

I've search and only found one that works but it uses "xterm" not xfce4-terminal. Funny to find I had it installed and don't remember ever installing it.

A test file [test.sh] with this in it does show a window that has to be closed but the text is very small compared to xfce4-terminal.
'xterm -hold -e ls'

The commands that I'll will use are ones that I would use in a terminal. I think that if the list can be done or even lsblk -f for testing then I should be able to work the rest out.

skidoo
Forum Guide
Forum Guide
Posts: 1336
Joined: Tue Sep 22, 2015 6:56 pm

Re: Need some scripting help

#2

Post by skidoo » Fri Mar 15, 2019 5:43 pm

At a glance https://helpmanual.io/help/xfce4-terminal/
-H or the longform --hold commandline option instructs xfce4-terminal to remain open
and
-x or longform --execute option is used to instruct "execute a command immediately after launching"
for example
-x /path/to/myscript ( or --command=/path/to/myscript ) to specify the command tobe immediately executed

xfce4-terminal -H -x /path/to/mycommand

the appropriate "Exec=" line, if you intend to create a .desktop launcher file:
Exec=xfce4-terminal -H -x /path/to/mycommand

a line within your script can handle the "ok to close now" message output:
echo "this terminal window can now be closed"

edited to add:
xfce4-terminal -H -x '/path/to/mycommand'
Whenever passing along a to-be-executed commandstring, generally it's advisable to delimit it with single quote marks.
Last edited by skidoo on Fri Mar 15, 2019 5:59 pm, edited 1 time in total.

User avatar
Jerry3904
Forum Veteran
Forum Veteran
Posts: 24412
Joined: Wed Jul 19, 2006 6:13 am

Re: Need some scripting help

#3

Post by Jerry3904 » Fri Mar 15, 2019 5:56 pm

nice helpful post
Production: 4.15.0-1-amd64, MX-17.1, AMD FX-4130 Quad-Core, GeForce GT 630/PCIe/SSE2, 8 GB, SSD 120 GB, Data 1TB
Testing: AAO 722: 4.15.0-1-386. MX-17.1, AMD C-60 APU, 4 GB
Personal: XPS 13, 4.18.0-19.3-liquorix, 4 GB

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 9865
Joined: Wed Jul 12, 2006 1:42 am

Re: Need some scripting help

#4

Post by Adrian » Fri Mar 15, 2019 6:11 pm

I think you can also use something like:

Code: Select all

xfce4-terminal -e '/path/to/mycommand'
if you have in "mycommand" script something like this at the end:

Code: Select all

read -n 1 -s -p $"Press any key to close"
That's what we use in "Quick System Info" that actually runs 'inxi -Fxrz' in a new terminal. Not much advantage over skidoo's command only that you can press a key to close the terminal.

User avatar
towwire
Forum Regular
Forum Regular
Posts: 502
Joined: Fri Oct 15, 2010 12:15 pm

Re: Need some scripting help

#5

Post by towwire » Fri Mar 15, 2019 6:55 pm

Adrian wrote:
Fri Mar 15, 2019 6:11 pm
I think you can also use something like:

Code: Select all

xfce4-terminal -e '/path/to/mycommand'
if you have in "mycommand" script something like this at the end:

Code: Select all

read -n 1 -s -p $"Press any key to close"
That's what we use in "Quick System Info" that actually runs 'inxi -Fxrz' in a new terminal. Not much advantage over skidoo's command only that you can press a key to close the terminal.
Thanks I'll try later some of this later, have other things to do. with skidoo's command I get [Failed to execute child process "Exec=xfce4-terminal"(No such file or directory)]. When you close the error's there are 2 empty windows. I will be trying to learn.

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 9865
Joined: Wed Jul 12, 2006 1:42 am

Re: Need some scripting help

#6

Post by Adrian » Fri Mar 15, 2019 7:27 pm

That's because that "Exec=" line if for a .desktop file not for running it in the terminal.

skidoo
Forum Guide
Forum Guide
Posts: 1336
Joined: Tue Sep 22, 2015 6:56 pm

Re: Need some scripting help

#7

Post by skidoo » Sat Mar 16, 2019 2:13 am

Here's a wrapper I've used for scripts launched via gksu, with a goal of getting the user into, and immediately out of, the elevated permissions environment.

Code: Select all

#!/bin/bash
### launch the script in a NON-TABBED terminal emulator (e.g. xterm)
### xterm -e '/tmp/exit-terminal-when-finished.sh'
set -eu
trap '' 2 ### disable the ability to breakout via Ctrl+c


# THE MEAT OF THE SCRIPT WILL RESIDE HERE


poof() {
  scriptpid=$$
  shellpid=$( grep PPid: /proc/${scriptpid}/status | awk '{print $2'} )
  termpid=$( grep PPid: /proc/${shellpid}/status | awk '{print $2'} )
  ###---------- for testing or demonstration, uncomment the following line
# ps -F -p "${termpid},${shellpid},${scriptpid}"; echo -e "\n";
  ###----------
  read -s -n 1 -p $"Press any key to close/exit this terminal emulator (e.g. xterm)"
  kill ${termpid}; exit
}
trap poof exit

User avatar
towwire
Forum Regular
Forum Regular
Posts: 502
Joined: Fri Oct 15, 2010 12:15 pm

Re: Need some scripting help

#8

Post by towwire » Sat Mar 16, 2019 3:00 pm

Adrian wrote:
Fri Mar 15, 2019 7:27 pm
That's because that "Exec=" line if for a .desktop file not for running it in the terminal.
Sorry that you made an assumtion that was not true. The path is /home/towwire/Desktop/ and I've tried files like test1.sh, test2.sh etc and made sure they "Allow this file to run as a program. Then make a launcher for each. It was easier to do everything on the Desktop for editing etc.

Well I'm testing some stuff on my machine this is for some that are ex-Windows happy clicker's that I'm trying to make it easy for them now. I know "xterm" is not installed on MX18.1 and it could be installed but hard to explain how to change to a larger type face for it. What I'm testing will not be transfer to them but will help me go to one of their machines and then make it for them.

If you open an 'xfce4-terminal' and type in 'xfce4-terminal' it will open another window but you can not enter anything in the first terminal until the second terminal is closed, same if you do 'xterm' there.

Here is a test that I did just see what would work in a terminal and also using a launcher. Typed into 'xfce4-terminal' [xterm -hold -e 'expressvpn status'] and an 'xterm' window will open and show the status of the VPN. Or just make a launcker with it as the command and it works. I can also make launcher's using 'xterm' with these:
'xterm -hold -e lsblk -f' or 'xterm -hold -e ls'. I have NOT been able to get any of them to work the command in a launcher or and sh file using 'xfce4-terminal', like 'Exec=xfce4-terminal -H -x /home/towwire/Desktop/test(#).sh. I seem to get blank windows or just a window flash.

OK. I would perfer using 'xfce4-terminal' and not 'xterm'. If someone can show what I need to put in an sh file and then us a launcher for like the following 'lsblk -f' or 'ls', I hope I can then figure out what to do for what I want. Will get back later.

skidoo
Forum Guide
Forum Guide
Posts: 1336
Joined: Tue Sep 22, 2015 6:56 pm

Re: Need some scripting help

#9

Post by skidoo » Sat Mar 16, 2019 3:52 pm

If the script is destined for potential use on non-Xfce systems, you can generically specify x-terminal-emulator
(a websearch "x-terminal-emulator xdg" would be enlightening here)
If someone can show
To learn by example, you can browse (open in text editor) any of the existing, working,
/usr/share/applications/*.desktop launchers ~~ in this case you're interested in one that accomplishes "open in terminal" behavior.
While browsing, note the fact that the ".desktop" files are not chmod +x (aka, are not marked "Allow this file to run as a program").
(you may already be aware of this detail, but other future readers here might not be)

Copy from /usr/share/applications/ into your ~/local/share/applications/ directory and open it in a text editor.
Note what's specified on its Exec= line, edit to suit, SaveAs,
then drag-copy your new launcher to desktop and click to test its effectiveness.
show what I need to put in an sh file
When .desktop launchers are involved, the intent is often to altogether avoid the creation of yet another one-liner "sh file".

Post Reply

Return to “MX Help”