Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh survive glob failure

I have checked out a project, the project contains a .gitignore file.

The contents of this file are like so

vx1% head .gitignore 
./deps
/*.exe
/*.la
/.auto
.libs
ChangeLog
Makefile
Makefile.in
aclocal.m4
autom4te.cache

I want to

  • read the file line by line
  • for each line read, list the actual files that are being ignored
  • finally I want to tweak the script to delete those files

The reason for wanting to do this - is that I don't trust the project Makefile to fully clean up it's generated files.

Notes

As you can see, the .gitignore uses some globs that I need to modify before running the commands, otherwise the glob will resolve to my root directory.

What I already know

To dynamically evaluate an arbitrary string as a glob pattern

DYN="*.c"

print $~DYN

To strip the leading /, if it exists

DYN="/*.c"

print ${~DYN/#//}

What I've got

cat .gitignore | while read i; do echo $i ; print ${~i/#//} ; done

The problem

The first glob failure that this loop encounters, it terminates with error

zsh: no matches found: *.exe

What I want

The 'script' should keep going through each line of the file, trying each line in turn.

I answered this myself, answer is below

like image 464
bryan hunt Avatar asked Sep 21 '13 10:09

bryan hunt


1 Answers

Found the answer on the zsh mailing list, in typical Zsh fashion - it's very simple when you know how, and impossible otherwise.

print *.nosuchextension(N)

The (N) glob parameter prevents raising an error on match failure.

like image 86
bryan hunt Avatar answered Oct 13 '22 09:10

bryan hunt