Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare lists of comma separated options

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
I want to compare list of default shells with list of shells on a host (to get "missing" and superfluous shells configured on a host).
Below is my current solution in ksh script using arrays.
Do you see it could be done more efficient/easier with e.g. awk or the solution I paste is good enough?

Code:
$ cat ./test_arr
#!/bin/ksh

set -A stdshells $(echo $S1|sed s/\,/\ /g)
set -A hstshells $(echo $S2|sed s/\,/\ /g)

checkshells ()
{
for a in "${stdshells[@]}"; do
  if [[ " ${hstshells[*]} " != *" $a "* ]]; then
    printf "$a(-),"
  fi
done

for a in "${hstshells[@]}"; do
  if [[ " ${stdshells[*]} " != *" $a "* ]]; then
    printf "$a(+),"
  fi
done
}

checkshells | awk '{sub(/,$/,"");print}'

$ export S1=/bin/bsh,/bin/csh,/bin/false,/bin/ftponly
$ export S2=/bin/bsh,/bin/csh,/bin/extra,/bin/ftponly
$ ./test_arr
/bin/false(-),/bin/extra(+)
$ export S1=/bin/bsh,/bin/csh,/bin/false,/bin/ftponly,/bin/csh
$ ./test_arr
/bin/false(-),/bin/extra(+)
$ export S2=/bin/bsh,/bin/csh,/bin/extra,/bin/ftponly,/bin/csh,/bin/csh
$ ./test_arr
/bin/false(-),/bin/extra(+)
$ export S1=/bin/bsh,/bin/csh,/bin/false,/bin/ftponly,/bin/csh,/bin/csh2
$ ./test_arr
/bin/false(-),/bin/csh2(-),/bin/extra(+)
$ export S2=/bin/bsh,/bin/csh,/bin/extra,/bin/ftponly,/bin/csh,/bin/csh,EXTRA
$ ./test_arr
/bin/false(-),/bin/csh2(-),/bin/extra(+),EXTRA(+)
$
 
Seems good enough, I would probably have sorted the two lists and used 'diff' and customised output format, or 'comm' and my own format handling, but I don't think it would be any better really. And yes, you could use perl/awk/whatever but it would just be the same code as you wrote in a different language...

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Why using arrays ?
Code:
#!/bin/ksh
IFS=",$IFS"
checkshells() {
for a in $S1; do
  if [[ ",$S2," != *",$a,"* ]]; then
    printf "$a(-),"
  fi
done
for a in $S2; do
  if [[ ",$S1," != *",$a,"* ]]; then
    printf "$a(+),"
  fi
done
}
checkshells | awk '{sub(/,$/,"");print}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top