shell Bash:删除以特定字符串开头的文件[已关闭]

laawzig2  于 2023-06-24  发布在  Shell
关注(0)|答案(1)|浏览(153)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
我从一个存储帐户中获得一个blob列表,我想删除它们不以mf开头的文件,我有下一个命令:

blob=$(az storage blob list -c '$web' --account-name "$SA_NAME" --query "[].{name:name}" --output tsv)
prefix="mf"
for b in $blob
do
  if [[ "$blob" == "$prefix"* ]];
  then
    echo "file $b: is not deleted"
  else
    echo "file $b: is deleted"
  fi
  
done

我得到以下输出,其中删除了所有文件,包括以mf开头的文件:

file assets/fonts/Brevia-SemiboldItalic.ttf: is deleted
file assets/fonts/Brevia-SemiboldItalic.woff: is deleted
file assets/locales/es.json: is deleted
file assets/microapp-dev.json: is deleted
file assets/microapp-local.json: is deleted
file assets/microapp.json: is deleted
file favicon.ico: is deleted
file main.js: is deleted
file main.js.map: is deleted
file mf/angular-mfe/1.89801b3a85a55b24.js: is deleted
file mf/angular-mfe/181.66ff660a1257fc84.js: is deleted
file mf/angular-mfe/197.596dd9d1162203a2.js: is deleted
file mf/angular-mfe/340.c07e82cc376fa4c7.js: is deleted
file mf/angular-mfe/3rdpartylicenses.txt: is deleted
file mf/angular-mfe/471.deda3ccf16192f26.js: is deleted
file mf/angular-mfe/529.c3fdd0c3260c48da.js: is deleted
file mf/angular-mfe/650.48ddcd2f364e73da.js: is deleted
file mf/angular-mfe/797.8d193779021f0a0b.js: is deleted
file mf/angular-mfe/895.a91f2f017e309e9d.js: is deleted
jmo0nnb3

jmo0nnb31#

我认为您需要更改if语句中的条件,将每个blob名称$b与前缀进行比较,而不是将整个blob名称列表$blob与前缀进行比较
比如这样:

blob=$(az storage blob list -c '$web' --account-name "$SA_NAME" --query "[].{name:name}" --output tsv)
prefix="mf"
for b in $blob
do
  if [[ "$b" == "$prefix"* ]];
  then
    echo "file $b: is not deleted"
  else
    echo "file $b: is deleted"
    # Delete the blob
    az storage blob delete -c '$web' --account-name "$SA_NAME" -n "$b"
  fi
done

结果示例:

file mf/file1.txt: is not deleted
file file2.txt: is deleted
file mf/file3.txt: is not deleted
file file4.txt: is deleted

相关问题