You are not logged in.

#1 2011-10-18 02:52:20

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 23,007
Website

Script to edit conf files: suggestions & comment sought

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)


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2 2011-10-18 03:37:35

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,109
Website

Re: Script to edit conf files: suggestions & comment sought

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

#3 2011-10-18 04:42:31

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 23,007
Website

Re: Script to edit conf files: suggestions & comment sought

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.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#4 2011-10-18 05:13:32

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,716

Re: Script to edit conf files: suggestions & comment sought

jasonwryan wrote:

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

#5 2011-10-18 05:15:34

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 23,007
Website

Re: Script to edit conf files: suggestions & comment sought

D'oh! Thanks lolilolicon smile


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#6 2011-10-18 08:54:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 23,007
Website

Re: Script to edit conf files: suggestions & comment sought

@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?


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#7 2011-10-18 09:54:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,109
Website

Re: Script to edit conf files: suggestions & comment sought

jasonwryan wrote:

Is that to determine that it is non-empty?

Spot on. Otherwise, the array expands to nothing and your editor is invoked without any file arguments.

Offline

#8 2011-10-18 10:26:56

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 23,007
Website

Re: Script to edit conf files: suggestions & comment sought

falconindy wrote:
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.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB