You are not logged in.
I have this script to edit conf files in $XDG_CONFIG_HOME. It works fine:
#!/bin/sh
dir=$XDG_CONFIG_HOME/$1
if [ -d "$dir" ]; then
for f in $dir/* ; do
file=${f##*/}
case $file in
*conf | config | *.cfg | *rc) $EDITOR "$f" ;;
*) : ;;
esac
done
fi
It has already been pointed out that this could easily be achieved in Bash 4 with:
#!/bin/bash
shopt -s extglob failglob
$EDITOR ~/.config/$1/@(conf|config|*.cfg|*rc)
The shortcoming with my script is that it only handles files correctly placed in $XDG_CONFIG_HOME, and as many apps litter thier conf files about the shop, I wanted to try something that dealt with those cases. Hence this.
#!/bin/sh
dirs=($HOME/.$1* $HOME/.$1/ $XDG_CONFIG_HOME/$1/)
search=$(find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" -o -name \
"config" -o -name "*rc" -o -name "*.cfg" \) 2>/dev/null )
for f in $search; do
$EDITOR "$f"
done
...which again works, but seems pretty ugly.
I'd appreciate any comments on approaches to the same problem that achieve the result in a more efficient and portable way.
Last edited by jasonwryan (2011-10-18 02:53:06)
Offline
Instead of:
search=$(find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" -o -name \
"config" -o -name "*rc" -o -name "*.cfg" \) 2>/dev/null )
You should either read the results into an array and act on the array, or launch the editor straight from find via exec.
Using an array:
IFS=$'\n' read -r -d '' -a files < \
<(find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" -o -name "config" -o -name "*rc" -o -name "*.cfg" \) 2>/dev/null
(( ${#files[*]} )) && "$EDITOR" "${files[@]}"
Usind only find:
find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" -o -name "config" -o -name "*rc" -o -name "*.cfg" \) -exec "$EDITOR" {} +
I prefer the second method -- its right to the point and isn't shell specific. It is, however, somewhat GNU find specific.
Offline
Thanks falconindy. The find approach works well. The first method, however, has a syntax error:
/home/jason/Scripts/confs: line 5: syntax error near unexpected token `<'
/home/jason/Scripts/confs: line 5: ` <(find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" \'
I tried removing the line break, but it persists. I'm not sure what else is causing it.
Offline
Thanks falconindy. The find approach works well. The first method, however, has a syntax error:
/home/jason/Scripts/confs: line 5: syntax error near unexpected token `<' /home/jason/Scripts/confs: line 5: ` <(find "${dirs[@]}" -type f \( -name "*.conf" -o -name "conf" \'
I tried removing the line break, but it persists. I'm not sure what else is causing it.
#!/bin/sh is causing it, because <(process substitution) is bashism.
-- EDIT --
BTW, if "portability" isn't a concern, the globstar shopt of bash(1) should make this really easy.
Last edited by lolilolicon (2011-10-18 05:17:53)
This silver ladybug at line 28...
Offline
D'oh! Thanks lolilolicon
Offline
@falconindy: can you elaborate a little on this:
(( ${#files[*]} )) && "$EDITOR" "${files[@]}"
It evaluates the arithmetic expression of the number of elements of the array before passing it to the editor? Is that to determine that it is non-empty?
Offline
Offline
jasonwryan wrote:Is that to determine that it is non-empty?
Spot on.
Everyone gets lucky once in a while...
Thanks - that's a tidy little trick.
Offline