Bash: Delete items present in one array from another
As a comment said, it's just syntax errors.
Space-delimit the array entries, don't use the @
, and the function is fine for your example data. Note that it removes any entry containing an entry in tapes_in_drives
, not just those that match an entry it exactly.
tapes=(SP0001 SP0002 SP0434 SP0995 SP1026 SP2000 SP3000)
tapes_in_drives=(SP1026 SP0995 SP0434)
for i in "${tapes_in_drives[@]}"; do
tapes=(${tapes[@]//*$i*})
done
Results:
$ echo ${tapes[0]}
SP0001
$ echo ${tapes[1]}
SP0002
$ echo ${tapes[2]}
SP2000
$ echo ${tapes[3]}
SP3000
$ echo ${tapes[4]}
$
EDIT to respond to the edit in the question
This line:
export tapes=`vmquery -rn 1 -b | tail +4 | awk '{print \$1}' && vmquery -rn 4 -b | tail +4 | awk '{print \$1}'`
And this line:
export tapes_in_drives=`ssh srv-reg-nbms-01 "echo 's d q'|/usr/openv/volmgr/bin/tldtest -r /dev/smc0|grep 'Barcode'" | awk '{print $3}' && ssh srv-reg-nbms-02 "echo 's d q'|/usr/openv/volmgr/bin/tldtest -r /dev/smc0|grep 'Barcode'" | awk '{print $3}'`
initialise tapes
and tapes)in_drives
as strings, not arrays. You need to add parentheses around the value being assigned to make them into arrays, or the loop won't work. You can also drop the export
, it isn't necessary unless you want processes spawned by the script to inherit those shell variables as environment variables.
tapes=(`vmquery -rn 1 -b | tail +4 | awk '{print \$1}' && vmquery -rn 4 -b | tail +4 | awk '{print \$1}'`)
tapes_in_drives=(`ssh srv-reg-nbms-01 "echo 's d q'|/usr/openv/volmgr/bin/tldtest -r /dev/smc0|grep 'Barcode'" | awk '{print $3}' && ssh srv-reg-nbms-02 "echo 's d q'|/usr/openv/volmgr/bin/tldtest -r /dev/smc0|grep 'Barcode'" | awk '{print $3}'`)
Another solution:
tapes_in_drives=( SP1026 SP0995 SP0434 )
tapes=(SP0001 SP0002 SP0434 SP0995 SP1026 SP2000 SP3000)
tps=" ${tapes[*]} " # stringify the array
for item in ${tapes_in_drives[@]}; do
tps=${tps/ ${item} / } # replace item
done
tapes=( $tps ) # replace the array