POSIX
To normalize the slashes in all the parameters, I'll use the rotating argument trick: shift $1
off, transform it and put the result at the end of the parameter list. If you do that as many time as there are parameters, you've transformed all the parameters, and you've got them back in order.
For the second part of the code, I changed your logic to be less confusing: the outer loop iterates over the parameters, and the inner loop iterates over path components. for x; do … done
iterates over the positional parameters, it's a convenient idiom. I use a POSIX-compliant way of matching a string against a pattern: the case
construct.
Tested with dash 0.5.5.1, pdksh 5.2.14, bash 3.2.39, bash 4.1.5, ksh 93s+, zsh 4.3.10.
Side note: there seems to be a bug in bash 4.1.5 (not in 3.2): if the case pattern is "${common_path%/}"/*
, one of the tests fails.
posix_path_common () {
for tmp; do
tmp=$(printf %s. "$1" | tr -s "/")
set -- "$@" "${tmp%.}"
shift
done
common_path=$1; shift
for tmp; do
while case ${tmp%/}/ in "${common_path%/}/"*) false;; esac; do
new_common_path=${common_path%/*}
if [ "$new_common_path" = "$common_path" ]; then return 1; fi
common_path=$new_common_path
done
done
printf %s "$common_path"
}
bash, ksh
If you're in bash (or ksh), you can use arrays — I don't understand why you seem to be restricting yourself to the positional parameters. Here's a version that uses an array. I have to admit it's not particularly clearer than the POSIX version, but it does avoid the initial n^2 shuffling.
For the slash normalization part, I use the ksh93 construct ${foo//PATTERN/REPLACEMENT}
construct to replace all occurrences of PATTERN
in $foo
by REPLACEMENT
. The pattern is +(\/)
to match one or more slash; under bash, shopt -s extglob
must be in effect (equivalently, start bash with bash -O extglob
). The construct set ${!a[@]}
sets the positional parameters to the list of subscripts of the array a
. This provides a convenient way to iterate over the elements of the array.
For the second part, I the same loop logic as the POSIX version. This time, I can use [[ … ]]
since all the shells targeted here support it.
Tested with bash 3.2.39, bash 4.1.5, ksh 93s+.
array_path_common () {
typeset a i tmp common_path new_common_path
a=("$@")
set ${!a[@]}
for i; do
a[$i]=${a[$i]//+(\/)//}
done
common_path=${a[$1]}; shift
for tmp; do
tmp=${a[$tmp]}
while [[ "${tmp%/}/" != "${common_path%/}/"* ]]; do
new_common_path="${common_path%/*}"
if [[ $new_common_path = $common_path ]]; then return 1; fi
common_path="$new_common_path"
done
done
printf %s "$common_path"
}
zsh
Sadly, zsh lacks the ${!array[@]}
feature to execute the ksh93 version as-is. Fortunately, zsh has two features that make the first part a breeze. You can index the positional parameters as if they were the @
array, so there's no need to use an intermediate array. And zsh has an array iteration construct: "${(@)array//PATTERN/REPLACEMENT}"
performs the pattern replacement on each array element in turn and evaluates to the array of results (confusingly, you do need the double quotes even though the result is multiple words; this is a generalization of "$@"
). The second part is essentially unchanged.
zsh_path_common () {
setopt local_options extended_glob
local tmp common_path new_common_path
set -- "${(@)@//\/##//}"
common_path=$1; shift
for tmp; do
while [[ "${tmp%/}/" != "${common_path%/}/"* ]]; do
new_common_path="${common_path%/*}"
if [[ $new_common_path = $common_path ]]; then return 1; fi
common_path="$new_common_path"
done
done
printf %s "$common_path"
}
Test cases
My solutions are minimally tested and commented. I've changed the syntax of your test cases to parse under shells that don't have $'…'
and report failures in a more convenient way.
do_test () {
if test "$@"; then echo 0; else echo $? "$@"; failed=$(($failed+1)); fi
}
run_tests () {
function_to_test=$1; shift
failed=0
do_test "$($function_to_test /a/b/c/d /a/b/e/f; echo x)" = /a/bx
do_test "$($function_to_test /long/names/foo /long/names/bar; echo x)" = /long/namesx
do_test "$($function_to_test / /a/b/c; echo x)" = /x
do_test "$($function_to_test a/b/c/d a/b/e/f ; echo x)" = a/bx
do_test "$($function_to_test ./a/b/c/d ./a/b/e/f; echo x)" = ./a/bx
do_test "$($function_to_test '
/
/
' '
/
'; echo x)" = '
/
'x
do_test "$($function_to_test --/-- --; echo x)" = '--x'
do_test "$($function_to_test '' ''; echo x)" = x
do_test "$($function_to_test /foo/bar ''; echo x)" = x
do_test "$($function_to_test /foo /fo; echo x)" = x
do_test "$($function_to_test '--$`\! *@ \a\b\e\E\f\r\t\v\\\"'\''
' '--$`\! *@ \a\b\e\E\f\r\t\v\\\"'\''
'; echo x)" = '--$`\! *@ \a\b\e\E\f\r\t\v\\\"'\''
'x
do_test "$($function_to_test /foo/bar //foo//bar//baz; echo x)" = /foo/barx
do_test "$($function_to_test foo foo; echo x)" = foox
do_test "$($function_to_test /fo /foo; echo x)" = x
if [ $failed -ne 0 ]; then echo $failed failures; return 1; fi
}
There's just about one new syntax element per line, nice...
I'll annotate each line with the relevant section from man bash
- may be helpful as is, or in combination with another answer:
From the argument $1
, cut out 1 char starting at 0 and check it's a /
:
if [ ${1:0:1} = '/' ]
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of the
value of parameter starting at the character specified by off‐
set. If parameter is @, an indexed array subscripted by @ or *,
or an associative array name, the results differ as described
below. If length is omitted, expands to the substring of the
value of parameter starting at the character specified by offset
and extending to the end of the value. length and offset are
arithmetic expressions (see ARITHMETIC EVALUATION below).
If offset evaluates to a number less than zero, the value is
used as an offset in characters from the end of the value of
parameter. If length evaluates to a number less than zero, it
is interpreted as an offset in characters from the end of the
value of parameter rather than a number of characters, and the
expansion is the characters between offset and that result.
Note that a negative offset must be separated from the colon by
at least one space to avoid being confused with the :- expan‐
sion.
Leave char 0 out and get chars from 1 to the end from $1
:
tmp=${1:1} # Strip off leading '/' . . .
See section above, first case.
For arguments like --foo=bar
, cut off text matching '=*' from the right, as much as possible to the left (think of handling --foo=bar=baz
):
parameter=${tmp%%=*} # Extract name.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion of the expanded value of parameter, then the
result of the expansion is the expanded value of parameter with
the shortest matching pattern (the ``%'' case) or the longest
matching pattern (the ``%%'' case) deleted. If parameter is @
or *, the pattern removal operation is applied to each posi‐
tional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or
*, the pattern removal operation is applied to each member of
the array in turn, and the expansion is the resultant list.
For arguments like --foo=bar
, cut off text matching '*=' from the left, as much as possible to the right (think of handling --foo=bar=baz
):
value=${tmp##*=} # Extract value.
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
the beginning of the value of parameter, then the result of the
expansion is the expanded value of parameter with the shortest
matching pattern (the ``#'' case) or the longest matching pat‐
tern (the ``##'' case) deleted. If parameter is @ or *, the
pattern removal operation is applied to each positional parame‐
ter in turn, and the expansion is the resultant list. If param‐
eter is an array variable subscripted with @ or *, the pattern
removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.
(Note: the example case --foo=bar=baz
is not supported as --foo
and bar=baz
, but as --foo
and baz
)
Source: section Parameter Expansion in man bash
,
man bash | less '+/Parameter Expansion'
(or, shorter man bash | less '+/##'
)
Best Answer
This is not
bash
specific but it existed in the Bourne Shell since 1976.Check the Bourne Shell man page:
http://schillix.sourceforge.net/man/man1/bosh.1.html
Check section Parameter Substitution currently starting on page 7.
For a complete overview, there is: