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 ofa
inputb
), followed third process. output sequence run in parallelsomething
, output collected file namedresult
. 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.
- replace awk , perl python. leave else alone.
- look @ replacing grep python. can bit more complex, version of grep can tailored processing needs.
- look @ replacing find python loops use
os.walk
. big win because don't spawn many processes. - look @ replacing common shell logic (loops, decisions, etc.) python scripts.
Comments
Post a Comment