shellcheck は、検索開始パスが指定されていても、検索出力のループについて警告します。

shellcheck は、検索開始パスが指定されていても、検索出力のループについて警告します。

Ubuntu16.04

#!/bin/bash

site="hello"
wDir="/home/websites/${site}/httpdocs/"

for file in $(find "${wDir}" -name "*.css")
do
   echo "$file";
done
exit 0;

起動ディレクトリを定義してもShellcheckで警告が表示されますが、スクリプトは正常に動作します。

root@me /scripts/ # shellcheck test.sh

In test.sh line 6:
for file in $(find "${wDir}" -name "*.css")
            ^-- SC2044: For loops over find output are fragile. Use find -exec or a while read loop.

ベストアンサー1

問題はまさにshellcheckが言うことです:for繰り返しループや同様のコマンドの出力は脆弱です。findたとえば、

$ ls
'a file with spaces' 

$ for file in $(find . ); do    echo "$file"; done
.
./a
file
with
spaces

安全な方法は以下-execを使用することですfind

$ find . -exec echo  {} \;
.
./a file with spaces

またはwhileループを使用してください。

$ find . -print0 | while IFS= read -r -d '' file; do echo "$file"; done
.
./a file with spaces

おすすめ記事