[英]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
i'm using this scirpt to open multiple tabs using shell script. 我正在使用此scirpt使用shell脚本打开多个选项卡。
call it multitab.sh
and execute this way user@user:~$ sh multitab.sh
称之为multitab.sh
并以这种方式执行user@user:~$ sh multitab.sh
currently this script supposed to open 3 tabs and all of them will execute python command. 目前这个脚本应该打开3个标签,所有这些标签都会执行python命令。 but when i execute it, throws en error 但是当我执行它时,抛出错误
multitab.sh: 8: multitab.sh: Syntax error: word unexpected (expecting ")")
What is the reason of this error? 这个错误的原因是什么? How can I make this script to execute 3 different commands? 如何让这个脚本执行3个不同的命令?
I've already gone through. 我已经经历过了。 below SOF threads but none of them worked for me. 低于SOF线程,但它们都不适合我。
This is because you are running the script with sh
, where the +=
syntax to add elements is not available: 这是因为您使用sh
运行脚本,其中添加元素的+=
语法不可用:
foo+=($tab -e "$cmd")
# ^^
So all you need to do is to run the script with Bash: 所以你需要做的就是用Bash运行脚本:
bash multitab.sh
Or just using ./multitab.sh
(after giving executing mode to the file), since the shebang in the script ( #!/bin/bash
) already mentions Bash. 或者只是使用./multitab.sh
(在给文件执行模式之后),因为脚本中的shebang( #!/bin/bash
)已经提到了Bash。
From the Bash Reference Manual: 从Bash参考手册:
Appendix B Major Differences From The Bourne Shell 附录B与Bourne Shell的主要区别
- Bash supports the '+=' assignment operator, which appends to the value of the variable named on the left hand side. - Bash支持'+ ='赋值运算符,它附加到左侧命名的变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.