手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下Shell脚本编程

[日期:2015-07-23] 来源:Linux社区  作者:mofansheng [字体:大 中 小]

1、 if 判断一些特殊用法
if [ -z $a ]  这个表示当变量a的值为空时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
$3不存在,所以n为空;判断是否为空;
[root@yong ~]# n=`wc -l /etc/passwd|awk '{print $3}'`
[root@yong ~]# echo $n
 
[root@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,返回值为真;
[root@localhost shell]# cat 1.txt
123
sdwe
[root@localhost shell]# if grep -q "123" 1.txt ;then echo kong;fi
kong

2、 shell中的case判断
格式: 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}
1234567891011121314151617181920 case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

举例,输入的是字母,提示输入一个纯数字,输入数字判断是偶数或奇数;
[root@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

[root@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
[root@localhost 0618]# sh case1.sh 
please input a number:234
偶数
[root@localhost 0618]# sh case1.sh 
please input a number:we
please input a number

case实验2:输入为空则提示输入数字然后退出;输入的数字在0-100内判断成绩;输入为非数字则提示输入数字然后退出;
输入负数和大于100的都会提示输入的数字为0-100;
#!/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

[root@localhost 0618]# sh case2.sh 
please input a number:-200
输入的数字为0-100
[root@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目录下所有的目录
[root@yong ~]# cat for.sh 
#!/bin/bash
for f in `ls /etc/`
do
if [ -d /etc/$f ]
then
      ls -d "/etc/$f"
fi
done

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

[root@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循环实验:

如果输入为空,提示要求输入东西,如果输入字符提示输入纯数字,输入纯数字打印数字,退出;
1234567891011121314151617181920 [root@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实验:条件匹配的话退出整个循环;
[root@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

[root@localhost 0618]# sh break.sh
1
1
2
2
3
OK

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

1234567891011 #!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then       
        continue
    fi
    echo $i
done
echo OK

[root@localhost 0618]# sh continue.sh 
1
1
2
2
3
4
4
5
5
OK

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

#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then
        exit 1
    fi
    echo $i
done
echo OK

[root@localhost 0618]# sh break.sh 
1
1
2
2
3

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

#!/bin/bash
input(){
    echo $1
    }
input yonglinux
[root@yong ~]# sh 1.sh 
yonglinux

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

12345678 #!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}
sum 1 2
[root@localhost 0618]# sh 2.sh 
3

查看ip地址的函数;
#!/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"

[root@localhost 0618]# sh 2.sh 
please input the eth name:eth0
eth0 address is 192.168.11.100
[root@localhost 0618]# sh 2.sh 
please input the eth name:eth1
eth1 address is 192.168.20.100
[root@localhost 0618]# sh 2.sh 
please input the eth name:lo
lo address is 127.0.0.1

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-07/120405.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款