[英]how do I start a command in a terminal as if I'd opened the terminal and typed the command
I spend a lot of time opening terminals and typing commands in them. 我花了很多时间打开终端并输入命令。
$ gnome-terminal
(change mouse focus to new terminal)
$ reset && clear && tail -F ~/file_that_grows
every so often, I'll find myself going to that window and killing the process with control-C, and then restarting it. 每隔一段时间,我会发现自己会进入那个窗口并使用control-C终止进程,然后重新启动它。 In fact I'll usually alias the reset && .... bit to something more typable, like rctf. 事实上,我通常将重置&& ....位别名为rctf。
How can I automate the first bit of the process? 如何自动完成流程的第一部分? The best I can do so far is 到目前为止我能做的最好的是
gnome-terminal --title rctf -e 'tail -F ~/.bashrc'
But there are various problems with this. 但是这有各种各样的问题。 Firstly the ~ doesn't get expanded, so the file isn't found. 首先〜没有扩展,所以找不到文件。 Secondly, when the command is over, the terminal exits. 其次,当命令结束时,终端退出。
Is there any way to automatically start a terminal exactly as if I'd opened it and then typed something? 有没有办法自动启动一个终端,就像我打开它然后输入一些东西一样? (Extra points if you can get the command in the history so that it can be restarted with up-arrow ). (如果您可以在历史记录中获取命令以便可以使用向上箭头重新启动,则可以获得额外分数)。
For interactive programs, I use: 对于交互式程序,我使用:
xterm -e 'sh -c "vi $HOME/foo.txt; sh"'
The sh -c
means that variables in my command string are substituted and the sh
at the end means that a shell is started after I finish with vi
instead of closing the terminal window. sh -c
表示我的命令字符串中的变量被替换,而末尾的sh
表示在我用vi
结束而不是关闭终端窗口之后启动shell。
For a non-interactive program like tail
that can only be stopped by pressing CTRL-C to send SIGINT to the process, I use the shell trap
built-in to be sure that a shell is run if the user presses CTRL-C instead of closing the terminal window: 对于像tail
这样只能通过按CTRL-C将SIGINT发送到进程来停止的非交互式程序,我使用内置的shell trap
来确保如果用户按下CTRL-C而不是关闭终端窗口:
xterm -e sh -c 'trap sh SIGINT; tail -f $HOME/foo.txt'
If you don't mind storing your command in a file, you can abuse the --rcfile
option in bash
. 如果您不介意将命令存储在文件中,则可以滥用bash
的--rcfile
选项。 For example, using the following script to store your command(s): 例如,使用以下脚本存储命令:
[me@home]$ cat $HOME/.term-rcfile
. ~/.bashrc # chain in the standard rc file
tail -F ~/.bashrc # command to run
You can then do: 然后你可以这样做:
xterm -e "bash --rcfile $HOME/.term-rcfile -i"
Should also work for gnome-terminal: 也应该适用于gnome-terminal:
gnome-terminal -e "bash --rcfile $HOME/.term-rcfile -i"
"(Extra points if you can get the command in the history so that it can be restarted with up-arrow )." “(如果您可以在历史记录中获取命令以便可以使用向上箭头重新启动,则可以获得额外分数。)
I can't see how to do that automatically, but if your run: 我无法看到如何自动执行此操作,但如果您运行:
history -r ~/.term-rcfile
once you're in the new terminal (or whenever you want/need), entries from the file will be appended to your history (without running them) and you would then have access to them as you would normal history entries. 一旦你进入新的终端(或者你想要/需要的话),文件中的条目将被附加到你的历史记录中(不运行它们),然后你可以像普通的历史条目一样访问它们。
使用$ HOME变量而不是波形符。
gnome-terminal --title rctf -e "tail -F $HOME/.bashrc"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.