kinght2008 发表于 2018-8-27 11:42:47

脚本入门:shell变量和条件测试

  写脚本是运维行业中最有趣的一部分(我是这么觉得),一个功能强大的脚本可以省去许多的时间(如果以后都是自动化运维,我们该干什么呢?)。脚本的用途主要有:自动化处理、执行系统管理和故障排除、一键部署管理应用程序、批量处理文本文件。
  shell是一个解释型的脚本语言,它有一定的格式规范,bashshell是主流Linux的默认shell,在shell脚本的首行一般都有shebang机制,即首行会写上使用哪一个shell解释器,当给脚本赋予执行权限并执行时,Linux将根据这里指定的shell去解释执行。当然也可以直接将脚本文件当作参数给shell去执行,这时就不必要写shebang了。
# bash test.sh  
1
  
# ./test.sh
  
1
  一、shell变量
  和其他编程语言的变量一样,变量是指向一段内存空间的字符串。shell是弱类型的语言,使用变量前不必首先声明,shell默认所有变量都是字符类型的。
  变量命名:
  良好的命名习惯可以方便团队协作,能让以后的工作人员在已有的脚本上进行扩展从而节省了运维成本
  变量名只能用字母、数字和下划线组成,数字不能作为变量名的开头
  命名采用驼峰法以免出现歧义,要做到见名知意,变量名不能使用保留字
  shell变量类型:
  按照变量的内容可以分为:字符型和数值型
  字符型:
  数值型:
  按照变量的作用范围分:

[*]  本地变量:变量只对当前shell生效
# cat test1.sh  
#!/bin/bash
  
var1="this is test1"
  
bash /testdir/bin/test2.sh
  
# cat test2.sh
  
#!/bin/bash
  
echo $var1
  
# bash test1.sh
  
#
  可以看到执行test1.sh没有任何输出

[*]  环境变量:只对当前shell及其子shell有效,对父shell无效。
  变量声明、赋值
  export name=value 或declare -x name=value,也可以声明本地变量后用export和declare -x转换为环境变量.
  引用方式和本地变量一样
  如果要显示所有环境变量可用:export、env、printenv或declare -x
  删除环境变量:unset name
# cat testenv.sh  
#!/bin/bash
  
env1="this is a env test"
  
name="this is a local variable"
  
export env1
  
./testenv2.sh
  
# cat testenv2.sh
  
#!/bin/bash
  
echo $env1
  
echo $name
  
# bash testenv.sh
  
this is a env test
  

  
#

[*]  只读变量:声明后无法修改和删除
  变量声明:readonly name或declare -r name,只读变量在退出当前shell时失效
# name="readonly"  
# readonly name
  
# echo $name
  
readonly
  
# name="hello"
  
-bash: name: readonly variable
  
# unset name
  
-bash: unset: name: cannot unset: readonly variable
  
# bash
  
# echo $name
  

  
# exit
  
exit
  只读变量和本地变量一样只能在当前shell生效,要想在子shell生效需要执行export变为环境变量

[*]  位置变量:在脚本代码中调用通过命令行传递给脚本的参数
  $1、$2.........${10}.........:在脚本中调用传递给脚本的对应参数,注意:两位数以上的数要加括号
  $0表示脚本本身,如果执行脚本时写的全路径,$0就表示全路径加脚本名
  $*传递给脚本的所有参数,如果被引号括起来则是一个字符串
  $@和$*一样,但它叫所有参数看作不同的字符串
  $#:传递给脚本的参数个数
  $$:当前进程的pid
# cat teshu.sh  
#!/bin/bash
  
./teshu1.sh "$*"
  
echo "-------------------------"
  
./teshu1.sh "$@"
  
echo "-------------------------"
  
./teshu1.sh $*
  
echo "-------------------------"
  
./teshu1.sh $@
  
echo "-------------------------"
  
# cat teshu1.sh
  
#!/bin/bash
  
echo "the first arg is $1"
  
echo "the first arg is $2"
  
echo "the first arg is $3"
  
echo "the num agrs is: $#"
  
# bash teshu.sh abc 123 hello
  
the first arg is abc 123 hello
  
the first arg is
  
the first arg is
  
the num agrs is: 1
  
-------------------------
  
the first arg is abc
  
the first arg is 123
  
the first arg is hello
  
the num agrs is: 3
  
-------------------------
  
the first arg is abc
  
the first arg is 123
  
the first arg is hello
  
the num agrs is: 3
  
-------------------------
  
the first arg is abc
  
the first arg is 123
  
the first arg is hello
  
the num agrs is: 3
  
-------------------------
  
#
  二、算数运算
  bash中的算数运算有:+、-、*、/、%、**等
  实现算数运算:
  1、let var=算数表达式
# let a=1+2  
# echo $a
  
3
  2、var=$[算数表达式]
# a=1  
# b=2
  
# c=$
  
# echo $c
  
3
  3、var=$((算术表达式))
# c=0  
# c=$((a+b))
  
# echo $c
  
3
  4、var=$(expr arg1 op arg2):算数操作符两边必须空格
# a=2  
# b=2
  
# c=`expr $a \* $b`
  
# echo $c
  
4
  
# c=`expr $a * $b`
  
expr: syntax error
  5、echo "算是表达式" |bc 能够支持小数运算
  三、聚合命令
  有两种聚集命令的方式:
  1、复合式
# date;who |wc -l  
Sat Aug 13 11:07:13 CST 2016
  
2
  
#
  以分号为分隔符,依次来执行命令,命令之间没有逻辑关系
  2、子shell式
# (sleep 20 && ls /etc)|wc -l  
270

  当执行上面的命令时,括号生成一个新的bash,wc将等待子shell的结果
  四、退出状态码
  Linux中退出状态码来判断进程是否执行成功,$?代表上一个进程的退出状态码,其中0代表成功,1-255代表失败
  自定义退出码:exit
  在脚本中有时我们通过判断了解到当前环境不适合继续执行该脚本,则可使用exit退出,默认退出码是0
  五、条件测试
  判断某需求是否满足,需要由测试机制来实现
  测试命令:
  1、test expression一般很少用
  2、[ expression ]中括号里的expression两边必须加空格,一般由于数值判断和文件判断
  3、` expression `expression两边必须加空格,一般用于字符串的判断

[*]  数值测试:
  -gt: 是否大于;
  -ge: 是否大于等于;
  -eq: 是否等于;
  -ne: 是否不等于;
  -lt: 是否小于;
  -le: 是否小于等于;
  数值测试比较大小不能用>、:ascii码是否大于ascii码
  $" &> /dev/null&& echo "this is a useful ip" || echo "this is not useful ip"
  
# bash ipcheck.sh
  
Please enter the ipaddr:10.10.10.10
  
this is a useful ip
  
# bash ipcheck.sh
  
Please enter the ipaddr:10.10.10.10.
  
this is not useful ip
  
# bash ipcheck.sh
  
Please enter the ipaddr:10.10.1.256
  
this is not useful ip
  13、计算1+2+3+...+100
# bash sumand.sh  
Please enter a int that is great then one:55
  
1540
  
# bash sumand.sh
  
Please enter a int that is great then one:100
  
5050
  
# cat sumand.sh
  
#!/bin/bash
  
read -p "Please enter a int that is great then one:" int
  
echo `seq 1 $int`|tr ' ' '+'|bc
  14、输入起始值A和最后值B,计算从A+(A+1)...+(B-1)+B的总和
# bash sumrandom.sh  
Please enter the first num:10
  
please enter the second num:15
  
75
  
# bash sumrandom.sh
  
Please enter the first num:15
  
please enter the second num:10
  
75
  
# cat sumrandom.sh
  
#!/bin/bash
  
read -p "Please enter the first num:" first
  
read -p "please enter the second num:" second
  
[ $first -gt $second ] && echo `seq $second $first`|tr ' ' '+'|bc || echo `seq $first $second`|tr ' ' '+'|bc


页: [1]
查看完整版本: 脚本入门:shell变量和条件测试