I will regularly collect some good shell and PY scripts to see here, for reference learning:
Shell resources:
Https://github.com/lannyMa/ops_doc/tree/master/LazyManage
Check Help
yum install man-pages-zh-CN -yecho 'LANG="zh_CN.UTF-8"' >> ~/.bashrcecho 'LANGUAGE="zh_CN:zh"' >> ~/.bashrcsource ~/.bashrc
Shell Auto-complete
yum install bash-com*
View shell Options
man sh
Script error exiting
set -ue 遇到错误退出 -x debug模式
Generating sequences
{2..10}seq 10for ((i=0,i<10;i++));do donefor i in `ls /`;do echo ${i}_$(date +%F)done
Determine if a variable exists
man sh-z 如果变量值长度为0,则为真-d-f-w 可写-x 可执行 --启动脚本是否可执行-e 是否存在.--配置文件是否存在
Mathematical operations
$[$a+$b]
Monitor network card Traffic (number of packets/bytes)
#!/usr/bin/env bash#/sys/class/net/eth0/statistics/rx_packets: 收到的数据包数据#/sys/class/net/eth0/statistics/tx_packets: 传输的数据包数量#/sys/class/net/eth0/statistics/rx_bytes: 接收的字节数#/sys/class/net/eth0/statistics/tx_bytes: 传输的字节数#/sys/class/net/eth0/statistics/rx_dropped: 当收到包数据包下降的数据量#/sys/class/net/eth0/statistics/tx_dropped: 传输包数据包下降的数据量function get_pkgs(){ rx_packets=$(cat /sys/class/net/eth0/statistics/rx_packets) tx_packets=$(cat /sys/class/net/eth0/statistics/tx_packets) rx_bytes=$(cat /sys/class/net/eth0/statistics/rx_bytes) tx_bytes=$(cat /sys/class/net/eth0/statistics/tx_bytes) rx_kb=$[$rx_bytes/1024/1024] tx_kb=$[$tx_bytes/1024/1024]}function print(){ get_pkgs echo -n "数据包/收: "; echo -n "数据包/发: "; echo -n "数据包/发-字节: "${rx_kb} Mb; echo -n "数据包/收-字节: "${tx_kb} Mb; sleep 1}while :;do print echo sleep 1done
Determine the number of parameters
function help(){ echo "usage: xxx this is a test "}# 参数总数#if [ $# -eq 0 ];then# help#elif [ $1 == "22" ];then# echo "22 happy"#else# echo "default..."#fi# 参数是否存在if [ ! -z "$1" ];then echo $1else echo "very sad"fi
Determine if execution parameters and directories exist
Reference: https://github.com/jenkinsci/docker/blob/master/plugins.sh
set -eecho "WARN: plugins.sh is deprecated, please switch to install-plugins.sh"if [ -z "$1" ]then echo "USAGE: Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed in the reference directory, so user can define a derived Docker image with just : FROM jenkins COPY plugins.txt /plugins.txt RUN /usr/local/bin/plugins.sh /plugins.txt Note: Plugins already installed are skipped" exit 1else JENKINS_INPUT_JOB_LIST=$1 if [ ! -f "$JENKINS_INPUT_JOB_LIST" ] then echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST" exit 1 fifi
[Svc]shell Script