Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to refer to indexes in $@? I can't find any reference to use like the following anywhere in GrayCat's wiki, and the Advanced Scripting Guide and others assign this to a different variable before modifying that instead.

$ echo ${@[0]}
-bash: ${@[0]}: bad substitution

The goal is DRY: The first argument is used for one thing, and the rest for something else, and I'd like to avoid duplicating either the code to normalize, the $@ array, or to create a separate function for this (although at this point it's probably the easiest way out).

Clarification: The object was to modify the values of the variable-length $@ to make the code easier to debug. The current version is a bit too hacky for my liking, although it works even for bizarre paths like

$'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n'

Update: Looks like this isn't possible. The code now uses both code and data duplication, but at least it works:

path_common()
{
    # Get the deepest common path.
    local common_path="$(echo -n "${1:-}x" | tr -s '/')"
    common_path="${common_path%x}"
    shift # $1 is obviously part of $1
    local path

    while [ -n "${1+defined}" ]
    do
        path="$(echo -n "${1}x" | tr -s '/')"
        path="${path%x}"
        if [[ "${path%/}/" = "${common_path%/}/"* ]]
        then
            shift
        else
            new_common_path="${common_path%/*}"
            [ "$new_common_path" = "$common_path" ] && return 1 # Dead end
            common_path="$new_common_path"
        fi
    done
    printf %s "$common_path"
}

Bounty goes to anyone who can get rid of the duplication of code to collapse duplicate slashes or duplication of data to hold $1 and the other parameters, or both, while keeping the code a reasonable size and succeeding all the unit tests:

test "$(path_common /a/b/c/d /a/b/e/f; echo x)" = /a/bx
test "$(path_common /long/names/foo /long/names/bar; echo x)" = /long/namesx
test "$(path_common / /a/b/c; echo x)" = /x
test "$(path_common a/b/c/d a/b/e/f ; echo x)" = a/bx
test "$(path_common ./a/b/c/d ./a/b/e/f; echo x)" = ./a/bx
test "$(path_common $'\n/\n/\n' $'\n/\n'; echo x)" = $'\n/\n'x
test "$(path_common --/-- --; echo x)" = '--x'
test "$(path_common '' ''; echo x)" = x
test "$(path_common /foo/bar ''; echo x)" = x
test "$(path_common /foo /fo; echo x)" = x
test "$(path_common $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n' $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n'; echo x)" = $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n'x
test "$(path_common /foo/bar //foo//bar//baz; echo x)" = /foo/barx
test "$(path_common foo foo; echo x)" = foox
test "$(path_common /fo /foo; echo x)" = x
share|improve this question
add comment

5 Answers

up vote 10 down vote accepted
+50

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
}
share|improve this answer
    
+50, just wow. More than I asked for, at any rate. You, sir, are awesome. –  l0b0 Apr 28 '11 at 7:59
add comment

The first argument is used for one thing, and the rest for something else,

I think what you want is shift

$ set one two three four five
$ echo $@
one two three four five
$ echo $1
one
$ foo=$1
$ echo $foo
one
$ shift
$ echo $@
two three four five
$ shift 2
$ echo $@
four five
$ echo $1
four
share|improve this answer
add comment

Why don't you just use $1, $2 .. $9, ${10}, ${11}.. and so on? It's even more DRY-er than what you're attempting to do :)

More on the relation between $number and $@:

$@ can be considered as shorthand for "all elements of an array containing all arguments"

So, $@ is a sort-of shorthand of ${args[@]} (args here is a 'virtual' array containing all arguments -- not a real variable, mind you)

$1 is ${args[1]}, $2 is ${args[2]}, and so on.

When you've hit [9], use a brace: ${10} is ${args[10]}, ${11} is ${args[11]}, and so on.


Indirectly use a command line argument

argnum=3  # You want to get the 3rd arg
do-something ${!argnum}  # Do something with the 3rd arg

Example:

argc=$#
for (( argn=1; argn<=argc; argn++)); do
    if [[ ${!argn} == "foo" ]]; then
        echo "Argument $argn of $argc is 'foo'"
    fi
done
share|improve this answer
    
The obvious downside of having to use $*number* is that you can't use an index variable as with ${args[$i]}. –  intuited Apr 16 '11 at 5:08
    
@intuited then use indirection; I'll edit my answer. –  pepoluan Apr 16 '11 at 12:16
    
Ah, yes, good. Thanks for adding that. –  intuited Apr 16 '11 at 16:21
add comment

I don't quite know why you don't just use $1 $2, etc.. but.. This may suit your needs.

$ script "ed    it" "cat/dog"  33.2  \D  

  echo "-------- Either use 'indirect reference'"
  for ((i=1;i<=${#@};i++)) ;do
    #  eval echo \"\$$i\" ..works, but as *pepoluan* 
    #    has pointed out: echo "${!i}" ..is better.
    echo "${!i}"
  done
  echo "-------- OR use an array"
  array=("$@")
  for ((i=0;i<${#array[@]};i++)) ;do
    echo "${array[$i]}" 
  done
  echo "-------- OR use 'set'"
  set  "$@"
  echo "$1"
  echo "$2"
  echo "$3"
  echo "$4"

output

  -------- Either use 'indirect reference'
  ed    it
  cat/dog
  33.2
  D
  -------- OR use an array
  ed    it
  cat/dog
  33.2
  D
  -------- OR use 'set'
  ed    it
  cat/dog
  33.2
  D

set works of anything which follows it, to create $1, $2.. etc.. This will of course override the original values, so just be aware of that.

share|improve this answer
    
ahh... so by 'eval' you meant indirect reference... ${!var} construct is safer, like what I wrote in my answer –  pepoluan Apr 17 '11 at 8:51
    
@pepoluan ... Thanks for alerting me to that. It's much simpler to write... (I've just now gone back to the webpage I referred to, If I had read further, I would have seen it mentioned it there too :( .... –  Peter.O Apr 19 '11 at 10:27
    
heh. but if the indirection happens on the left side, eval is a necessary evil, tho' :) –  pepoluan Apr 19 '11 at 11:45
    
@peopluan...okay, thanks for pointing that out... and just as an aside: I don't understand why eval is considered, by some, to be evil ... (maybe it's because of the spelling :) ... If eval is "bad", then is ${!var} equally "bad"? ... To me it is just part of the language, and a useful part, at that.. but I definitely prefer ${!var} ... –  Peter.O Apr 27 '11 at 2:03
add comment

So here is my answer. Note I support spaces in filenames.

function SplitFilePath {
    IFS=$'/' eval "${1}"=\( \${2} \)
}
function JoinFilePath {
    IFS=$'/' eval echo -n \"\${*}\"
    [ $# -eq 1 -a "${1}" = "" ] && echo -n "/"
}
function path_common {
    set -- "${@//\/\///}"       ## Replace all '//' with '/'
    local -a Path1
    local -i Cnt=0
    SplitFilePath Path1 "${1}"
    IFS=$'/' eval set -- \${2} 
    for CName in "${Path1[@]}" ; do
        [ "${CName}" != "${1}" ] && break;
        shift && (( Cnt++ ))
    done
    JoinFilePath "${Path1[@]:0:${Cnt}}"
}

I added a test case for filenames with spaces and fixed 2 tests that were missing a leading /

    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      ## Changed from 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          ## Changed from x
  do_test "$($function_to_test "/fo d/fo" "/fo d/foo"; echo x)" = "/fo dx"

  if [ $failed -ne 0 ]; then echo $failed failures; return 1; fi
}

Hope it helps

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.