从当前终端在新终端中运行shell脚本

mre*_*nia 23 linux shell terminal

如何在Windows的新终端中从Windows中的"start test.bat"等终端运行shell脚本,它也应该在控制台模式下工作.

sam*_*hen 23

这是一个让您入门的简单示例:

要编写shell脚本,请在命令提示符下执行此操作:

echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
Run Code Online (Sandbox Code Playgroud)

这写道:

#!/bin/sh
echo "hello world"
Run Code Online (Sandbox Code Playgroud)

到一个名为的文件 abc.sh

接下来,您要将其设置为可执行文件:

chmod +x abc.sh
Run Code Online (Sandbox Code Playgroud)

现在,您可以通过以下方式运行它:

./abc.sh
Run Code Online (Sandbox Code Playgroud)

你应该看到:

hello world
Run Code Online (Sandbox Code Playgroud)

在你的终端上.

要在新终端中运行它,您可以执行以下操作:

gnome-terminal -x ./abc.sh
Run Code Online (Sandbox Code Playgroud)

或者,如果是xterm:

xterm -e ./abc.sh
Run Code Online (Sandbox Code Playgroud)

这是一个不同的终端模拟器列表.

或者,您只需在当前终端中运行它,但请将其替换为:

./abc.sh &
Run Code Online (Sandbox Code Playgroud)

  • 嗨桑普森.我在另一个窗口中执行的shell脚本需要挂起.所以我原来的sh文件需要继续下一行命令.我怎样才能做到这一点?谢谢! (2认同)

Joa*_*sen 5

我来到这里是想弄清楚如何使脚本产生一个终端并在其中自我运行,因此对于那些想要这样做的人,我想出了以下解决方案:

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
Run Code Online (Sandbox Code Playgroud)