[RndTbl] bash expansion question

Robert Keizer robert at cluenet.org
Wed Jul 7 02:02:14 CDT 2010


Adam Thompson writes: 

> I can't find what I'm looking for in the bash(1) manpage, hoping there's an
> easy answer... 
> 
> I want to use a construct like
> 	for i in ~/path/*; do something $i && rm $i; done 
> 
> which works great as long as there are files in ~/path/.  However, when the
> directory is empty, I get:
> 	rm: cannot remove `/home/athompso/path/*': No such file or directory 
> 
> which isn't quite what I want.  The quick fix in this case is to use
> 	for i in ~/path/*; do [ -f $i ] && something $i && rm $i; done 
> 
> but once again, that strikes me as inelegant and I can't remember OR find
> the better way to do it. 
> 
> Anyone? 
> 
> Thanks,
> -Adam
 

for i in `ls /path/to/dir`; do
    do something $i;
    rm $i;
done; 

If you need the power of regular expressions I would just use grep.. 
although the way you're using it doesn't look like its necessary. IE 

for i in `ls /dev | grep tty`; do
    echo -n $i;
done; 


My last reply didn't go through ( I'm guessing due to the length. ) Hence 
the random blathering at 2AM to lengthen this one... 

All the best,
Robert Keizer


More information about the Roundtable mailing list