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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

store result of command into an array

Status
Not open for further replies.

lrhatice

Instructor
Mar 20, 2013
2
US
Hey guys,

New on this forum. Would really appreciate some help on this issue I'm having. I'm trying to store
the result of this command into an array so that I can compare this array with another array so
I can get the difference between the two. My first problem is storing this output into an array? Any suggestions would be great guys.

launchctl list | egrep -v "com.apple|anonymous.launchd|anonymous|master|cron|ntpd|bombich|cupsd" | awk '{print $3}'

org.test.squid
org.test.disk
org.test.maint
 
And how would you compare 2 arrays ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[[ "${array1[@]}" = "${array2[@]}" ]] && echo same || echo different
 
I'd try this:
declare -a array2=$(launchctl list | egrep -v "com.apple|anonymous|master|cron|ntpd|bombich|cupsd" | awk '{print $3}')

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Perhaps just like this it would work:
Code:
array2=$(launchctl list | egrep -v "com.apple|anonymous|master|cron|ntpd|bombich|cupsd" | awk '{print $3}')
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

PS: In order for the compare statement you posted to work, you would need both arrays to be SORTED.
Otherwise they will be different even if the contain the same elements:
Code:
==> a1=$(ls -1 *.log)
==> echo $a1
EXTERNAL_EXAMPLE_13762668.log EXTERNAL_EXAMPLE_25100426.log load_example.log student.log
==> a2=$(ls -1r *.log)
==> echo $a2
student.log load_example.log EXTERNAL_EXAMPLE_25100426.log EXTERNAL_EXAMPLE_13762668.log
==> [[ "${a1[@]}" = "${a2[@]}" ]] && echo "Equal" || echo "Different"
Different
[noevil]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top