需要 korn shell 中数组的帮助。我需要将值从第一个 for 循环传递到第二个循环。
第一个循环返回 /opt/IBM/db2/V11.5 /opt/IBM/db2/V11.5.8 /opt/IBM/db2/V11.5.8
然后我很欣赏“uniq”价值观。所以我想传递以下 uniq 值 /opt/IBM/db2/V11.5 /opt/IBM/db2/V11.5.8
但是,我的脚本只传递最后一个值。任何帮助都会很棒。
db2instance=`ps -eaf | grep db2sysc |grep -v grep | awk '{print $1}' | sort`
for instance in ${db2instance}; do
set -A db2dirpath
db2dirpath=$(su - $instance -c db2level| grep "Product is installed at" | awk '{print $5}' | sed 's/"//g' | sed 's/.$//')
do
DIRDB2=$(echo ${db2dirpath[@]} | uniq)
for db2dir in "${DIRDB2[@]}"
do
su - $instance -c "/db/data1/Ansible/db2dirpath.sh $instance $db2dir"
done
答案1
我能够解决这个问题。下面是正在运行的代码。
db2instance=`ps -eaf | grep db2sysc |grep -v grep | awk '{print $1}' | sort`
DIRDB2=$(
for instance in ${db2instance}; do
db2dirpath=$(su - $instance -c db2level| grep "Product is installed at" | awk '{print $5}' | sed 's/"//g' | sed 's/.$//')
echo "$db2dirpath"
done
)
DIRPATH=$(echo "$DIRDB2" | sort -u | uniq)
for db2dir in $DIRPATH
do
su - $instance -c "/db/data1/Ansible/db2dirpath.sh $db2dir"
done
答案2
由于OP似乎不愿意回答澄清问题,这只是一个猜测。
# get a list of usernames that are running db2sysc. No need to sort them
# Assumption is the user names don't have unusual characters in them.
db2instance=$(ps -eaf | awk '/db2sys[c]/ {print $1}')
# set up an associative array to map dirpath to user name.
# probably should be called dpbath2user
typeset -A db2dirpath
# Loop over the usernames. Use associate array so we only record each
# db path once in the array.
for instance in ${db2instance}; do
db2dirpath[$(su - $instance -c db2level| awk '/Product is installed a[t]/" {print $5}' | sed 's/"//g;s/.$//')]=$instance
done
# Loop over the paths
for db2dir in "${!db2dirpath[@]}"
do
# process the path with the correct user name
su - ${db2dirpath[db2dir]} -c "/db/data1/Ansible/db2dirpath.sh $instance $db2dir"
done