views:

82

answers:

5

How can I convert a string containing glob characters such as

/var/lib/gems/*/bin

into a colon-separated string of filenames (i.e. PATH compatible) matching the pattern?

i.e. echo /var/lib/gems/*/bin will return

/var/lib/gems/1.8/bin /var/lib/gems/1.9.1/bin

I want

/var/lib/gems/1.8/bin:/var/lib/gems/1.9.1/bin 

instead.

The obvious approach is simply to replace the space character with ':' via tr, but that doesn't work if the filename itself contains the space character.

+1  A: 
printf "%s\n" /var/lib/gems/*/bin | tr "\n" ":"
ghostdog74
Close! But if there's only one match, you get a colon at the end: "/var/lib/gems/1.9.1/bin:". (And if there's no match, a single colon.)
mjs
+4  A: 

This should do it for you:

dirs=(/var/lib/gems/*/bin)    # put filenames (dirnames) in an array
saveIFS=$IFS IFS=':'          # set the Internal Field Separator to the desired delimiter
dirs=("${dirs[*]}")           # convert the array to a scalar with the new delimiter
IFS=$saveIFS                  # restore IFS
Dennis Williamson
A: 
PATH="$(printf "%s:" /usr/*/bin)"
PATH="${PATH%:}"
timo
This works, but I'd use a different variable. You can also do the inital assignment this way: `printf -v varname "%s:" /var/lib/gems/*/bin`
Dennis Williamson
A: 

It's pretty trivial if you drop into Perl:

perl -e 'print join ":", @ARGV' /var/lib/gems/*/bin

Or Python:

python -c 'import sys; print ":".join(sys.argv[1:])' /var/lib/gems/*/bin

Or any number of other popular scripting languages.

Sean
It seems as though this might be the best approach overall (the IFS fiddling is a bit messy), although it is a shame to have have to use an external program...
mjs
+2  A: 

Actually, I thought of a better solution: use a shell function.

function join() {
    local IFS=$1
    shift
    echo "$*"
}

mystring=$(join ':' /var/lib/gems/*/bin)
Sean