0

Esteemed colleagues,

I have written a small code where i'm storing some command output into two different variables and aspiring those two values to be printed under into different columns called "PreferredList IP's" & "DefaultServerList IP's".

there are Variables PLIST & DLIST,So, when i'm running the script i only see output under the first column and unable to get the data under second column. This looks weird to me, i don't know where i'm doing mistake..please do correct me..

#!/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"

RESULT: while using debug mode, I saw the problem is both the varibale output coming into first column.

======================================================
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

Below is the ldapsearch command output sample:

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

$ ldapsearch -h myldap -x -b "ou=profile,o=cadence.com" "cn=*" preferredserverlist -LLL | awk '/preferredserverlist: / {print $2}' | head -2
123.18.8.15 
123.18.8.16

$ ldapsearch -h myldap -x -b "ou=profile,o=cadence.com" "cn=*" defaultserverlist -LLL | awk '/defaultserverlist: / { print $2 }' | head -2
123.18.8.16 
10.218.88.38
4
  • Do the single quotes instead of double inside the command string solve the issue? Commented Feb 5, 2017 at 13:39
  • @0andriy .. thats did not make any diffrence Commented Feb 5, 2017 at 14:51
  • There might be some newlines embedded in the ldapsearch results. Try running the command and passing it to hexdump -C (or just hd). Also, your output doesn't match your code: your printf has a colon in the string and there are no colons in your output. Commented Feb 5, 2017 at 15:11
  • @Harvey .. colon is placed deliberately as a separator between two columns, there is nothing wrong. Secondly, this ldapsearch command runs correctly alone on the shell while running directly. This is only problem while running with shell script. Commented Feb 5, 2017 at 16:20

1 Answer 1

1

It seems like the real problem you have is formatting your columns. You have two list of IPs stored in PLIST and DLIST as strings, separated by newlines. When you type

printf "%-30s : %10s\n"  "$PLIST"    "$DLIST"

It will not automatically format those into columns for you.

You really need to change the way you're parsing your LDAP results. /bin/sh is really not inherently suited to this kind of output formatting.

If you have the option of using bash (version > 4), use mapfile and restructure your program like this:

#!/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

This uses bash's mapfile to store the output of the ldap search commands in an indexed array and prints it out in a formatted column.

As a test, I wrote this out and replaced your ldap commands with mock seq calls to generate numbers. Here's a sample run.

Sign up to request clarification or add additional context in comments.

5 Comments

@ffedgling .. i have bash 4.1 while running this , it gives the token erroe as below .. line 5: syntax error near unexpected token <'** **line 5: mapfile -t PLIST < <(ldapsearch -h mylap -x -b "ou=profile,o=cadence.com" "cn=*" preferredserverlist -LLL | awk '/preferredserverlist: / {print $2}')'
@ffedgling ..your sample code works but while using that method with real code thats returns no output on the terminal except header.
@Karn Sorry about, I forgot to replace X and Y with PLIST and DLIST, can you please try it again?
@ffedgling ..awesome , that's working indeed :) even i overlooked that as well. Moreover, can we add third column "LOCATION" we can have below command..putting that into LOCT var ldapsearch -h myldap -x -b "ou=profile,o=cadence.com" "cn=*" defaultserverlist -LLL | awk '/dn: cn=/ {print $2}'| cut -d "," -f1 | head also appreciate if you can explain the code bit on your answer.
@Karn you should be able to add the third column based on what I've done here. I'll add comments inline to explain the code a bit more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.