shell2

对于简单的需求,shell比python好,语法简单

一 什么是shell script
将OS命令堆积到可执行的文件里,由上至下的顺序执行文本里的OS命令 就是脚本了.
  再加上些智能(条件/流控)控制,就变成了智能化脚本了.

系统变量
set和env的区别
set:显示所有变量
env:环境变量
[[email protected] ~]# xxxxxx=1
[[email protected] ~]# set |grep xxxx
xxxxxx=1
[[email protected] ~]#

要取消abd的赋值,就用unset,因为set保存了。

[[email protected] ~]# abd=value

[[email protected] ~]# echo $abd

value

[[email protected] ~]# unset abd

[[email protected] ~]# echo $abd

 

[[email protected] ~]#

 

常用系统变量
PATH
PWD
LANG
HOME
HISTSIZE
PS1
IFS
域分隔符 是空格,换行,TAB键的合集
part6 全局变量与局部变量
[[email protected] ~]# HISTSIZE=2
[[email protected] ~]# history
172 HISTSIZE=2
173 history
[[email protected] ~]#

Profile上面就有HISTSIZE=1000
[[email protected] ~]# vim /etc/profile
HISTSIZE=1000

 

\u是用户名,\h是hostname, \W是所在目录

[[email protected] ~]# echo $PS1

[\[email protected]\h \W]\$

[[email protected] ~]#

 

母函数和子函数,母函数能用到子函数上,但是子函数(局部变量)不会用到母函数上

[[email protected] ~]# export money=10000

[[email protected] ~]# echo $money

10000

[[email protected] ~]# bash

[[email protected] ~]# echo $money

10000

[[email protected] ~]# export mon=200

[[email protected] ~]# exit

exit

[[email protected] ~]# echo $mon

 

[[email protected] ~]#

 

取第3行第2个

[[email protected] ~]# free

              total        used        free      shared  buff/cache   available

Mem:         999696      261084      387380        7400      351232      555268

Swap:       2097148           0     2097148

[[email protected] ~]# free |awk 'NR==3{print $2}'

2097148

[[email protected] ~]#

 

运算符号:

[[email protected] ~]# ((2<10))

[[email protected] ~]# echo $?

0

[[email protected] ~]#

 

如果想用[],要查man test 

       STRING1 != STRING2

              the strings are not equal

 

       INTEGER1 -eq INTEGER2

              INTEGER1 is equal to INTEGER2

-eq表示==

-lt表示小于

-gt 表示大于

-a表示&&

-o表示||

-ge表示大于等于

-le表示小于等于

-ne表示不等于

 

例子:

[[email protected] ~]# [ 2 -eq 10 ]

[[email protected] ~]# echo $?

1

[[email protected] ~]# [ 2 -eq 2 ]

[[email protected] ~]# echo $?

0

[[email protected] ~]#

 

2小于等于10

[[email protected] ~]# [ 2 -le 10 ]

[[email protected] ~]# echo $?

0

[[email protected] ~]#

 

赋值:

[[email protected] ~]# x=7

[[email protected] ~]# y=$x

[[email protected] ~]# echo $x

7

[[email protected] ~]# echo $y

7

[[email protected] ~]#

 

数学运算要在[]内

[[email protected] ~]# x=2

[[email protected] ~]# x=$[$x+1]

[[email protected] ~]# echo $x

3

[[email protected] ~]#

 

最好用下面的方法:直观

[[email protected] ~]# echo $x

4

[[email protected] ~]# ((x=x+1))

[[email protected] ~]# echo $x

5

[[email protected] ~]#

这样也行:

[[email protected] ~]# x=1

[[email protected] ~]# ((x+=1))

[[email protected] ~]# echo $x

2

[[email protected] ~]#

如果自增1的话,可以用++

[[email protected] ~]# i=0

[[email protected] ~]# ((i++))

[[email protected] ~]# echo $i

1

[[email protected] ~]#

自减1,--

 

[[email protected] ~]# x=1

[[email protected] ~]# echo $((x+=1))

2

[[email protected] ~]#

 

 

Shell的所有计算器

$[]

(())

expr

[[email protected] ~]# expr 1 + 2

3

[[email protected] ~]#

[[email protected] ~]# x=1

[[email protected] ~]# y=2

[[email protected] ~]# expr $x + $y

3

[[email protected] ~]#

[[email protected] ~]# a=`expr $x + $y`

[[email protected] ~]# echo $a

3

[[email protected] ~]#

 

上面都只取整数

[[email protected] ~]# expr 99 / 134

0

[[email protected] ~]#

 

要是除数结果有小数点,就要安装BC软件包

[[email protected] ~]# yum install bc -y

 

保留两位小数

[[email protected] ~]# echo 'scale=2;30/1000'|bc -l

.03

[[email protected] ~]#

 

找使用率

[[email protected] ~]# free

              total        used        free      shared  buff/cache   available

Mem:         999696      306920      214848        7384      477928      487236

Swap:       2097148           0     2097148

 

计算小数要用双引号

[[email protected] ~]# total=`free | awk 'NR==2{print $2}'`

[[email protected] ~]# echo $total

999696

[[email protected] ~]# use=`free | awk 'NR==2{print $3}'`

[[email protected] ~]# echo $use

307648

[[email protected] ~]# echo "scale=2;$use/$total"|bc -l

.30

[[email protected] ~]#

 shell2

 

如果要输出百分比,就要取以.做分割cut -d.,取第2个,再加%

[[email protected] ~]# echo "scale=2;$use/$total"|bc -l|cut -d. -f2

30

[[email protected] ~]# b=`echo "scale=2;$use/$total"|bc -l|cut -d. -f2`

[[email protected] ~]# echo $b%

30%

[[email protected] ~]# echo ${b}%

30%

[[email protected] ~]#

 

part5-1、测试文件状态
-d 测试目录是否存在
[[email protected] ~]# test -d /etc
[[email protected] ~]# echo $?
0
[[email protected] ~]# [ -d /etc ]
[[email protected] ~]# echo $?
0
[[email protected] ~]#

-e文件
[[email protected] ~]# [ -e /root/a.txt ]
[[email protected] ~]# echo $?
0
[[email protected] ~]#

-s 文件长度 > 0、非空(文件存在且非空)
[[email protected] ~]# [ -s /etc/passwd ]
[[email protected] ~]# echo $?
0

-f 正规文件(普通文件)
-w 可写
-r 可读
-x 可执行
[[email protected] ~]# [ -w /etc/passwd ]
[[email protected] ~]# echo $?
0
[[email protected] ~]# [ -r /etc/passwd ]
[[email protected] ~]# echo $?
0
[[email protected] ~]# [ -x /etc/passwd ]
[[email protected] ~]# echo $?
1
[[email protected] ~]# ll -d /etc/passwd
-rw-r--r--. 1 root root 2225 11月 16 23:22 /etc/passwd
当前用户是否具有可读可写可执行权限
[[email protected] ~]#

-L 符号连接
-u 文件有 suid 位设置

-h连接文件
[[email protected] ~]# ln -s /root/abg.txt /tmp/
[[email protected] ~]# ll /tmp/abg.txt
lrwxrwxrwx 1 root root 13 12月 9 22:53 /tmp/abg.txt -> /root/abg.txt
[[email protected] ~]# [ -h /tmp/abg.txt ]
[[email protected] ~]# echo $?
0
[[email protected] ~]#

 

 

part5-2、字符串测试
= 两个字符串相等
!= 两个字符串不相等
-z 空串
-n 非空串

 

[[email protected] ~]# x='hello'

[[email protected] ~]# y='hello'

[[email protected] ~]# [ -z $x ]

[[email protected] ~]# echo $?

1

[[email protected] ~]# [ -n $x ]

[[email protected] ~]# echo $?

0

[[email protected] ~]# [ $x = $y ]

[[email protected] ~]# echo $?

0

[[email protected] ~]# [ $x != $y ]

[[email protected] ~]# echo $?

1

[[email protected] ~]#

 

[[email protected] ~]# var1='abc'
[[email protected] ~]# var2='123'
[[email protected] ~]# [ $var1 == $var2 ]
[[email protected] ~]# echo $?
1

part5-3、测试数值
-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于

 

建一个脚本:写脚本判断文件是什么文件

[[email protected] ~]# mkdir /test

mkdir: 无法创建目录"/test": 文件已存在

[[email protected] ~]# cd /test

[[email protected] test]# ls

a.txt  b.txt  c.txt  d.txt  gaizhujiming.sh  root.txt  son

[[email protected] test]#

 

[[email protected] test]# vim 1.sh

[[email protected] test]# cat 1.sh

# !/bin/bash

var='/etc/passwd'

if [ -f $var  ]

    then

        echo "$var is regular file"

elif [ -b $var  ]

    then

        echo "$var is block"

elif [ -d $var  ]

    then

        echo "$var is direcotry"

elif [ -h $var  ]

    then

        echo "$var is symlink"

else

    echo "$var is unkown"

fi

[[email protected] test]#

 

[[email protected] test]# chmod +x 1.sh

[[email protected] test]# ./1.sh

/etc/passwd is regular file

[[email protected] test]#

 

修改var的赋值,得到:

[[email protected] test]# cat 1.sh

# !/bin/bash

var='/etc/passwd'

var='/dev/sda'

var='/etc'

var='sdf'

if [ -f $var  ]

    then

        echo "$var is regular file"

elif [ -b $var  ]

    then

        echo "$var is block"

elif [ -d $var  ]

    then

        echo "$var is direcotry"

elif [ -h $var  ]

    then

        echo "$var is symlink"

else

    echo "$var is unkown"

fi

[[email protected] test]#

 

 

[[email protected] test]# vim 1.sh

[[email protected] test]# ./1.sh

/dev/sda is block

[[email protected] test]# vim 1.sh

[[email protected] test]# ./1.sh

/etc is direcotry

[[email protected] test]# ./1.sh

sdf is unkown

[[email protected] test]#

 

把1.sh是全部信息和权限都复制到2.sh

[[email protected] test]# cp -a 1.sh 2.sh

[[email protected] test]#

 

如果想不打开文件,直接修改脚本就要:

[[email protected] test]# vim 1.sh

[[email protected] test]# cat 1.sh

# !/bin/bash

read -p 'please input your file paht:' var

if [ -f $var  ]

    then

        echo "$var is regular file"

elif [ -b $var  ]

    then

        echo "$var is block"

elif [ -d $var  ]

    then

        echo "$var is direcotry"

elif [ -h $var  ]

    then

        echo "$var is symlink"

else

    echo "$var is unkown"

fi

[[email protected] test]#

 

输入,就可以自动得到结果:

[[email protected] test]# ./1.sh

please input your file paht:/etc

/etc is direcotry

[[email protected] test]#

 

[[email protected] test]# ./1.sh

please input your file paht:/etc/passwd

/etc/passwd is regular file

[[email protected] test]#

如果不要./的话,就用整个目录,如:

[[email protected] test]# /test/1.sh

please input your file paht:/root

/root is direcotry

[[email protected] test]#

 

 

[[email protected] test]# vim 4.sh

脚本意思是取第1、2、3、4、5、10个数

[[email protected] test]# cat 4.sh

#!/bin/bash

 

echo $1

echo $2

echo $3

echo $4

echo $5

echo ${10}

[[email protected] test]#

[[email protected] test]# ./4.sh

-bash: ./4.sh: 权限不够

[[email protected] test]# chmod +x 4.sh

先执行看看:

[[email protected] test]# ./4.sh

 

 

 

 

 

 

[[email protected] test]#

[[email protected] test]# ./4.sh a b c e u y zz 1 2 3 4 5

a

b

c

e

u

3

[[email protected] test]#

 

如果想直接在./2.sh后面直接写var要输入的值,可以这样写:

[[email protected] test]# cat 2.sh

# !/bin/bash

echo $1

if [ -f $var  ]

    then

        echo "$var is regular file"

elif [ -b $var  ]

    then

        echo "$var is block"

elif [ -d $var  ]

    then

        echo "$var is direcotry"

elif [ -h $var  ]

    then

        echo "$var is symlink"

else

    echo "$var is unkown"

fi

[[email protected] test]#

 

[[email protected] test]# ./2.sh /etc

/etc

 is regular file

[[email protected] test]#

 

Is前面没有路径,需要加上,于是用vim 2.sh然后按esc后输入::%s /var/1/g,就可以把var变成1

 

# !/bin/bash

echo $1

if [ -f $var  ]

    then

        echo "$var is regular file"

elif [ -b $var  ]

    then

        echo "$var is block"

elif [ -d $var  ]

    then

        echo "$var is direcotry"

elif [ -h $var  ]

    then

        echo "$var is symlink"

else

    echo "$var is unkown"

fi

~                                                        

~              

 

:%s /var/1/g

[[email protected] test]# ./2.sh /etc

/etc

/etc is direcotry

[[email protected] test]#

 

放到/usr/bin,就可以在任何一个文件夹运行2.sh

[[email protected] test]# cp 2.sh /usr/bin/

[[email protected] test]# cd

[[email protected] ~]# 2.sh /etc

/etc

/etc is direcotry

[[email protected] ~]#

如果放到profile中,就是永久保存

 

如果把脚本写到终端上,就要用分号

 

[[email protected] ~]# x=1

[[email protected] ~]# if [ $x -eq 1 ];then echo 'x is 1';fi

x is 1

[[email protected] ~]#

 

 

[[email protected] test]# cat 5.sh

#test.sh

echo $0

echo $1

echo $2

echo $3

echo ${11}

echo '$$' $$

echo '$*' $*

echo '[email protected]' [email protected]

echo '$#' $#

echo '$?' $?

[[email protected] test]#

 

[[email protected] test]# ./5.sh 1 2 3 4 5 6 7 8 9 10 11

./5.sh     ($0就是./5.sh)

1

2

3

11

$$ 2236   (进程号,kill -9 2236就会杀掉,如果要睡眠10000秒,就sleep 10000)

$* 1 2 3 4 5 6 7 8 9 10 11

[email protected] 1 2 3 4 5 6 7 8 9 10 11

$# 11    (多少个参数)

$? 0      ($# 11这个执行成功没有)

[[email protected] test]#

 

查看nginx的状态:

[[email protected] test]# systemctl status nginx

开启:systemctl start nginx

 

写一个脚本,看是否正在启动nginx,如果没,就开启

红色这行的干扰项,需要去掉,用grep -v 'grep'

[[email protected] test]# ps aux | grep nginx

root        925  0.0  0.2 122808  2080 ?        Ss   12月11   0:00 nginx: master process /usr/sbin/ngin

nginx       927  0.0  0.3 125324  3524 ?        S    12月11   0:00 nginx: worker process

root       2587  0.0  0.0 112676   980 pts/0    R+   00:00   0:00 grep --color=auto nginx

[[email protected] test]# ps aux | grep nginx | grep -v 'grep'

root        925  0.0  0.2 122808  2080 ?        Ss   12月11   0:00 nginx: master process /usr/sbin/nginx

nginx       927  0.0  0.3 125324  3524 ?        S    12月11   0:00 nginx: worker process

[[email protected] test]#

 

 

 

[[email protected] test]# vim ningx_check.sh (名字不能带nginx,否则脚本的语句判断出错)

[[email protected] test]# cat ningx_check.sh

#!/bin/bash

ps aux | grep nginx | grep -v 'grep'

if [ $? -ne 0 ]

then

    systemctl start nginx

fi

[[email protected] test]# chmod +x ningx_check.sh

[[email protected] test]# systemctl stop nginx

[[email protected] test]# systemctl status nginx

[[email protected] test]# ./ningx_check.sh

[[email protected] test]# systemctl status nginx

 

[[email protected] ~]# var=10

[[email protected] ~]# awk -v x=$var '{print x}' b.txt

10

10

10

10

10

10

10

10

10

10

[[email protected] ~]#