You are not logged in.
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
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
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
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
Offline
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
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
I need user to hit enter to run the command, this will open the terminal running that command
Offline
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
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
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
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
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
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
loqs I have another one with pacman -Syu
Offline
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
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
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
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
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
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