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.

I want to read a numbered directory which is under a path, into a variable in shell script. The paths are something like this:

.../releases/R1/...
.../releases/R2/...
.../releases/R3/...

Each time that the script is executed I want to read the latest numbered R (R3 in this case) directory into a variable and then pass this as an argument to some functions inside the script. How shall I do that?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

It's easiest if you're using zsh, thanks to its glob qualifiers: [-1] to retain the last match and n to use numeric sorting so that R9 comes before R10. You can also take advantage of its <-> glob pattern to match numbers.

last_release=(releases/R<->(n[-1]))
if (($#last_release == 0)); then
  echo 1>&2 "Fatal error: no release available"
  exit 2
fi

With only portable tools, the easiest method is to offload the numeric sorting to sort. If you only have “tame” file names in the releases directory (ASCII characters only, no control characters, no whitespace), filtering the output of ls is a handy way of generating the list of file names.

set_last_release () {
  set -- $(ls releases | sed -n 's/^R\([0-9][0-9]*\)//p' | sort -rn)
  if [ $# -eq 0 ]; then
    echo 1>&2 "Fatal error: no release available"
    exit 2
  fi
  last_release=releases/R$1
}

If you use leading zeroes in your release numbers, you can rely on the shell's built-in sorting. Note that there is no portable way to match only file names consisting of R followed by one or more digit; in ksh, you can use releases/R+([0-9]), and you can also use this in bash if you put shopt -s extglob at the beginning of your script (it must be in effect both when the function is parsed and when it is executed).

set_last_release () {
  set -- releases/R[0-9]*
  if [ "$1" = 'releases/R[0-9]*' ]; then
    echo 1>&2 "Fatal error: no release available"
    exit 2
  fi
  eval last_release=\$$#
}
share|improve this answer
add comment

how about that:

DIRECTORY=$(ls .../releases | tail -n 1)

This works as long as your directory names can be sorted like strings. for example a '2' would be after a '12', unless you name the '2' actually '02'

share|improve this answer
    
Otherwise, If your directories can't be sorted as strings and you can't change their names, you may also do this ` for i in {1..1000}; do if [ ! -d .../releases/R${i} ]; then break; fi; done; let i=${i}-1` in the end ${i} is the number of the latest directory –  mauro.stettler Feb 22 '13 at 11:03
add comment

Simply try this :

for dir in releases/R*[0-9]/; do
   echo "$dir"
done
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.