[Ilugc] What is wrong in this script ?

  • From: rajasuperman@xxxxxxxxx (Raja Subramanian)
  • Date: Fri, 29 Jul 2011 13:16:48 +0530

On Thu, Jul 28, 2011 at 8:34 PM, Mukesh Yadav <mak.gnu at gmail.com> wrote:

I'm trying to compress js file via shell script here is code

#!/bin/sh
for file in `find . -name "*.js"`
do
echo "Compressing $file ?"
java -jar ~/yuicompressor-2.4.6/build/yuicompressor-2.4.6.jar --type js -o
min/$file $file
done

This shell script has other bad practices which should be
avoided:

1. Don't run programs like find in back ticks and capture output.
    If the output from find is large, then shell will end up using a
    lot of RAM or run out of RAM and break.  Back ticks is also
    a security risk.

2. Always protect your scripts from file names with spaces.  In
    your case a filename with a space character will break your script.


Best option is to use the -exec flag in find and invoke commands
directly.  See the man pages.
Eg.  find . -name \*.js -exec gzip '{}' ';'

If there are too many matches, then use xargs.
Change all dirs to 755 permissions like this:
Eg. find . -type d -print0 | xargs -0 chmod 755

- Raja

Other related posts: