for i in $@; do
if [ -z ${i##*.c} ]; then
echo "YES: $i"
fi
done
$ ./test.sh .c .c-and-more before.c-and-after foo.h foo.c barc foo.C
YES: .c
YES: foo.c
$
Объяснение (спасибо jpaugh ):
- Перебрать аргументы командной строки:
for i in $@; do
- Главный трюк здесь:
if [ -z ${i##*.c} ]; then
. Здесь мы проверяем, равна ли длина строки ${i##*.c}
нулю. ${i##*.c}
означает: принять значение $ i и удалить подстроку по шаблону "* .c". Если результат - пустая строка, то у нас есть суффикс ".c".
Здесь, если какая-то дополнительная информация от man bash, раздел Расширение параметров
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pat‐
tern just as in pathname expansion. If the pattern matches the begin‐
ning of the value of parameter, then the result of the expansion is the
expanded value of parameter with the shortest matching pattern (the
``#'' case) or the longest matching pattern (the ``##'' case) deleted.
If parameter is @ or *, the pattern removal operation is applied to
each positional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or *, the
pattern removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.