如何在Linux中从终端运行一个新的终端窗口中的shell脚本,类似于Windows中的“start test.bat”,同时它应该能够在控制台模式下工作。
以下是一个简单的例子,帮助你入门:
要编写一个Shell脚本,请在命令提示符上执行以下操作:
echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
这将输出:
#!/bin/sh
echo "hello world"
创建一个名为abc.sh
的文件。
接下来,你需要将其设置为可执行文件:
chmod +x abc.sh
现在,你可以通过以下方式运行它:
./abc.sh
你应该看到:
hello world
在你的终端中执行。
要在新的终端中运行它,可以执行以下命令:
gnome-terminal -x ./abc.sh
或者,如果是 xterm
:
xterm -e ./abc.sh
这里有一份不同终端仿真器的列表。
或者你可以在当前的终端中运行它,但是可以通过以下方式将其放在后台:
./abc.sh &
gnome-terminal -x ./abc.sh
将无法在你的Windows环境中生成一个新的终端,据我所知,这是在Windows端进行脚本编写,而不再是在Unix端。 - sampson-chen我来这里是想弄清楚如何让脚本生成一个终端并在其中运行自己,所以对于那些想要做到这一点的人,我找到了以下解决方案:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
x-terminal-emulator -e "$0" "$@"
# and abort running the rest of it
exit 0
fi
gnome-terminal
中的 -e
和 -x
选项仍然可以正常运行,但会抛出以下警告:-e
:
对于# 选项“-e”已过时,可能在以后的 gnome-terminal 版本中删除。
# 使用“-- ”终止选项,并在其后放置要执行的命令行。
-x
:
根据上述信息,我确认您可以运行以下两个命令而不会收到任何警告消息:# 选项“-x”已过时,可能在以后的 gnome-terminal 版本中删除。
# 使用“-- ”终止选项,并在其后放置要执行的命令行。
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
gnome-terminal -x sh -c "ls|less"
我希望这是你想要的