尊敬的同事,
我写了一个小代码,我将一些命令输出存储到两个不同的变量中,并希望将这两个值打印到名为“PreferredList IP”的不同列中。 “DefaultServerList IP”。
有变量PLIST& DLIST,所以,当我运行脚本时,我只看到第一列下的输出,无法获取第二列下的数据。 这对我来说很奇怪,我不知道我在哪里做错了..请纠正我..
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
结果:在使用调试模式时,我看到问题是varibale输出进入第一列。
#!/bin/sh
set -f # to prevent filename expansion
printf "=%.0s" $(seq 1 50)
printf "\n"
printf "%-30s : %10s\n" "PreferredList IP's" "DefaultServerList IP's" # print header
printf "=%.0s" $(seq 1 50) # print separator
printf "\n" # print newline
PLIST="$(ldapsearch -h mylap -x -b "ou=profile,o=cadence.com" "cn=*" preferredserverlist -LLL | awk '/preferredserverlist: / {print $2}')"
DLIST="$(ldapsearch -h myldap -x -b "ou=profile,o=cadence.com" "cn=*" defaultserverlist -LLL | awk '/defaultserverlist: / { print $2 }')"
printf "%-30s : %10s\n" "$PLIST" "$DLIST"
以下是ldapsearch命令输出示例:
dn:cn = india,ou = profile,o = cadence.com preferredServerList:123.18.8.15 123.18.8.16 defaultServerList:123.18.8.16 123.18.8.15
dn:cn = japan,ou = profile,o = cadence.com preferredServerList:192.10.167.9 192.10.167.8 defaultServerList:123.18.8.16 10.218.88.38
======================================================
PreferredList IP's : DefaultServerList IP's
========================================================
123.18.8.15
123.18.8.16
192.10.167.9
192.10.167.8
123.18.8.16
10.218.88.38
答案 0 :(得分:1)
看起来您遇到的真正问题是格式化列。
您有两个以PLIST
和DLIST
存储的IP列表作为字符串,以换行符分隔。当您键入
printf "%-30s : %10s\n" "$PLIST" "$DLIST"
它不会自动将这些格式化为列。
您确实需要更改解析LDAP结果的方式。 /bin/sh
实际上并不适合这种输出格式。
如果您可以选择使用bash(版本> 4),请使用mapfile并重构您的程序,如下所示:
#!/bin/bash
set -f # to prevent filename expansion
# Store the output of the ldapsearches in arrays using mapfile.
mapfile -t PLIST < <(ldapsearch -h mylap -x -b "ou=profile,o=cadence.com" "cn=*" preferredserverlist -LLL | awk '/preferredserverlist: / {print $2}')
mapfile -t DLIST < <(ldapsearch -h myldap -x -b "ou=profile,o=cadence.com" "cn=*" defaultserverlist -LLL | awk '/defaultserverlist: / { print $2 }')
# Count the number of elements in each array.
count_x=${#PLIST[@]}
count_y=${#DLIST[@]}
# Print the count for debugging.
echo $count_x
echo $count_y
# Find out which of the two arrays is larger in size, assuming that's a possibility, pick the length of the bigger one.
if [[ $count_x -lt $count_y ]]
then
count=$count_y
else
count=${count_x}
fi
printf "=%.0s" $(seq 1 50)
printf "\n"
printf "%-30s : %10s\n" "PreferredList IP's" "DefaultServerList IP's" # print header
printf "=%.0s" $(seq 1 50) # print separator
printf "\n" # print newline
# Use an index 0 < i <= count, to loop over the arrays simultaneously.
for i in $(seq $count);
do
printf "%-30s : %10s\n" "${PLIST[i-1]}" "${DLIST[i-1]}"
done
这使用bash的mapfile将ldap搜索命令的输出存储在索引数组中,并将其打印在格式化列中。
作为测试,我写了这个并用模拟seq
调用替换你的ldap命令来生成数字。 Here's a sample run