how to improve simple python shell script (to compile list of files)

Chris F.A. Johnson cfajohnson at gmail.com
Sat Oct 15 14:08:12 EDT 2005


On 2005-10-15, Jari Aalto wrote:
>
> [Keep CC, thank you]
>
> Please suggest comments how can I make this script to work 
> from bash. Also how can I skip better the [0] argument from
> command line without hte extra variable i?
>
>     #!/bin/bash
>
>     function compile ()
>     {
>         python -c '
>         import os, sys, py_compile;
>         i = 0;
>         for arg in sys.argv:
>             file  = os.path.basename(arg);
>             dir   = os.path.dirname(arg);
>             i += 1;
>             if i > 1  and os.path.exists(dir):
>                 os.chdir(dir);
>                 print "compiling %s\n" % (file);
>                 py_compile.compile(file);
>         '  $*
>     }

   Don't indent:

function compile ()
{
         python -c '
import os, sys, py_compile;
i = 0;
for arg in sys.argv:
      file  = os.path.basename(arg);
      dir   = os.path.dirname(arg);
      i += 1;
      if i > 1  and os.path.exists(dir):
          os.chdir(dir);
          print "compiling %s\n" % (file);
          py_compile.compile(file);
  '  $*
}

>     compile $(find path/to -type f -name "*.py")
>
>     # End of example
>
> The error message reads:
>
>       File "<string>", line 2
>         import os, sys, py_compile;
>         ^
>     SyntaxError: invalid syntax
>
> Jari
>


-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>



More information about the Python-list mailing list