打开多个选项卡并在 shell 脚本中执行命令
Open multiple tabs and execute command in shell script
#!/bin/bash
tab="--tab"
cmd="bash -c 'python';bash"
foo=""
for i in 1 2 3; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
我正在使用此脚本通过 shell 脚本打开多个选项卡。
调用它multitab.sh
并这样执行user@user:~$ sh multitab.sh
目前这个脚本应该打开 3 个选项卡,并且所有选项卡都将执行 python 命令。
但是当我执行它时,抛出错误
multitab.sh: 8: multitab.sh: Syntax error: word unexpected (expecting ")")
这个错误的原因是什么?如何让这个脚本执行 3 个不同的命令?
我已经经历过了。在 SOF 线程之下,但其中 none 对我有用。
这是因为您 运行 使用 sh
连接脚本,其中 +=
添加元素的语法不可用:
foo+=($tab -e "$cmd")
# ^^
所以您需要做的就是 运行 带有 Bash 的脚本:
bash multitab.sh
或者只使用 ./multitab.sh
(在为文件提供执行模式之后),因为脚本中的 shebang (#!/bin/bash
) 已经提到了 Bash.
来自 Bash 参考手册:
Appendix B Major Differences From The Bourne Shell
- Bash supports the ‘+=’ assignment operator, which appends to the value of the variable named on the left hand side.
#!/bin/bash
tab="--tab"
cmd="bash -c 'python';bash"
foo=""
for i in 1 2 3; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
我正在使用此脚本通过 shell 脚本打开多个选项卡。
调用它multitab.sh
并这样执行user@user:~$ sh multitab.sh
目前这个脚本应该打开 3 个选项卡,并且所有选项卡都将执行 python 命令。 但是当我执行它时,抛出错误
multitab.sh: 8: multitab.sh: Syntax error: word unexpected (expecting ")")
这个错误的原因是什么?如何让这个脚本执行 3 个不同的命令?
我已经经历过了。在 SOF 线程之下,但其中 none 对我有用。
这是因为您 运行 使用 sh
连接脚本,其中 +=
添加元素的语法不可用:
foo+=($tab -e "$cmd")
# ^^
所以您需要做的就是 运行 带有 Bash 的脚本:
bash multitab.sh
或者只使用 ./multitab.sh
(在为文件提供执行模式之后),因为脚本中的 shebang (#!/bin/bash
) 已经提到了 Bash.
来自 Bash 参考手册:
Appendix B Major Differences From The Bourne Shell
- Bash supports the ‘+=’ assignment operator, which appends to the value of the variable named on the left hand side.