I have two commands. The first, when stored in a script variable, gives output like this:
one two three four five
The second also gives a list, but some of the items may be missing that were in the first command:
one three five
I want my script to do something if an item is in the first command but not the second. None of the items will have spaces (they tend to be kabab-format). How can I do this in Bash?
One approach using the current variables, and relying on the fact that individual values do not contain embedded white space:
NOTE: do not wrap the
${var1}and${var2}references in double quotes, ie, we want word splitting to occur when feeding theprintfcallsAnother idea using an associative array to track unique values:
This generates:
NOTES:
${var1}and${var2}references in double quotes, ie, we want word splitting to occurawkscriptarr[]from${var1}) will eliminate duplicates from${var1}, eg,var1='one one one'would lead to a single array entry:arr[one]=1