I have a function:
function checkConn()
{
RET=0
echo "in function: ${2}"
echo
for i in `cat ${1}`
do
for p in ${2}
do
if nc -dzw 2 ${i} ${p} 2>&1 >/dev/null
and so on.
In the "main" body of the script, I have the following:
PORTS='22 161 162 1521'
checkConn ${FILE} ${PORTS}
FILE is the name of a file which contains a list of IPs.
When I pass PORTS to the function, only the 1st item gets passed. I also tried it with double-quotes.
I put that "echo" statement to confirm. It only shows the first item in PORTS, which is 22.
How can I pass all the ports to this function, and then loop through each one?
ports=( 22 161 162 1521 )is the correct way to define an array namedports.checkConn "$file" "${ports[@]}"will pass the filename as the first argument and the individual ports as subsequent arguments; inside that function, one can do something likecheckConn() { file=$1; shift; for port; do echo "Checking port "$port"; done; }-- iterating over the argument vector.