开发者

Globbing/pathname expansion with colon as separator

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.


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

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

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


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


PATH="$(printf "%s:" /usr/*/bin)"
PATH="${PATH%:}"


No need to mess with IFS, zsh can join arrays with a simple variable flag:

dirs=(/var/lib/gems/*/bin(N))
dirs=${(j.:.)dirs}

The (N) on the first line suppresses a warning if there are no files; the (j.:.) joins the array with :s. Works with 0, 1, or multiple matches.


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.


without saving IFS and command substitution

dirs=(/var/lib/gems/*/bin) ; IFS=: eval 'dirs="${dirs[*]}"'


printf "%s\n" /var/lib/gems/*/bin | tr "\n" ":"


Another oneliner: printf "%s\n" /var/lib/gems/*/bin | paste -s -d':'

But @timo's answer is better in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜