mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2144 (markdown)
31
SC2144.md
Normal file
31
SC2144.md
Normal file
@@ -0,0 +1,31 @@
|
||||
## -e doesn't work with globs. Use a for loop.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
if [ -e dir/*.mp3 ]
|
||||
then
|
||||
echo "There are mp3 files."
|
||||
fi
|
||||
|
||||
### Correct code:
|
||||
|
||||
for file in dir/*.mp3
|
||||
do
|
||||
if [ -e "$file" ]
|
||||
then
|
||||
echo "There are mp3 files"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
### Rationale:
|
||||
|
||||
`[ -e file* ]` only works if there's 0 or 1 matches. If there are multiple, it becomes `[ -e file1 file2 ]`, and the test fails.
|
||||
|
||||
`[[ -e file* ]]` doesn't work at all.
|
||||
|
||||
Instead, use a for loop to expand the glob and check each result individually.
|
||||
|
||||
### Contraindications
|
||||
|
||||
None.
|
Reference in New Issue
Block a user