如何在Windows的新终端中从Windows中的“start test.bat”等终端运行shell脚本,它也应该在控制台模式下运行。
答案 0 :(得分:21)
这是一个让你入门的简单例子:
要编写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
这是list of different terminal emulators。
或者,您只需在当前终端中运行它,而不是通过以下方式运行它:
./abc.sh &
答案 1 :(得分:4)
对于gnome试试这个。
将ls替换为您要运行的命令
gnome-terminal -x sh -c "ls|less"
我希望这就是你想要的
答案 2 :(得分:3)
我来到这里是想弄清楚如何让脚本产生一个终端并在其中自行运行,所以对于那些想要这样做的人我想出了这个解决方案:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window
x-terminal-emulator -e "$0"
# and abort running the rest of it
exit 0
fi
答案 3 :(得分:2)
截至2020年1月,-e
中的-x
和gnome-terminal
选项仍然可以正常运行,但会发出以下警告:
对于-e
:
#不建议使用选项“ -e”,在更高版本中可能会删除它 gnome-terminal。
#使用“-”终止选项并将命令行放入 之后执行。
对于-x
:
#不建议使用选项-x,在更高版本中可能会删除它 gnome-terminal。
#使用“-”终止选项并将命令行放入 之后执行。
根据上述信息,我确认您可以运行以下两个命令而不会收到任何警告消息:
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
我希望这对目前遇到此问题的其他人有所帮助:)