AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 5363
Accepted
emf
emf
Asked: 2010-10-09 09:39:17 +0800 CST2010-10-09 09:39:17 +0800 CST 2010-10-09 09:39:17 +0800 CST

如何使用已在命令行上输入的某些文本启动终端?

  • 772

让我向您描述所需的用户案例,而不是重新表述我的问题:

我创建了一个简短的 shell 脚本来运行命令“gnome-terminal --someoptionflagname '我要发布的文本'”,然后执行这个脚本。

Gnome-terminal 弹出,命令行提示后跟我的文本。

IE:fields@mycomputer:/$ my text to be posted

这可以做到吗?

scripts command-line gnome-terminal bash
  • 7 7 个回答
  • 37035 Views

7 个回答

  • Voted
  1. Best Answer
    ændrük
    2010-10-09T15:06:43+08:002010-10-09T15:06:43+08:00

    您可以使用expect ( install ) 来做到这一点。创建并生成可执行文件~/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
    
    • 33
  2. Gordon McCreight
    2010-12-10T22:27:16+08:002010-12-10T22:27:16+08:00

    ændrük 的建议非常好并且对我有用,但是该命令在脚本中是硬编码的,如果您调整终端窗口的大小,它就不能正常工作。使用他的代码作为基础,我添加了将命令作为参数发送到 myprompt 脚本的功能,并且该脚本可以正确处理调整终端窗口的大小。

    #!/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\""
    
    • 9
  3. Gilles 'SO- stop being evil'
    2010-10-09T16:02:09+08:002010-10-09T16:02:09+08:00

    如果我理解正确,您希望将您的第一行输入预填充为您在 gnome-terminal 命令行上传递的内容。

    我不知道如何用 bash 准确地做到这一点,但这里有一些接近的东西。在您的~/.bashrc中,在最后添加以下行:

    history -s "$BASH_INITIAL_COMMAND"
    

    运行gnome-terminal -x env BASH_INITIAL_COMMAND='my text to be posted' bash,并在提示时按下Up以调用文本。

    另请注意,如果您set -o history在..bashrcUp#

    • 8
  4. msw
    2010-10-10T06:13:19+08:002010-10-10T06:13:19+08:00

    æ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.shthen 的文件中chmod,则使其可执行,然后执行。

    $ chmod +x rt.sh && ./rt.sh echo hello world
    

    所有这一切的结果将是一个启动的 gnome 终端,显示

    hello world
    

    在它的第一行,然后启动一个交互式外壳:

    msw@myhost:~$
    
    • 1
  5. Brian Vandenberg
    2015-01-24T10:37:08+08:002015-01-24T10:37:08+08:00

    我最喜欢解决这个问题的两个答案是 usingexpect和这个他们建议--init-file在 shebang 或执行终端时使用标志的地方:

    #!/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's时遇到的问题--command是为了解决这个问题,shell 在执行初始化脚本时无法访问 STDIN。例如,如果我想运行一些需要用户输入(ftp、telnet 等)但之后让父 shell 运行的东西,它将无法工作;到目前为止,我看到的唯一例外是 ssh:

    xterm -e /bin/bash --init-file <(echo 'ssh -X some-machine')
    

    ...但我需要的比这个玩具示例更复杂。无论如何,我希望这些--init-file东西对阅读这个问题的人有用。

    • 0
  6. mikebridge
    2020-09-19T10:50:07+08:002020-09-19T10:50:07+08:00

    要在 gnome-terminal 中的交互式 bash 中开始,并且已经执行了一些命令,我​​将这些命令放在一个安装文件中,并将内容动态附加到我的.bashrc文件中:

    # my_startup_script.sh
    export FOO=bar
    echo "hello world"
    
    gnome-terminal --tab -- bash -c "bash --rcfile <(cat ${HOME}/.bashrc my_startup_script.sh; )"
    => 
    hello world
    $ echo $FOO
    bar
    
    • 0
  7. Karl Bielefeldt
    2010-10-09T09:46:07+08:002010-10-09T09:46:07+08:00

    您可以使用-eor-x参数在新弹出的终端中运行命令,但这并不是您想要的。

    • -1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve