簡體   English   中英

bash - 使用帶有 find 命令的數組

[英]bash - using array with find command

我想將找到的目錄附加到數組。

#!/bin/bash
FILES=()
find . ! \( \( -path './android/out' -o -path './.repo' \) -prune \) -type d -name prebuilts | while read file; do
  echo "FILES -->$file"
  FILES+=("$file")
done
echo "num of FILES: ${#FILES[@]}"
echo "FILES: ${FILES[@]}"

但結果如下:

FILES -->./android/test/vts/prebuilts
FILES -->./android/system/apex/shim/prebuilts
FILES -->./android/system/sepolicy/prebuilts
FILES -->./android/vendor/tvstorm/prebuilts
FILES -->./android/vendor/dmt/prebuilts
FILES -->./android/kernel/prebuilts
FILES -->./android/prebuilts
FILES -->./android/developers/build/prebuilts
FILES -->./android/external/selinux/prebuilts
FILES -->./android/development/vndk/tools/header-checker/tests/integration/version_script_example/prebuilts
num of FILES: 0
FILES: 

為什么數組的 num 是 0?

您在|之后填充數組 ,即在子外殼中。 來自子 shell 的更改不會傳播到父 shell。

改用進程替換:

while read file; do
    echo "FILES -->$file"
    FILES+=("$file")
done < <(find . ! \( \( -path './android/out' -o -path './.repo' \) -prune \) -type d -name prebuilts)

如果不需要作業控制,也可以shopt -s lastpipe在當前 shell 的管道中運行最后一條命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM