使用 shell 脚本在终端中顺序执行多个命令

问题描述 投票:0回答:1

我想做以下事情:

  1. 打开一个终端。
  2. 切换到目标脚本所在的目录。
  3. 打开为目标脚本提供输入的文本文件。保留/等待,直到用户保存并关闭文本文件。
  4. 执行目标脚本。

我写了下面的脚本来做上面的事情:

#!/bin/bash 
echo "hello"
gnome-terminal -x bash -c "cd ~/automation/DVF99_Automation/Scripts;pwd;gedit sample.txt;python test.py;exec $SHELL"
echo "good bye"

上面给了我以下输出:

user4@user-pc-4:~/Desktop$ ./DAT_run.sh
hello
good bye

在打开的新 gnome 终端上,我看到以下消息:

/home/user4/Scripts
From python script
From python script
From python script
From python script
From python script

以上表示它已经执行了 python 代码,并且满足了我的要求 1,2 和 4(不是第 3 个)。在保存并关闭

gedit
窗口之前,我无法将其作为前台进程保留(以便仅在我关闭
gedit
中打开的文件后才执行下一条语句)。

这里可能出了什么问题?我是 shell 脚本的新手,觉得我很可能在这里遗漏了一些东西。我怎样才能达到以上所有要求?

linux bash shell scripting gnome-terminal
1个回答
0
投票

顺便说一句,你需要在后台启动 gedit。

例子:

$ gedit sample.txt&;python test.py;exec $SHELL"

更好的是可变替换。

 #!/she/bang
 FILE=sample.txt
 gedit $(FILE)&;python test.py;exec $SHELL"
© www.soinside.com 2019 - 2024. All rights reserved.