[英]Run a shell script in new terminal from current terminal
如何從 Windows 中的“start test.bat”等終端在 Linux 的新終端中運行 shell 腳本,它也應該在控制台模式下工作。
這是一個讓您入門的簡單示例:
要編寫 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 &
我來這里是想弄清楚如何讓腳本生成一個終端並在其中自行運行,所以對於那些想這樣做的人,我想出了這個解決方案:
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
對於侏儒試試這個。
將 ls 替換為您要運行的命令
gnome-terminal -x sh -c "ls|less"
我希望這就是你想要的
截至 2020 年 1 月, gnome-terminal
中的-e
和-x
選項仍然可以正常運行,但會拋出以下警告:
對於-e
:
# 選項“-e”已棄用,可能會在更高版本的 gnome-terminal 中刪除。
# 使用“--”結束選項,將命令行放在其后執行。
對於-x
:
# 選項“-x”已棄用,可能會在更高版本的 gnome-terminal 中刪除。
# 使用“--”結束選項,將命令行放在其后執行。
根據以上信息,我確認您可以運行以下兩個命令而不會收到任何警告消息:
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
我希望這可以幫助目前遇到此問題的其他人:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.