简体   繁体   English

如何将数组作为参数传递给 shell 脚本并在 shell 脚本中获取该数组

[英]How to pass array as an argument to shell script and fetch that array in shell script

I have an array of IPs and I want to pass that array as an argument to perform further operations in shell script.我有一个 IP 数组,我想将该数组作为参数传递,以在 shell 脚本中执行进一步的操作。 I want to know how to pass the array and access in shell script.我想知道如何在 shell 脚本中传递数组和访问。

eg.例如。 ["1.2.22.22","33.2.5.2"] ["1.2.22.22","33.2.5.2"]

you can pass the IP addresses as arguments and then make an array inside the script using $@ .您可以将 IP 地址作为参数传递,然后使用$@在脚本内创建一个数组。 this is needed to list each argument as a separate word :这需要将每个参数列为一个单独的单词:

arr-script.sh: arr-script.sh:

declare -a ARR
ARR=($@)


echo ${ARR[0]}
echo ${ARR[1]}
echo ${ARR[2]}
echo ${ARR[*]}

u can run it this way: sh arr-script.sh IP1 IP2 IP3你可以这样运行它:sh arr-script.sh IP1 IP2 IP3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM