Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个shell脚本,如下所示。
#/bin/bash ip1="1.1.1.1" ip2="2.2.2.2" for ((i=1; i<=2; i++)) do echo "$[ip$i]" done
运行后我得到错误。 ./1.sh:第 8 行:1.1.1.1:语法错误:算术运算符无效(错误标记为“.1.1.1”)
太感谢了 !
使用间接变量扩展。
#/bin/bash ip1="1.1.1.1" ip2="2.2.2.2" for ((i=1; i<=2; i++)) do var="ip$i" echo "${!var}" done
但是,更好的主意是使用数组。
ips=( "1.1.1.1" "2.2.2.2" ) for ip in "${ips[@]}"; do echo "$ip" done for ((i=1; i<=${#ip[@]}; i++)); do echo "${ips[i]}" done