views:

116

answers:

2

Whenever glob pattern match fails, it stops the whole job. For instance,

$ mv *.jpg *.png folder1 && blahblah
mv: cannot stat `*.jpg': No such file or directory

*.png isn't moved to folder1 and blahblah is not run.

And the script below works only for the case when both .[A-z]* and * succeed.

#!/bin/bash
cd $1
du -sk .[A-z]* *| sort -rn | head

How do I make globbing fail gracefully, at most only displaying warnings, but never stopping the job?

+4  A: 

In Bash, shopt -s nullglob will allow a failed glob to expand to nothing with no errors.

ephemient
nullglob solves most of the problem, but not quite all; if none of the globs match, you wind up with something like `mv folder1
Gordon Davisson
A: 

then use a loop. KISS

for files in jpg png
do
  mv *.${files} /destination 2>/dev/null && do_something 
done
ghostdog74
Not only is this slower, but also it still emits `mv: cannot stat '*jpg': No such file or directory`.
ephemient
ghostdog74
as for the error "cannot stat" , its trivial to redirect stderr to /dev/null...
ghostdog74
ephemient