设为首页 收藏本站
查看: 366|回复: 0

[经验分享] linux下shell脚本编程2

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-24 09:11:45 | 显示全部楼层 |阅读模式
1、 if 判断一些特殊用法
if [ -z $a ]  这个表示当变量a的值为空时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
$3不存在,所以n为空;判断是否为空;
1
2
3
4
5
[iyunv@yong ~]# n=`wc -l /etc/passwd|awk '{print $3}'`
[iyunv@yong ~]# echo $n

[iyunv@yong ~]# if [ -z $n ];then echo "\$n is null";fi
$n is null



if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样?

1.txt中含有123;grep -q '123' 1.txt 匹配OK,返回值为真;
1
2
3
4
5
[iyunv@localhost shell]# cat 1.txt
123
sdwe
[iyunv@localhost shell]# if grep -q "123" 1.txt ;then echo kong;fi
kong



2、 shell中的case判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
格式: case  变量名 in
                     value1)
                          command
                          ;;
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac
在case程序中,可以在条件中使用 |,表示或的意思, 比如
2|3)
    command
    ;;
当变量为2或者3时,执行该部分命令。



/etc/init.d/naginx 里面有case语句;匹配输入的第一个参数是否为start stop reload restart conifgtest,输入其他字符则返回

Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac




举例,输入的是字母,提示输入一个纯数字,输入数字判断是偶数或奇数;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[iyunv@localhost 0618]# cat case1.sh
#!/bin/bash
#要求输入的是数字,判断奇数或偶数;非数字则提示输入数字,然后退出;
read -p "please input a number:" n
n1=`echo $n|sed 's/[0-9]//g'`
#输入为数字则sed替换为空,返回值为空;输入为字母则返回值不为空;
if [ ! -z $n1 ]
then   
    echo "please input a number "   
exit 1
fi

n2=$[$n%2]
case $n2 in
   0)
    echo "偶数"
    ;;   
   1)        
    echo "奇数"        
    ;;   
   *)        
    echo "不存在"        
    ;;
esac



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@localhost 0618]# sh -x case1.sh
+ read -p 'please input a number:' n
please input a number:
de2
++ echo de2
++ sed 's/[0-9]//g'
+ n1=de
+ '[' '!' -z de ']'
+ echo 'please input a number '
please input a number
+ exit 1
[iyunv@localhost 0618]# sh case1.sh
please input a number:234
偶数
[iyunv@localhost 0618]# sh case1.sh
please input a number:we
please input a number



case实验2:输入为空则提示输入数字然后退出;输入的数字在0-100内判断成绩;输入为非数字则提示输入数字然后退出;
输入负数和大于100的都会提示输入的数字为0-100;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash#case实验,输入为空提示输入数字然后退出;输入的数字在0-100内判断成绩;输入的非数字提示输入数字然后退出;
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'`
#sed替换加了-表示负数;
if [ ! -z $n1 ]
then
    echo "please input a number "
    exit 1
fi

if [ $n -ge 0 ] && [ $n -lt 60 ]
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 "不及格"
        ;;   
    2)        
        echo "及格"      
        ;;         
    3)        
       echo "良好"        
       ;;   
    4)        
       echo "优秀"        
       ;;   
    *)        
       echo "输入的数字为0-100"        
       ;;
esac



1
2
3
4
5
6
[iyunv@localhost 0618]# sh case2.sh
please input a number:-200
输入的数字为0-100
[iyunv@localhost 0618]# sh case2.sh
please input a number:101
输入的数字为0-100



3、 shell脚本中的循环
for循环 语法结构:for  变量名 in 条件; do … done
while 循环语法结构: while 条件; do … done 死循环用:表示
break直接结束本层循环; continue忽略continue之下的代码,直接进行下一次循环
exit 直接退出shell

for循环实验:列出/etc目录下所有的目录
1
2
3
4
5
6
7
8
9
[iyunv@yong ~]# cat for.sh
#!/bin/bash
for f in `ls /etc/`
do
if [ -d /etc/$f ]
then
       ls -d "/etc/$f"
fi
done



while循环实验:判断负载的循环;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@yong ~]# cat load.sh
#!/bin/bash
#监测负载的脚本,取w负载1分钟内的负载值如果大于10,则30秒发一次邮件;
while :
do
load=`w |head -1 |awk -F "load average: " '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
    top|mail -s "load is high:$load" xxx@163.com
else
    exit 0
fi
    sleep 30
done



while循环实验:

如果输入为空,提示要求输入东西,如果输入字符提示输入纯数字,输入纯数字打印数字,退出;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@localhost 0618]# cat while.sh
#!/bin/bash
#输入为空,提示输入东西一直到输入不为空结束;如输入的是字母则提示只能输入一个纯数字,直到输入纯数字为止,打印数字结束;
while :
do
    read -p "please input a number:" n
    if [ -z $n ]
    then
        echo "请输入一个东西"
    continue
fi
    n1=`echo $n | sed 's/[-0-9]//g'`
    if [ ! -z $n1 ]
    then
        echo "请输入一个纯数字"
    continue
fi
    break
done
echo $n



continue 退出本次循环;循环内部继续,不执行循环后面的了;
break跳出整个循环,循环后面的还会执行。
exit的话退出整个脚本;

break实验:条件匹配的话退出整个循环;
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost 0618]# cat break.sh
#!/bin/bash
#break实验;条件匹配的话退出整个循环;循环后面的还会执行;
for i in `seq 1 5`
do
    echo $i   
    if [ $i == 3 ]
then        
        break   
fi
echo $i
done
echo OK



1
2
3
4
5
6
7
[iyunv@localhost 0618]# sh break.sh
1
1
2
2
3
OK



continue实验;条件匹配的话退出本次循环,继续执行循环;

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then        
        continue
    fi
    echo $i
done
echo OK



1
2
3
4
5
6
7
8
9
10
11
[iyunv@localhost 0618]# sh continue.sh
1
1
2
2
3
4
4
5
5
OK



exit实验;条件匹配的话退出整个脚本;

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then
        exit 1
    fi
    echo $i
done
echo OK



1
2
3
4
5
6
[iyunv@localhost 0618]# sh break.sh
1
1
2
2
3



4、shell中的函数
函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。
格式: function f_name() {
                      command
             }
函数必须要放在最前面
函数里可以export 全局变量;
函数实验1:定义input函数,输入一个字符,则打印一个字符;

1
2
3
4
5
6
7
#!/bin/bash
input(){
    echo $1
    }
input yonglinux
[iyunv@yong ~]# sh 1.sh
yonglinux



函数实验2:定义sum函数,进行求和运算;

1
2
3
4
5
6
7
8
#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}
sum 1 2
[iyunv@localhost 0618]# sh 2.sh
3




查看ip地址的函数;
1
2
3
4
5
6
7
8
#!/bin/bash
#查看ip地址的函数;输入一个网卡名,输出网卡对应的ip地址;$1为交互时输入的网卡名;
ip() {
    ifconfig |grep -A1 "$1"|tail -1|awk '{print $2}'|awk -F ':' '{print $2}'
}
read -p "please input the eth name:" e
myip=`ip $e`
echo "$e address is $myip"



1
2
3
4
5
6
7
8
9
[iyunv@localhost 0618]# sh 2.sh
please input the eth name:eth0
eth0 address is 192.168.11.100
[iyunv@localhost 0618]# sh 2.sh
please input the eth name:eth1
eth1 address is 192.168.20.100
[iyunv@localhost 0618]# sh 2.sh
please input the eth name:lo
lo address is 127.0.0.1






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-80081-1-1.html 上篇帖子: linux下shell脚本编程1 下篇帖子: 使用root账户登录ubuntu 15.04 linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表