On Thu, May 04, 2017 at 02:42:00PM +1000, Andrew McGlashan wrote:
> That's okay, so long as there is at least one target file, otherwise it
> fails.
>
> I've added a test before now and dropped the ls in the for.

or you could just set nullglob in the script.

From the bash man page:

    nullglob   If set, bash allows patterns which match no files (see
               Pathname Expansion above) to expand to a null string,
               rather than themselves.

e.g.

$ for i in *.doesnotexist ; do echo $i ; done
*.doesnotexist

$ shopt -s nullglob

$ for i in *.doesnotexist ; do echo $i ; done
$

Note that this only affects interpretation of glob patterns, not fixed
strings.  so 'for i in 1 2 3 4 5; ...' still works as expected because there
aren't any glob/wildcard characters in it.


you can put the nullglob setting and for the loop in a different scope (e.g. a
sub-shell or a function) if you need to avoid nullglob having unwanted
side-effects on other parts of the script.


but, really, why write a 58 line shell script when a oneliner rename command
(or 2 or 3 lines when reformatted for readability) is all that's needed?

or one of the many similar utilites to do this extremely common task (most of
which are both harder to use and less functional than the perl rename tool).

craig

ps: the previously mentioned Bash Pitfalls and the Bash FAQ at the same site
are, IMO, **ESSENTIAL** reading for anyone wanting or needing to write bash
scripts. also useful for other sh dialects.

http://mywiki.wooledge.org/BashPitfalls

http://mywiki.wooledge.org/BashFAQ

I also highly recommend the Unix & Linux Stack Exchange site for anything to
do with shell, awk, sed, etc scripting.  Searching there will almost certainly
find good solutions for whatever you're trying to do and if not, you can
always ask your own question and get a good answer in short order.

https://unix.stackexchange.com/ 

--
craig sanders <[email protected]>
_______________________________________________
luv-main mailing list
[email protected]
https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main

Reply via email to