Looking to minimize 'for' loops in script

Hi, Below is the script that I came up with but looking to see if there is a more appropriate way to achieve this by reducing number of "for" loops or something.

Regards,
mbak

#!/usr/bin/ksh
status=missing
for disk in `lspv | awk '{print $1}'`
do
MISSPATH=`lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
	for path in ${MISSPATH}
	do
		rmpath -d -l ${disk} -i ${pathid}
	done
done

status=failed
for disk in `lspv | awk '{print $1}'`
do
FAILPATH=`lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
	for path in ${FAILPATH}
	do
		rmpath -d -l ${disk} -i ${pathid}
	done
done

Like this?

#!/usr/bin/ksh
for status in missing failed
do
  for disk in `lspv | awk '{print $1}'`
  do
    for path in `lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
    do
      rmpath -d -l ${disk} -i ${pathid}
    done # end path
  done   # end disk
done     # end status
1 Like

Noticed that variable names do not match: for path vs. ${pathid} .

1 Like

In the previous suggestion the two outer loops can be swapped, so lspv is run once.
The following suggestion does not run lspv at all:

for status in missing failed
do
  lspath -s $status -F "name parent path_id connection" |
  while read dname dparent dpath_id dconnection
  do
    echo rmpath -d -l "$dname" -i "$dpath_id"
  done
done

Remove the echo when you think it works.

1 Like