for i in $(ls $INPUT_DIR | egrep -i '^'$INPUT_FILE_PREFIX'[0-9][0-9]([0][1-9]|1[0-2])([0][1-9]|[12][0-9]|[3][01])'$INPUT_FILE_SUFFIX);
In general, there's no reason to use ls
in a structure like this, it just makes the command more awkward to read, plus you run into issues with some corner cases (see ParsingLs in BashGuide). However, the regex you have can not be represented as a standard shell glob, so there's some point into using it. Though since this was tagged with bash, we can do it in the shell, either with extglob
(or using regex matches with the [[ .. ]]
construct after a wider glob).
shopt -s extglob
for i in "$INPUT_DIR/$INPUT_FILE_PREFIX"[0-9][0-9]@(0[1-9]|1[0-2])@(0[1-9]|[12][0-9]|3[01])"$INPUT_FILE_SUFFIX" ; do
If you don't really need such a strict pattern, you could just use [0-9][0-9][0-9][0-9][0-9][0-9]
instead.
In the assignment to MYDATE
, I assume you just want to remove the prefix and the suffix. (though if your prefix/suffix contains a six-digit string, the grep would match that, too.)
MYDATE=${i#"$INPUT_DIR/"} # remove the directory
MYDATE=${MYDATE#"$INPUT_FILE_PREFIX"} # remove the prefix
MYDATE=${MYDATE%"$INPUT_FILE_SUFFIX"} # and the suffix
In full:
shopt -s extglob
for i in "$INPUT_DIR/$INPUT_FILE_PREFIX"[0-9][0-9]@(0[1-9]|1[0-2])@(0[1-9]|[12][0-9]|3[01])"$INPUT_FILE_SUFFIX" ; do
MYDATE=${i#"$INPUT_DIR/"} # remove the directory
MYDATE=${MYDATE#"$INPUT_FILE_PREFIX"} # remove the prefix
MYDATE=${MYDATE%"$INPUT_FILE_SUFFIX"} # and the suffix
echo "$MYDATE"
done