如何启动已在命令行输入某些文本的终端?
让我向您描述所需的用户案例,而不是重述我的问题:
我创建一个简短的shell脚本来运行命令“gnome-terminal –someoptionflagname’我的文本要发布’”,然后执行这个脚本。
弹出Gnome-terminal,命令行提示符后跟我的文本。
ie: fields@mycomputer:/$ my text to be posted
可以这样做吗?
您可以使用expect ( 安装 )执行此操作。 创建并生成可执行文件~/bin/myprompt
:
#!/usr/bin/expect -f # Get a Bash shell spawn -noecho bash # Wait for a prompt expect "$ " # Type something send "my text to be posted" # Hand over control to the user interact exit
并运行Gnome终端:
gnome-terminal -e ~/bin/myprompt
如果我理解正确,您希望将第一个输入行预先填充到您在gnome-terminal命令行上传递的内容。
我不知道如何用bash做到这一点,但这里有一些接近的东西。 在~/.bashrc
,在最后添加以下行:
history -s "$BASH_INITIAL_COMMAND"
运行gnome-terminal -x env BASH_INITIAL_COMMAND='my text to be posted' bash
,然后在提示符处按Up调用文本。
另请注意,如果在.bashrc
末尾放置set -o history
后跟注释,它们将在bash启动时输入到历史记录中,因此您可以使用它们作为编辑的基础,通过使用Up键和删除最初的#
。
ændrük的建议非常好并且对我有用,但是命令在脚本中是硬编码的,如果你调整终端窗口的大小,它就不能正常工作。 使用他的代码作为基础,我添加了将命令作为参数发送myprompt脚本的function,并且此脚本正确处理调整终端窗口的大小。
#!/usr/bin/expect #trap sigwinch and pass it to the child we spawned #this allows the gnome-terminal window to be resized trap { set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) } WINCH set arg1 [lindex $argv 0] # Get a Bash shell spawn -noecho bash # Wait for a prompt expect "$ " # Type something send $arg1 # Hand over control to the user interact exit
并运行Gnome终端:
gnome-terminal -e "~/bin/myprompt \"my text to be posted\""
ændrük的答案很好,但也许有点重量级的任务。
这是一个基于其参数编写脚本的脚本
#!/bin/sh # terminal-plus-command: start a subordinate terminal which runs # the interactive shell after first running the command arguments tmpscript=/tmp/tmpscript.$$ echo "#!$SHELL" > $tmpscript echo "$@" >> $tmpscript echo exec "$SHELL" >> $tmpscript chmod +x $tmpscript gnome-terminal --command $tmpscript rm -f $tmpscript
如果你没有做太多的shell编程,那么这里似乎有更多的魔力。 首先,我命名一个用于保存脚本的临时文件,其中$$
是运行此脚本的shell的进程ID。 /tmp/something.$$
隐喻用于在同时运行此脚本的两个实例的情况下,它们不会尝试使用相同的临时文件。
变量$SHELL
设置为运行脚本的shell的名称。 如果您使用/ usr / bin / bash,大概您也希望迷你脚本也能使用它。
"$@"
是一个shell成语,用于“插入我的所有参数,如果需要可以引用它们”。 这种特殊的语法导致
script.sh 'my file' your\ file
将参数插入为两个元素
"my file" "your file"
而不是$@
会产生的四个
"my" "file" "your" "file"
脚本的最后几行安排gnome-terminal开始运行迷你脚本,然后启动交互式shell。 当gnome-terminal退出时,临时脚本被删除,因为乱扔垃圾是不冷静的。
最后一行不是迷你脚本的一部分,它表明迷你脚本有效。 如果上面的11行脚本位于名为rt.sh
的文件中,则chmod
使其可执行,然后执行。
$ chmod +x rt.sh && ./rt.sh echo hello world
所有这一切的结果将是一个启动,显示的gnome终端
hello world
在它的第一行,然后启动一个交互式shell:
msw@myhost:~$
我最喜欢解决这个问题的两个答案是使用expect
和他们建议在shebang中使用--init-file
标志或执行终端时:
#!/bin/bash --init-file commands to run
…并执行它:
xterm -e /path/to/script # or gnome-terminal -e /path/to/script # or the-terminal -e bash --init-file /path/to/script/with/no/shebang
expect
可能是更好的解决方案(由于我将概述的原因),除了我无法控制它是否安装在我的目标环境中。
我遇到的问题是使用bash的--init-file
和gnome-terminal
的--command
作为解决这个问题的hack,因为shell在执行init脚本时无法访问STDIN。 例如,如果我想运行需要用户输入的东西(ftp,telnet等)但是之后让父shell运行它将无法工作; 到目前为止,我见过的唯一例外是ssh:
xterm -e /bin/bash --init-file <(echo 'ssh -X some-machine')
......但我需要的东西比这个玩具例子更复杂。 无论如何,我希望--init-file
东西对于阅读这个问题的人都有用。
您可以使用-e
或-x
参数在新弹出的终端内运行命令,但这并不是您想要的。