mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Add syntax highlighting to SC2045
26
SC2045.md
26
SC2045.md
@@ -2,18 +2,22 @@
|
|||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
for f in $(ls *.wav)
|
```sh
|
||||||
do
|
for f in $(ls *.wav)
|
||||||
|
do
|
||||||
echo "$f"
|
echo "$f"
|
||||||
done
|
done
|
||||||
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
for f in *.wav
|
```sh
|
||||||
do
|
for f in *.wav
|
||||||
|
do
|
||||||
[[ -e "$f" ]] || break # handle the case of no *.wav files
|
[[ -e "$f" ]] || break # handle the case of no *.wav files
|
||||||
echo "$f"
|
echo "$f"
|
||||||
done
|
done
|
||||||
|
```
|
||||||
|
|
||||||
Also note that in Bash, `shopt -s nullglob` will allow the loop to run 0 times instead of 1 if there are no matches. There are also [several other conditions](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) to be aware of.
|
Also note that in Bash, `shopt -s nullglob` will allow the loop to run 0 times instead of 1 if there are no matches. There are also [several other conditions](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) to be aware of.
|
||||||
|
|
||||||
@@ -23,10 +27,12 @@ When looping over a set of files, it's always better to use globs when possible.
|
|||||||
|
|
||||||
The following files can or will break the first loop:
|
The following files can or will break the first loop:
|
||||||
|
|
||||||
touch 'filename with spaces.wav'
|
```sh
|
||||||
touch 'filename with * globs.wav'
|
touch 'filename with spaces.wav'
|
||||||
touch 'More_Globs[2003].wav'
|
touch 'filename with * globs.wav'
|
||||||
touch 'files_with_fønny_chæracters_in_certain_locales.wav'
|
touch 'More_Globs[2003].wav'
|
||||||
|
touch 'files_with_fønny_chæracters_in_certain_locales.wav'
|
||||||
|
```
|
||||||
|
|
||||||
### Related resources:
|
### Related resources:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user