Best Practice
Python
It seems like most automation scripts are in Python.
- Standard
For example, for parameters passed through command line, we use argparse.
If scripts will be ran in some Automation Server like Jenkins, third-party libraries may be hard to include.
-
parser.parse_args() return a Namespace.
? Namespace
a dictionary that maps a name to an object
? Why Namepace
in case name conflicts -
String substitution
? There are many was to implement string substitution. Which way should I select?
If the string contains '$'s, for example, bash script, Template is tricky. -
File content substitution
? What is sed
a stream editor
? What is a stream editor
Perform basic text transformations on an input stream(a file or input from a pipeline)
? How to use sed
sed SCRIPT INPUTFILE…
? e.g. To replace all occurrences of ‘hello’ to ‘world’ in the file input.txt and write output to output.txt
sed ‘s/hello/world/g’ input.txt > output.txt
? How to filter contents of the standard input
don’t specify INPUTFILE, or set INPUTFILE as -
? Where is output
– writes output to standard output : by default
– edit files in-place : use -i, e.g. sed -i ‘s/helllo/world/g’ file.txt. in MAC OS, the -i flag expects a mandatory argument. So sed -i ‘’ ‘s/helllo/world/g’ file.txt if you don’t need any extension.
– writes output to other files: use s///w, here output means lines substituted only, rather than the whole input content
– writes substituted input to other files: sed ‘’ ‘s/helllo/world/g’ > otherfile.txt
? Why is sed good
sed treats multiple input files as one long stream
? What is s in sed, g in sed?
s means substitute
g means global, all matching occurrences in the line would be replaced