shell编程 shell是一种脚本语言 可以使用逻辑判断、循环等语法 可以自定义函数 shell是系统命令的集合 shell脚本可以实现自动化运维,能大大增加我们的运维效率
开头需要加#!/bin/bash 以#开头的行作为解释说明 脚本的名字以.sh结尾,用于区分这是一个shell脚本 执行方法有两种 chmod x 1.sh; ./1.sh bash 1.sh 查看脚本执行过程 bash -x 1.sh 查看脚本是否语法错误 bash -n 1.sh
date时间命令 date %Y-%m-%d=date %F 年月日 date %y-%m-%d 年月日 date %H:%M:%S = date %T 时间 date %s 时间戳 date -d @1504620492 date -d " 1day" 一天后 date -d "-1 day" 一天前 date -d "-1 month" 一月前 date -d "-1 min" 一分钟前 date %w 周几 date %W 今年的第几周
shell脚本中的变量 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi 引用某个命令的结果时,用变量替代 n=wc -l 1.txt 写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数 数学运算a=1;b=2; c=$(($a $b))或者$[$a $b]
shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi a=5 if [ $a -gt 3 ] ;then echo ok ; fi 格式2:if 条件; then 语句; else 语句; fi a=5 if [ $a -gt 3 ] ; then echo ok ; else not ok ; fi 格式3:if …; then … ;elif …; then …; else …; fi a=5 if [ $a -gt 1] ; then echo ok ;elif [ $a -lt 6 ] ; else nook ; fi 逻辑判断表达式: if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); 大于 -lt(<); 小于 -ge(>=); 大于等于 -le(<=); 小于等于 -eq(==); 等于 -ne(!=) 不等于 注意到处都是空格 可以使用 && || 结合多个条件 if [ $a -gt 5 ] && [ $a -lt 10 ]; then 且 if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或
文件目录属性判断 [ -f file ]判断是否是普通文件,且存在 f="/tmp/ceshi" if [ -f $f ] ; then echo $f exist ; else touch $f ; fi [ -d file ] 判断是否是目录,且存在 f="/tmp/ceshi" if [ -d $f ] ; then echo $f exist ; else touch $f ; fi [ -e file ] 判断文件或目录是否存在 f="/tmp/ceshi" if [ -e $f ] ; then echo $f exist ; else touch $f ; fi [ -r file ] 判断文件是否可读 f=''/tmp/ceshi'' if [ -r $f ] ; then echo $f is read; fi [ -w file ] 判断文件是否可写 f=''/tmp/ceshi'' if [ -w $f ] ; then echo $f is write; fi [ -x file ] 判断文件是否可执行 f=''/tmp/ceshi'' if [ -x $f ] ; then echo $f is exe fi
[ -f $f ] && rm -rf $f f=''/tmp/ceshi'' if [ -f $f ] ; then rm -rf $f ; fi [ ! -f $f ] || touch $f f=''/tmp/ceshi'' if [ ! -f $f ] ; then touch $f ;fi
if判断的一些特殊用法 if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样 if [ -n "$a" ] 表示当变量a的值不为空 if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样 if [ ! -e file ]; then 表示文件不存在时会怎么样 if (($a<1)); then … 等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
shell中case判断 格式case变量名 in value1) command ;; value2) command ;; *) command ;; 在case中可以在条件中使用|,表示或的意思,比如 2|3) command ;; read -p ""
shell脚本案例 #!/bin/bash read -p "Please input a number: " n if [ -z "$n" ] then echo "Please input a number." exit 1 fi
n1=echo $n|sed 's/[0-9]//g' //排查非数字情况 if [ -n "$n1" ] then echo "Please input a number." exit 1 fi
if [ $n -lt 60 ] && [ $n -ge 0 ] then tag=1 elif [ $n -ge 60 ] && [ $n -lt 80 ] then tag=2 elif [ $n -ge 80 ] && [ $n -lt 90 ] then tag=3 elif [ $n -ge 90 ] && [ $n -le 100 ] then tag=4 else tag=0 fi case $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The number range is 0-100." ;; esac
for循环 语法 for 变量名 in 条件;do ………… ;done
经典案例1 #!/bin/bash sum=0 for i in seq 1 100 do sum=$[$sum $i] echo $i done
echo $sum
文件列表循环 #!/bin/bash cd /etc/ for a in ls /etc/ do if [ -d $a ] then ls -d $a fi done 来源:https://www./content-3-394001.html
|