Automation

Best Practice

Python

It seems like most automation scripts are in Python.

  1. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值