我会定期的把看到的一些好的shell和py脚本搜集在这里,供参考学习:

命令行回收站

推荐一个不相关的:trash-cli,就是命令行版的回收站,它的神奇之处在于不是简单的把文件移动到回收站,而且可以在回收站里恢复。所以它说自己是Command line interface to the freedesktop.org trashcan,我的 zshrc 里有一行:alias rt="trash"

shell资源:

常用的一些script:

检查操作系统,内核,多少bit等

https://github.com/lannyMa/ops_doc/tree/master/LazyManage

执行时候加参数 -h start等

https://github.com/johanhaleby/kubetail/blob/master/kubetail

理解linux一个方言: 2>&1

http://chuansong.me/n/1108379451851

https://github.com/spujadas/elk-docker/blob/master/elasticsearch-init

100-shell-script-examples

https://github.com/itech001/100-shell-script-examples

数组操作

数组的定义,遍历
for((i=0;i<${#arr[*]};i++));do
echo ${arr[i]}
done echo
echo '---------------' for i in ${arr[@]};do
echo $i
done 取指定数组范围:
$ a=(1 2 3 4 5)
#从下标1开始取,取3位。
$ echo ${a[@]:1:3}
2 3 4

参考: https://github.com/influxdata/influxdata-docker/blob/master/influxdb/1.4/entrypoint.sh

#!/bin/bash
set -e if [ "${1:0:1}" = '-' ]; then
set -- influxd "$@"
fi if [ "$1" = 'influxd' ]; then
/init-influxdb.sh "${@:2}"
fi exec "$@"

性能等

创建一个300M的文件,预分配空间

fallocate -l 300M bigfile

查帮助

  • 中文化sh
yum install man-pages-zh-CN -y
echo 'LANG="zh_CN.UTF-8"' >> ~/.bashrc
echo 'LANGUAGE="zh_CN:zh"' >> ~/.bashrc
source ~/.bashrc
  • shell自动补全
yum install bash-com*
  • 查看shell选项
man sh

脚本出错退出

set -ue 遇到错误退出
-x debug模式

生成序列

{2..10}
seq 10
for ((i=0,i<10;i++));do done for i in `ls /`;do
echo ${i}_$(date +%F)
done

seq分割,tr替换某个字符

{}只能搞连续的
echo {1..10} seq可以搞不连续的
$ seq -s ':' 1 10|tr ":" "-"
1-2-3-4-5-6-7-8-9-10 $ seq -s ':' 1 2 10|tr ":" "-"
1-3-5-7-9
分割 开始 步长 结束 seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST

判断变量是否存在

man sh
-z 如果变量值长度为0,则为真 -d
-f
-w 可写
-x 可执行 --启动脚本是否可执行
-e 是否存在.--配置文件是否存在

数学运算

$[$a+$b]

监控网卡流量(数据包个数/字节数)

#!/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 1
done

判断参数个数

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 $1
else
echo "very sad"
fi

判断执行参数和目录是否存在

参考: https://github.com/jenkinsci/docker/blob/master/plugins.sh

set -e

echo "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 1
else
JENKINS_INPUT_JOB_LIST=$1
if [ ! -f "$JENKINS_INPUT_JOB_LIST" ]
then
echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST"
exit 1
fi
fi

如果参数不对,输出help信息

usage="${PROGNAME} <search term> [-h] [-c] [-n] [-t] [-l] [-s] [-b] [-k] [-v] -- tail multiple Kubernetes pod logs at the same time
where:
-h, --help Show this help text
-c, --container The name of the container to tail in the pod (if multiple containers are defined in the pod).
Defaults to all containers in the pod. Can be used multiple times.
-t, --context The k8s context. ex. int1-context. Relies on ~/.kube/config for the contexts.
-l, --selector Label selector. If used the pod name is ignored.
-n, --namespace The Kubernetes namespace where the pods are located (defaults to \"default\")
-s, --since Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 10s.
-b, --line-buffered This flags indicates to use line-buffered. Defaults to false.
-e, --regex The type of name matching to use (regex|substring)
-j, --jq If your output is json - use this jq-selector to parse it.
example: --jq \".logger + \\\" \\\" + .message\"
-k, --colored-output Use colored output (pod|line|false).
pod = only color pod name, line = color entire line, false = don't use any colors.
Defaults to line.
-z, --skip-colors Comma-separated list of colors to not use in output
If you have green foreground on black, this will skip dark grey and some greens -z 2,8,10
Defaults to: 7,8
--timestamps Show timestamps for each log line
--tail Lines of recent log file to display. Defaults to -1, showing all log lines.
-v, --version Prints the kubetail version
examples:
${PROGNAME} my-pod-v1
${PROGNAME} my-pod-v1 -c my-container
${PROGNAME} my-pod-v1 -t int1-context -c my-container
${PROGNAME} '(service|consumer|thing)' -e regex
${PROGNAME} -l service=my-service
${PROGNAME} --selector service=my-service --since 10m
${PROGNAME} --tail 1" if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi

ansible备份scm的脚本

- hosts: scm-server
remote_user: root
vars:
- sfpath: "/data/backup/scm-data/*_$(date +%F -d '-1 day')_scmdata.tar.gz"
- dfpath: "/data/data/backup/scm-data/"
tasks:
- name: Package | prepare the tar data on scm-server
# 0, clean the backup dir /data/backup/scm-data
# 1, copy the /root/.scm to /data/backup/scm-data
# 3, tar today's scm data
raw:
cd /data/backup/scm-data \
&& \rm -rf *.tar.gz .scm \
&& cp -r /root/.scm /data/backup/scm-data/ \
# 192.168.x.x_2017-12-12_scmdata.tar.gz
&& tar zcf /data/backup/scm-data/`ifconfig|sed -n '2p'|awk -F':' '{print $2}'|awk '{print $1}'`_$(date +%F -d '-1 day')_scmdata.tar.gz .scm - name: SYN_PKG | syn the today's scm data to backup server
synchronize:
src: "{{ sfpath }}"
dest: "{{ dfpath }}"
mode: pull # 远程解压
# - name "this vm restore scm data to test data on app"
# unarchive:
# src: "{{ dfpath }}"
# dest: /root
# remote_src: True - hosts: scm-server
remote_user: root
tasks:
# 清理本地scm压缩包,仅保存7天的压缩包.
- name: Keeps scm-data.tar.gz of 7 days in local
shell: find /data/data/backup/scm-data/ -name "*.tar.gz" -type f -mtime +7|xargs rm -f # 完事后,发邮件.
- name: sendMail to op
mail:
host: smtp.sina.com
port: 25
username: maotai@sina.com
password: 123456
from: maotai@qq.com (maotai)
to: maotai2 <maotai2>
# cc: John Doe <j.d@example.org>, Suzie Something <sue@example.com>
# attach: /etc/fstab /etc/hosts
subject: scm_data backup successfully
body: 'System {{ ansible_hostname }}-192.168.x.x from 192.168.y.y scm-data/backup has been successfully provisioned.'

[sh]shell脚本栗子的更多相关文章

  1. [svc]entrypoint.sh shell脚本解析

    最近搞influxdb绘图,看到其dockerfile的entry.sh,无奈看的不是很懂. 于是查了下.. docker run 通过传参实现配置文件覆盖 实现启动镜像时候可指定配置文件 如果不指定 ...

  2. shell脚本学习第一课

    shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell ...

  3. shell 脚本关键字&符号

    shell概念 "shell"既是一种解释型编程语言,也是一个这种编程语言的解释器的名字 shell是解释型语言,就是解释器会一条一条的翻译每一条语句并执行,对比之下,C语言是编译 ...

  4. Shell - Shell脚本调试方法

    Shell脚本调试选项 Shell本身提供一些调试方法选项: -n,读一遍脚本中的命令但不执行,用于检查脚本中的语法错误. -v,一边执行脚本,一边将执行过的脚本命令打印到标准输出. -x,提供跟踪执 ...

  5. 第1章:认识Shell脚本

     第一章:认识Shell脚本 1.1.什么是Shell Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行. 实际上Shell是一个命令解释器 ...

  6. Shell脚本编程与文件系统修复

    导读 Linux基金会发起了LFCS认证(Linux 基金会认证系统管理员)Linux Foundation Certified Sysadmin,这是一个全新的认证体系,旨在让世界各地的人能够参与到 ...

  7. shell脚本实例一

    一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim  test.sh     ##shell脚本一般都 ...

  8. Linux就该这么学--Shell脚本条件语句(二)

    1.for条件语句 先读取多个不同的变量值,然后逐一执行同一组命令. 从列表文件中读取主机地址,逐个测试是否在线. 从ipadds.txt中读取主机地址后赋值给HLIST变量后逐个ping列表中的主机 ...

  9. Linux 执行定时任务 shell脚本

    Linux上面执行定时任务,我们可以利用crontab -e直接编辑定时任务 另外我们还可以写好shell脚本,定时去执行shell脚本,这两个方法都可以起到定时执行的作用 下面我详细说一下入如何执行 ...

随机推荐

  1. TurboLinux11system»adjtimex简介

    Adjtimex介绍 linux 系统有两个时钟:一个是由主板电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  2. Andorid之Annotation框架初使用(二)

    Fragment: @EActivity(R.layout.fragments) public class MyFragmentActivity extends FragmentActivity { ...

  3. MySQL: tinyint(1) 和 tinyint(4), char 和varchar

    1.BIT[M] 位字段类型,M表示每个值的位数,范围从1到64,如果M被忽略,默认为1 2.TINYINT[(M)] [UNSIGNED] [ZEROFILL]  M默认为4,占1个字节 很小的整数 ...

  4. datagridview 单元格类型转换【备忘】

    datagridview  在设定列类型后,其下面所有行的该列都与设定的列类型相同. 在需要改变某一行的某个单元格时,遇到了一些问题,再次进行备忘: 之前在遇到该问题时参考别人的博客解决过,但是时间久 ...

  5. iOS: 如何获取ios设备的当前IP地址

    有的时候,我们项目上线后,需要根据ip地址去统计不同地区的用户情况,此时IP地址的收取显得尤其重要,一般情况下,在用户登录时去获取用户的ip是准确的,当然实时追踪ip的变化而统计是更安全可靠的. ip ...

  6. 数学图形(2.9) Capareda曲线

    还是绕在球上的线圈 #http://www.mathcurve.com/courbes3d/capareda/capareda.shtml vertices = t = to (*PI) q = ra ...

  7. oc 第五天(内存管理)

    OC的重点: 内存管理 1 基本原理     OC的内存回收机制是和JAVA的自动回收机制是不同的,它有两种模式,或者准确的说是同 一种模式的两种不同体现,下面简单总结下. 1手动内存回收       ...

  8. django知识点回顾与补充

    一.django知识点回顾 1.Cookie操作 - 客户端本地存储的键值对 2.Session操作 - 3.URL路由 - /index -> view.函数 4.MTV 5.View中返回方 ...

  9. js和jquery判断事件流

    $('body').on('click',function(e){ var $target = $(e.target); if($target.parents('.fixNav>div').le ...

  10. Android -- 经验分享(三)

    目录 获取系统版本号 获取系统信息 获取安装路径和已安装程序列表 获取图片.应用名.包名 解决listview上Item上有按钮时item本身不能点击的问题 不让文本框输入中文 获取屏幕宽高 将Tab ...