Can I use Python as a Bash replacement? -


i textfile manipulation through bunch of badly remembered awk, sed, bash , tiny bit of perl.

i've seen mentioned few places python kind of thing, know little , know more. python choice this, , there book or guide learning how use python replace shell scripting, awk, sed , friends?

any shell has several sets of features.

  • the essential linux/unix commands. of these available through subprocess library. isn't best first choice doing all external commands. @ shutil commands separate linux commands, implement directly in python scripts. huge batch of linux commands in os library; can these more in python.

    and -- bonus! -- more quickly. each separate linux command in shell (with few exceptions) forks subprocess. using python shutil , os modules, don't fork subprocess.

  • the shell environment features. includes stuff sets command's environment (current directory , environment variables , what-not). can manage python directly.

  • the shell programming features. process status code checking, various logic commands (if, while, for, etc.) test command , of it's relatives. function definition stuff. much, easier in python. 1 of huge victories in getting rid of bash , doing in python.

  • interaction features. includes command history , what-not. don't need writing shell scripts. human interaction, , not script-writing.

  • the shell file management features. includes redirection , pipelines. trickier. of can done subprocess. things easy in shell unpleasant in python. stuff (a | b; c ) | >result. runs 2 processes in parallel (with output of a input b), followed third process. output sequence run in parallel something , output collected file named result. that's complex express in other language.

specific programs (awk, sed, grep, etc.) can rewritten python modules. don't go overboard. replace need , evolve "grep" module. don't start out writing python module replaces "grep".

the best thing can in steps.

  1. replace awk , perl python. leave else alone.
  2. look @ replacing grep python. can bit more complex, version of grep can tailored processing needs.
  3. look @ replacing find python loops use os.walk. big win because don't spawn many processes.
  4. look @ replacing common shell logic (loops, decisions, etc.) python scripts.

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -