ubuntu - 如何从 cli 命令打开一个新的终端窗口并打印 "Hello World"

标签 ubuntu gnome-terminal

我一直在使用 gnome-terminal 命令进行一些小的自动化操作,但我一直遇到同样的问题。目标是在与当前窗口相同的目录中打开一个新窗口,并执行一个简单的命令(即 echo、cd 或类似命令)。此命令似乎可以在同一目录中打开新窗口:

DIR=$(pwd); gnome-terminal --working-directory=$DIR

但是,当我向它添加执行时,它会失败:
DIR=$(pwd); gnome-terminal --working-directory=$DIR -e 'echo "test"'

我收到一条消息说 -e已弃用,我应该使用 --所以我换成了
DIR=$(pwd); gnome-terminal --working-directory=$DIR -- 'echo "test"'

但是我现在收到此错误:
Failed to execute child process “echo "test"” (No such file or directory)
有谁知道为什么会这样?

确切的系统规范:
Ubuntu 18.04,使用 VTE 0.56.2 +GNUTLS 的 GNOME 终端 3.32.1

确切的弃用消息是:
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.

最佳答案

啊。我懂了。这两个消息实际上是不同的。

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

我认为这意味着您应该使用 -x , 而是 -- -x 之后的命令行上的所有内容被认为是要执行的命令的一部分。
# Use “-- ” to terminate the options and put the command line to execute after it.

这是命令行程序中使用的约定。两个破折号表示之后出现的任何内容,无论是否有破折号,都不是命令行选项。它与 -x 结合使用很有用, 因为你可能有一些你不想被解析为 gnome-terminal 的选项之一的东西——而不是它们是要在终端内运行的命令行参数。

我想一个例子可能会让这个更清楚......
gnome-terminal -x -- grep -v "42" hitchikers_guide_to_the_galaxy.txt

这将打开一个 gnome 终端。在终端内,我们正在运行 grep,并排除任何包含字符串“42”的行。如果我们没有使用 -- , gnome-terminal 会看到 -v作为 gnome-terminal 的命令行参数,并且可能会抛出一个警告说 -v不是 gnome-terminal 的有效参数。

编辑:

好的,所以两个 -x-c已弃用。您可以执行不带任何标志的命令。它确实需要使用 --终止所有其他选项......所以我对我所说的一切都非常错误。理由很充分,只是错了。
gnome-terminal -- nano

将打开一个新终端,并在终端内启动 nano。

当你尝试时事情会变得棘手
gnome-terminal -- echo foo

它会运行,但退出速度非常快,以至于您在屏幕上只看到一个闪光。

请注意,还有一个关于弃用 -c 的开放错误。 :

https://bugzilla.gnome.org/show_bug.cgi?id=784925

关于ubuntu - 如何从 cli 命令打开一个新的终端窗口并打印 "Hello World",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399107/

相关文章:

c - udp socket编程文件传输

macos - nodejs 密码 ubuntu 没有按预期工作

python - Ubuntu 18.04 上 Python 的 os.system 和 subprocess.check_output 中莫名其妙的 shell 命令取消转义行为

linux - 通过 crontab 终止后运行 python 程序

bash - 迷你项目 : Change the color of the terminal based on the time of day

linux - 在 grep 搜索后添加 echo

Laravel Homestead 和 vagrant 不会在 vagrant box 中创建文件夹

linux - 如何在 Linux 终端中更新打印的行?

c++ - 在 C++ 中使用 QProcess 重定向 gnome 终端的输出

linux - Shell 脚本语句不能一一运行