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?
Reading a directory in shell script
shell script;filenames;wildcards;sort
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 2fiWith 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=\$$#}