Reputation: 425
How can I check if a string in bash is from this format: exerciseNumber{N}Published
while {N} is some natural number.
for example:
for file in `ls`; do
if (( `isInRequiredFormat ${file}` )); then
echo Yes.
fi
done
And I ask for how can I write the function "isInRequiredFormat" according to the format the I writed.
Upvotes: 0
Views: 58
Reputation: 531808
Just use (extended) pattern matching:
shopt -s extglob
for file in *; do
if [[ $file = exerciseNumber@(0|[1-9]*([0-9]))Published ]]; then
echo yes
fi
done
@(x|y)
matches either x
or y
. *(x)
matches 0 or more occurrences of x
. So @(0|[1-9]*([0-9]))
matches either 0, or it matches a non-zero digit optionally followed by any number of digits.
Upvotes: 3
Reputation: 11110
you can use a regular expression.
for file in `ls`; do
if [[ $file =~ exerciseNumber[:digit:]*Published ]]; then
echo Yes.
fi
done
Upvotes: 0