Linux 从当前终端在新终端中运行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13648780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a shell script in new terminal from current terminal
提问by mreaevnia
How do you run a shell script in a new terminal in Linux from a terminal like "start test.bat" in Windows, also it should be working in the console mode.
您如何从 Windows 中的“start test.bat”等终端在 Linux 的新终端中运行 shell 脚本,它也应该在控制台模式下工作。
采纳答案by sampson-chen
Here's a simple example to get you started:
这是一个让您入门的简单示例:
To write a shell script, do this on your command prompt:
要编写 shell 脚本,请在命令提示符下执行以下操作:
echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
This writes:
这写道:
#!/bin/sh
echo "hello world"
To a file called abc.sh
到一个名为 abc.sh
Next, you want to set it to executable by:
接下来,您希望通过以下方式将其设置为可执行:
chmod +x abc.sh
Now, you can run it by:
现在,您可以通过以下方式运行它:
./abc.sh
And you should see:
你应该看到:
hello world
On your terminal.
在您的终端上。
To run it in a new terminal, you can do:
要在新终端中运行它,您可以执行以下操作:
gnome-terminal -x ./abc.sh
or, if it's xterm
:
或者,如果是xterm
:
xterm -e ./abc.sh
Here's a list of different terminal emulators.
Alternatively, you just run it in your current terminal, but background it instead by:
或者,您只需在当前终端中运行它,而是通过以下方式将其作为后台:
./abc.sh &
回答by knightrider
For gnome try this.
对于侏儒试试这个。
Replace ls with the command you want to run
将 ls 替换为您要运行的命令
gnome-terminal -x sh -c "ls|less"
I hope this is what you want
我希望这是你想要的
回答by Joakim L. Christiansen
I came here wanting to figure out how to make a script spawn a terminal and run it self in it, so for those who want to do that I figured out this solution:
我来到这里是想弄清楚如何让脚本生成一个终端并在其中自行运行,所以对于那些想要这样做的人,我想出了这个解决方案:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
x-terminal-emulator -e "gnome-terminal -- /bin/sh -c '<your command>'
" "$@"
# and abort running the rest of it
exit 0
fi
回答by Member2017
As of January 2020, the -e
and -x
option in gnome-terminal
still run properly but throw out the following warnings:
截至 2020 年 1 月,中的-e
and-x
选项gnome-terminal
仍然正常运行,但会抛出以下警告:
For -e
:
对于-e
:
# 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.
# 选项“-e”已弃用,可能会在更高版本的 gnome-terminal 中删除。
# 使用“--”终止选项并在其后执行命令行。
For -x
:
对于-x
:
# Option “-x” 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.
# 选项“-x”已弃用,可能会在更高版本的 gnome-terminal 中删除。
# 使用“--”终止选项并在其后执行命令行。
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
根据上述信息,我确认您可以运行以下两个命令而不会收到任何警告消息:
gnome-terminal -- ./<your script>.sh
##代码##
I hope this helps anyone else presently having this issue :)
我希望这可以帮助目前遇到此问题的其他人:)