0

as all know we can check easily if folder exists by the following bash command

[[ -d /opt/presto-server ]] && echo folder exist

but when we have folder as

/opt/presto-server-32.34

then how to use regular expression in order to validate that folder is exists

with this rule

presto-server-<any charecter>

as

presto-server-454.545
presto-server-4a-434
presto-server-aa-434-l

etc

  • you say "re" for regular expression, but you don't actually need a regex, right? Your test is "any character" -- so you don't care what those following characters are? Does there need to be a dash between presto-server and the remainder? – Jeff Schaller Apr 3 at 14:53
  • yes dash is between presto-server to any character – yael Apr 3 at 14:55
  • What about find with regexp ? stackoverflow.com/a/19840606/912046 – el-teedee Apr 3 at 14:55
  • yes But I ask with bash not by find command – yael Apr 3 at 14:56
3

I would count how many parameters resulted from the expansion of a wildcard. First, I would set nullglob so that if there are no matches, we get the expected zero instead of the wildcard itself.

shopt -s nullglob

Then ask for the expansion of the desired pattern and set them as the current parameters:

set -- /opt/presto-server-*/

Note the addition of the trailing slash -- that requires that the resulting matches are directories (or symlinks to directories).

Then ask how many there are:

[ "$#" -gt 0 ] && echo "There are one or more presto-server- directories"
2

You could define a function has_dirs like:

has_dirs() {
  for f do
    [ -d "$f" ] && return
  done
  false
}

And use it as:

if has_dirs /opt/presto-server-*; then
  echo there are matching dirs
fi

That's for directories or symlinks to directories. For directories only, you'd need:

    [ -d "$f" ] && [ ! -L "$f" ] && return

To more specifically match on prestor-server-<number>-<number>, with bash, you can do:

shopt -s extglob # to enable a subset of ksh extended globs
has_dirs /opt/presto-server-+([[:digit:]])-+([[:digit:]])

Though beware that it could give a false positive it there was a directory called literally /opt/presto-server-+([[:digit:]])-+([[:digit:]]) (the failglob or nullglob options can work around that).

1

If switching to zsh is an option:

if ()(($# > 0)) /opt/presto-server-*(N/); then
  echo there are directories matching that pattern
fi

Where

  • () compound-command args is an anonymous function with arguments
  • ((...)): compound command that evaluates the ... arithmetic expression and returns success if it results in a number other than 0.
  • $# > 0: arithmetic expression that evaluates to 1 if $# (the number of arguments to that anonymous function) is greater than 0.
  • (N/): glob qualifiers: N to turn on nullglob for that one glob, / to only select the files of type directory (change to -/ to also include symlinks to directories).

That's not terribly efficient as that computes the whole list of directories (for which zsh needs to do a lstat() on each matching file) which are later sorted, while finding just one directory would have been sufficient.

You can change * to <->-<-> to specifically match a sequence of numbers separated by -.

Of course, within bash, you can always do:

if zsh -c '()(($# > 0)) /opt/presto-server-<->-<->(N/)'; then
  echo there are directories matching that pattern
fi
  • what in case we need to run it in bash not in zsh? – yael Apr 3 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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