
我正在運行 Ubuntu 21.04,我想建立一個腳本,該腳本將執行以下操作:
跑步:
cd && cd path/to/repo && git pull && npm i && code . && npm run dev
然後使用以下命令開啟新的 GNOME 終端選項卡:
gnome-terminal --tab
然後切換到此選項卡並運行:
cd && cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev
所以基本上我想要一個有 2 個選項卡的終端,運行 2 個開發伺服器。
我嘗試使用xdotool
和xte
,但它從未起作用(甚至沒有按下按鍵)。
甚至可以做我想做的事嗎?
正如下面的建議,我嘗試過,它給了我,failed to run cd
所以我用谷歌搜尋並得出以下結果:
gnome-terminal --tab -- /bin/bash -e -c "cd path/to/repo && ls && git pull && npm i && code . && npm run dev" --tab -- /bin/bash -e -c "cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev"
但問題是它npm run dev
不應該停止,因此第二個終端選項卡永遠不會打開。我需要將它們一起運行......
答案1
儘管gnome-terminal
表明該-e
選項已棄用,但它在 3.40.3 中仍然有效。
# 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 --tab -e "htop" --tab -e "top"
將命令htop
和替換top
為您的自訂腳本。
答案2
所以sh腳本的內容是:
gnome-terminal --tab --title='frontend' -e "bash -c 'cd path/to/repo && git pull && npm i && code . && npm run dev'";
gnome-terminal --tab --title='backend' -e "bash -c 'cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev'"
感謝@vanadium 幫助找到谷歌搜尋的方向;)
它確實按預期工作。如果我打開一個終端並寫入,sh myScript.sh
它將打開 2 個額外的選項卡,並運行後端和前端儲存庫。
一個小問題是,如果我透過雙擊 sh 檔案來執行它,它將打開 2 個終端視窗(醜陋,我想要選項卡)。
真的不知道該如何解決這個問題。