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