此条目似乎是我想要做的事情的80%,但我提出的任何工作都不起作用:bash: how to delete elements from an array based on a pattern
我有两个数组:
@tapes_in_drives=(SP1026,SP0995,SP0434)
@tapes=(SP0001,SP0002,SP0434,SP0995,SP1026,SP2000,SP3000)
我需要一个bash解决方案,可以删除@tapes中@tapes_in_drives中的所有条目。
我正在构建一个脚本,以根据到期日期等自动从磁带库中弹出磁带。
我试过了:
for i in "${tapes_in_drives[@]}"; do
tapes=(${tapes[@]//*$i*})
done
但它不起作用,没有删除。
感谢您提供的任何帮助。
修改
以下是我正在使用的代码,我最初是用Perl编写的,因此@variable定义,当我写这个问题时,我感到非常疲惫。
CODE:
#!/usr/bin/bash
# If media expires less than this many days, keep it
export days_expired="30"
# Set to 1 to get debug output to console
export debug="1"
# Get list of SPxxxxx tapes that are physically in the library
if [ $debug -eq 1 ]; then echo "Getting tape list"; fi
export tapes=`vmquery -rn 1 -b | tail +4 | awk '{print \$1}' && vmquery -rn 4 -b | tail +4 | awk '{print \$1}'`
if [ $debug -eq 1 ]; then echo ${tapes[@]}; fi
# Query tape drives for tapes
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}'`
if [ $debug -eq 1 ]; then
echo ""
echo "Tapes in Drives:"
echo ${tapes_in_drives[@]}
echo "";
fi
# Remove tapes in drives from list of tapes
for i in "${tapes_in_drives[@]}"; do
tapes=(${tapes[@]//*$i*})
done
echo "Tape List 2"
echo ${tapes[@]}
结果:
获取磁带列表
SP0011 SP0039 SP0402 SP0434 SP0464 SP0516 SP0551 SP0600 SP0604 SP0726 SP0731 SP0765 SP0767 SP0779 SP0781 SP0787 SP0793 SP0794 SP0805 SP0828 SP0830 SP0832 SP0927 SP0928 SP0936 SP0983 SP0995 SP1001 SP1004 - 得自用SP1008 SP1015 SP1017 SP1026 SP1033 SP1036 SP1038 SP0042 SP0049 SP0150 SP0462 SP0473 SP0517 SP0557 SP0560 SP0642 SP0659 SP0697 SP0712 SP0723 SP0766 SP0777 SP0786 SP0788 SP0792 SP0907 SP0910 SP0923 SP0925 SP0926 SP0940 SP0963 SP0981 SP0986 SP0989 SP0994 SP0999 SP1007 SP1020 SP1021 SP1027 SP1039
驱动器中的磁带:
SP1001 SP1038 SP0923 SP0926 SP0925
磁带列表2
SP0011 SP0039 SP0402 SP0434 SP0464 SP0516 SP0551 SP0600 SP0604 SP0726 SP0731 SP0765 SP0767 SP0779 SP0781 SP0787 SP0793 SP0794 SP0805 SP0828 SP0830 SP0832 SP0927 SP0928 SP0936 SP0983 SP0995 SP1001 SP1004 - 得自用SP1008 SP1015 SP1017 SP1026 SP1033 SP1036 SP1038 SP0042 SP0049 SP0150 SP0462 SP0473 SP0517 SP0557 SP0560 SP0642 SP0659 SP0697 SP0712 SP0723 SP0766 SP0777 SP0786 SP0788 SP0792 SP0907 SP0910 SP0923 SP0925 SP0926 SP0940 SP0963 SP0981 SP0986 SP0989 SP0994 SP0999 SP1007 SP1020 SP1021 SP1027 SP1039
如您所见,tapes_in_drives中的磁带名称未从磁带阵列中删除。
答案 0 :(得分:12)
正如评论所说,这只是语法错误。
空格分隔数组条目,不要使用@
,并且该函数适用于您的示例数据。请注意,它会删除包含tapes_in_drives
中条目的所有条目,而不仅仅是那些与条目完全匹配的条目。
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
结果:
$ echo ${tapes[0]}
SP0001
$ echo ${tapes[1]}
SP0002
$ echo ${tapes[2]}
SP2000
$ echo ${tapes[3]}
SP3000
$ echo ${tapes[4]}
$
编辑以回复问题中的修改
这一行:
export tapes=`vmquery -rn 1 -b | tail +4 | awk '{print \$1}' && vmquery -rn 4 -b | tail +4 | awk '{print \$1}'`
这一行:
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}'`
将tapes
和tapes)in_drives
初始化为字符串,而不是数组。您需要在分配的值周围添加括号以使它们成为数组,否则循环将不起作用。您也可以删除export
,除非您希望脚本生成的进程将这些shell变量作为环境变量继承,否则没有必要。
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}'`)
答案 1 :(得分:5)
另一种解决方案:
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
答案 2 :(得分:0)
从this answer起被盗,并适应您的变量:
tapes_in_drives=(SP1026 SP0995 SP0434)
tapes=(SP0001 SP0002 SP0434 SP0995 SP1026 SP2000 SP3000)
free_tapes=( $(printf "%s\n" "${tapes[@]}" "${tapes_in_drives[@]}" | sort | uniq -u) )
echo "${free_tapes[@]}"
输出:
SP0001 SP0002 SP2000 SP3000
-u
切换到uniq
使其“仅打印唯一行”,因此它排除了两个阵列中的磁带。