4 ms·
It's not necessary to bring Python into it, Bash can handle filenames with weird characters properly if you know how to use it. E.g. instead of `ls | grep -Ev
by bheadmaster 2y ago
It's not necessary to bring Python into it, Bash can handle filenames with weird characters properly if you know how to use it.
E.g. instead of `ls | grep -Ev 'wav|draft'`, you'd have to do something like
for filename in *; do
if grep -E 'wav|draft' >/dev/null <<< "$filename"
then : # ...
fi
done
Of course, it's more convoluted, but when you're writing scripts that might be used for a long time and by many people, it helps to know that it is possible to write robust things. Tools like shellcheck certainly help.
- PeterWhittaker 2y agogrep -q and you won’t need the redirect of stdout.
- ykonstant 2y agoThe above is perfectly fine for small directories, but in general the preferred way to loop over files is with find: find . ! -name . -prune \ -exec grep -qE 'wav|draft' {} \; \ -exec "${action}" \; ; Edit: I missed the herestring in the original code, so the above is wrong as mentioned in the comments; if your find has regex, you can use it to save one grep: find . ! -name . -prune \ -regex '.*wav.*\|.*draft.*' \ -exec "${action}" \; ; Otherwise you can call sh to printf the filename into a grep. However, the point of my post is that find can perform seek, filter and execute, and should be used for all three unless it is really impossible (which is unlikely).
- some_random 2y agoAt that point I think you need to ask yourself why you're using Bash to begin with. If it's just meant to be a quick script that's run occasionally then this is good but probably overkill. If it's going into prod to be run regularly as part of business critical, then it should be in a language that has a less convoluted way to _ls a directory_. There's an inflection point somewhere in there, where it is depends on you.
- mywittyname 2y agoAm I monitoring the execution? Yes: bash is probably fine. No: real programming language time.