You are not logged in.

#1 2019-10-04 12:33:37

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Open Terminal with pre-written command in it [SOLVED]

Hi,
How can i open a terminal (xfce4,gnome,kde konsole or etc, terminals) with a pre-written command in it ?

example :

xfce4-terminal MYCOMMAND="sudo su"

i want something like above, that when i run it, i would be able to open an xfce4-terminal with command "sudo su" pre-written in it, so that i can hit enter and run the command

note: i dont want to run a command by script or ..., i just want to show a terminal with a command that user can press enter to run it, nothing more

Last edited by erfanjoker (2019-10-05 14:03:33)

Offline

#2 2019-10-04 12:40:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,583
Website

Re: Open Terminal with pre-written command in it [SOLVED]

If needing to press enter is a requirement, it really can't be done like that.  You could fake it with some variation of `xfce4-terminal -e /bin/bash -c "echo -n \"sudo su"; sudo su`.  Alternatively you could use xdotool to simulate key presses on the terminal to "type" in that line.

But this sounds a lot like an X-Y problem.  First note that `sudo su` itself doesn't make much sense.  What are you actually trying to do?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-10-04 18:13:49

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

Im trying to make my own shortcuts, for example when i press some keys, i want to open terminal with command : pacman -Syyu
but i want the user submittion by pressing enter to run the command, not just running the command by its own

Offline

#4 2019-10-04 18:37:12

ProgrammAbel
Member
From: UK
Registered: 2018-12-01
Posts: 21

Re: Open Terminal with pre-written command in it [SOLVED]

Using the -e and -H flags can achieve this, like so:

xfce4-terminal -H -e "pacman -Syu"

Next time, try checking the man pages when you face a problem like this. It'll save you a bunch of time wink

Offline

#5 2019-10-04 18:43:11

progandy
Member
Registered: 2012-05-17
Posts: 5,211

Re: Open Terminal with pre-written command in it [SOLVED]

Maybe you can run expect in the terminal and write a script that spawns a shell, does some things and then gives control to you?


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#6 2019-10-04 19:21:39

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

ProgrammAbel wrote:

Using the -e and -H flags can achieve this, like so:

xfce4-terminal -H -e "pacman -Syu"

Next time, try checking the man pages when you face a problem like this. It'll save you a bunch of time wink

I need user to hit enter to run the command, this will open the terminal running that command

Offline

#7 2019-10-04 19:23:01

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

progandy wrote:

Maybe you can run expect in the terminal and write a script that spawns a shell, does some things and then gives control to you?

I dont really get what you mean, can you please explain more ?

Offline

#8 2019-10-04 19:23:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,583
Website

Re: Open Terminal with pre-written command in it [SOLVED]

ProgrammAbel wrote:

Using the -e and -H flags can achieve this

No, it will not.  The OP wants the command to *not* execute until enter is pressed.  -H just keeps the terminal open after the command has run to completion.

Just add a read command or similar:

xfce4-terminal -e /bin/bash -c "read -p \"Press Enter to run 'sudo pacman -Syu'\" && sudo pacman -Syu"

Last edited by Trilby (2019-10-04 19:25:26)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2019-10-04 19:57:59

seth
Member
Registered: 2012-09-03
Posts: 51,984

Re: Open Terminal with pre-written command in it [SOLVED]

If you don't hang on the specific behavior, I'd suggest to invoke "dialog" since it can provide the user w/ context and a TUI to ok/cancel.
You can even take the sudo password from there.

Offline

#10 2019-10-04 20:06:22

progandy
Member
Registered: 2012-05-17
Posts: 5,211

Re: Open Terminal with pre-written command in it [SOLVED]

erfanjoker wrote:

I dont really get what you mean, can you please explain more ?

expect can be used to automate terminal input and react to output, for example like this:

xterm  -e /usr/bin/expect -c 'spawn /usr/bin/bash ; expect "$ " ; send "pacman -Syu" ; interact ; exit'

Edit: Trilby has a probably better solution, just don't forget -H to keep the terminal open so you can read the pacman output.

Last edited by progandy (2019-10-04 20:09:26)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#11 2019-10-04 21:34:30

peterklarc
Member
Registered: 2017-03-28
Posts: 28

Re: Open Terminal with pre-written command in it [SOLVED]

I know you don't want a script but what's the problem with a script? Using this for years:

#!/bin/bash
lxterminal --geometry=80x25 --title="Updating" \
  -e bash -c "echo 'Command: pacman -Syyu';sudo pacman -Syyu; echo "---------"; echo '[Press <Enter> to Close Terminal]'; read line" &

Offline

#12 2019-10-04 21:41:01

loqs
Member
Registered: 2014-03-06
Posts: 17,513

Re: Open Terminal with pre-written command in it [SOLVED]

peterklarc wrote:

Using this for years:

#!/bin/bash
lxterminal --geometry=80x25 --title="Updating" \
  -e bash -c "echo 'Command: pacman -Syyu';sudo pacman -Syyu; echo "---------"; echo '[Press <Enter> to Close Terminal]'; read line" &

Unless your mirror is broken you do not need to pass y twice to pacman.

Offline

#13 2019-10-04 22:42:10

peterklarc
Member
Registered: 2017-03-28
Posts: 28

Re: Open Terminal with pre-written command in it [SOLVED]

loqs I have another one with pacman -Syu

Offline

#14 2019-10-05 11:47:18

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

Trilby wrote:
ProgrammAbel wrote:

Using the -e and -H flags can achieve this

No, it will not.  The OP wants the command to *not* execute until enter is pressed.  -H just keeps the terminal open after the command has run to completion.

Just add a read command or similar:

xfce4-terminal -e /bin/bash -c "read -p \"Press Enter to run 'sudo pacman -Syu'\" && sudo pacman -Syu"

Thanks for response but it gives error below :

[erfan@erfan ~]$ xfce4-terminal -e /bin/bash -c "read -p \"Press Enter to run 'sudo pacman -Syu'\" && sudo pacman -Syu"
xfce4-terminal: Unknown option "-c"

Offline

#15 2019-10-05 11:48:33

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

progandy wrote:
erfanjoker wrote:

I dont really get what you mean, can you please explain more ?

expect can be used to automate terminal input and react to output, for example like this:

xterm  -e /usr/bin/expect -c 'spawn /usr/bin/bash ; expect "$ " ; send "pacman -Syu" ; interact ; exit'

Edit: Trilby has a probably better solution, just don't forget -H to keep the terminal open so you can read the pacman output.

Thanks for your response, this trick worked but it just works in xterm, i need a more-global command based on default desktop envirnoment choice for terminal (eg: xfce => xfce4-terminal, gnome => gnome-terminal , ...)

Offline

#16 2019-10-05 11:54:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,583
Website

Re: Open Terminal with pre-written command in it [SOLVED]

Apparently in xfce4-terminal, the -x flag does what the -e flag does in most other terminals.  This applies to Seth's suggestion as well: his works in xterm because it similarly uses the -e flag (which should be changed to -x for xfce4-terminal).

But if you want something that is terminal agnostic, use the -e flag and accept that it will work with almost every terminal except xfce4-terminal.  Otherwise, use a script which would be much cleaner and easier anyways.

Last edited by Trilby (2019-10-05 11:59:48)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#17 2019-10-05 11:57:49

progandy
Member
Registered: 2012-05-17
Posts: 5,211

Re: Open Terminal with pre-written command in it [SOLVED]

Terminals interpret the -e option differently. For some you have to give the complete command as a single parameter (e.g. termite), others take the rest of the commandline (e.g. xterm). For xfce4-terminal either put everything in a single parameter for -e or replace -e with -x
https://jlk.fjfi.cvut.cz/arch/manpages/ … ab_Options

termite  -e '/usr/bin/expect -c "spawn /usr/bin/bash ; expect \"$ \" ; send \"pacman -Syu\" ; interact ; exit"'

PS: This command has another issue if you want to be shell as well as terminal agnostic: It depends on the shell prompt containing "$ ". Therefore, writing an executable shell script and simply setting its path with the "-e" parameter is the portable solution.

Last edited by progandy (2019-10-05 12:03:12)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#18 2019-10-05 12:31:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,583
Website

Re: Open Terminal with pre-written command in it [SOLVED]

Here's a shell and terminal agnostic script for running any command in this manner - just pass the full command you want to run as arguments to this script:

#!/bin/sh

[ $# -lt 1 ] && exit 1

if [ $1 == 'TERM' ]; then
	shift
	read -p "$(printf 'Ready to run: %s\n-- Press Enter To Continue --' "$*")"
	"$@"
	ret=$?
	read -p "$(printf 'Completed: %s\n-- Press Enter To Exit --' "$*")"
	exit $ret
fi

case $TERM in
	xterm*|rxvt*|st*) $TERM -e $0 TERM "$@" ;;
	termite|xfce4-terminal) $TERM -x $0 TERM "$@" ;;
	*) xterm -e $0 TERM "$@" ;;
esac

You may want to expand the list of terminal emulators in the case statement.  If you call this script "run_in_term" you can just execute (or bind to a key) commands like `run_in_term pacman -Syu`.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#19 2019-10-05 14:03:08

erfanjoker
Member
From: Tabriz / Iran
Registered: 2017-03-26
Posts: 174
Website

Re: Open Terminal with pre-written command in it [SOLVED]

Thank you all guys, this did the trick

xfce4-terminal -x /usr/bin/expect -c 'spawn /usr/bin/bash ; expect "$ " ; send "sudo pacman -Syu" ; interact ; exit'

Offline

Board footer

Powered by FluxBB