gpt4 book ai didi

linux - 打开多个选项卡并在 shell 脚本中执行命令

转载 作者:IT王子 更新时间:2023-10-29 00:33:21 26 4
gpt4 key购买 nike

#!/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 线程之下,但没有一个对我有用。

最佳答案

这是因为您正在使用 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.

关于linux - 打开多个选项卡并在 shell 脚本中执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41481857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com