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
[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,返回值为真;

1
2
3
4
5
[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判断

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
[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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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;

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
[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目录下所有的目录

1
2
3
4
5
6
7
8
9
[root@yong ~]# cat for.sh 
#!/bin/bash
for 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
[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循环实验:

如果输入为空,提示要求输入东西,如果输入字符提示输入纯数字,输入纯数字打印数字,退出;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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实验:条件匹配的话退出整个循环;

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost 0618]# cat break.sh 
#!/bin/bash
#break实验;条件匹配的话退出整个循环;循环后面的还会执行;
for 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
[root@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 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
[root@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 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
[root@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
[root@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
[root@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
[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

本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1664557,如需转载请自行联系原作者

linux下shell脚本编程2相关推荐

  1. linux下shell脚本论文,Linux下Shell脚本编程

    1. shell脚本是什么 它是一种脚本语言,并非编程语言. 可以使用一些逻辑判断.循环等语法. 可以自定义子函数,是系统命令的集合. shell脚本可以实现自动化运维,大大增加我们的工作效率. 2. ...

  2. Linux之shell脚本编程

    Linux之shell脚本编程 编程介绍 shell 介绍 shell 脚本编程介绍 shell脚本命令 shell 编程基础知识 编程介绍 计算机编程的本质:输入.运算.输出 编译型语言: 程序在执 ...

  3. linux shell脚本攻略_(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤...

    今天为大家带来的内容是:(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤 本文内容主要介绍了Linux下shell脚本监控Tomcat的状态并实现自动启动的步骤,文章 ...

  4. Linux下Shell脚本实战之监测磁盘空间

    Linux下Shell脚本实战之监测磁盘空间 一.脚本目的及要求 二.脚本内容 三.运行脚本 一.脚本目的及要求 (1) 监控/home下每各个用户目录的占用磁盘大小 (2) 监控/var/log下前 ...

  5. Linux下shell脚本之双色球摇号脚本

    Linux下shell脚本之双色球摇号脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 1.编写脚本Lottery.sh,模拟摇号过程 2.6位数 ...

  6. Linux下shell脚本实战之批量新建用户

    Linux下shell脚本实战之批量新建用户 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 1.使用提供的user.txt用户列表 2.批量新建us ...

  7. linux下shell脚本启动其他可执行程序

    linux下shell脚本启动其他可执行程序 零.前言 一.C++代码 二.shell脚本 三.shell运行效果 零.前言 linux下的项目中经常需要使用shell脚本去启动其他程序的操作,下面是 ...

  8. Linux下shell脚本/Makefile编写

    Linux下shell脚本/Makefile编写 一.基本概念 代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). make只是一个指 ...

  9. linux系统shell脚本编程,Linux系统shell脚本编程(一)

    哈喽,大家好,我是Adam.前面我发了不少关于linux 的文章,今天也是一样,将分几篇文章系统化的讲讲shell脚本编程.废话不多说,走起走起!        首先说一下shell脚本是什么.简单来 ...

  10. Linux,shell脚本编程-图书管理系统

    Shell脚本编程-图书管理系统 准备的数据库 代码块 前期准备 进入页面 主菜单 展示图书 查找图书 添加图书 编辑图书 借书 还书 删除图书 运行 演示 主界面 展示图书 查找图书 添加图书 编辑 ...

最新文章

  1. Android热修复之 阿里开源的热补丁
  2. bzoj千题计划262:bzoj4868: [六省联考2017]期末考试
  3. php 注册回调函数,PHP回调函数
  4. Jenkins 笔记
  5. matlab求两向量夹角_高中数学《平面向量的数量积》说课稿
  6. shell实现简单的进程监控脚本
  7. java sampling_Java机器学习库ML之三Sampling(采样)
  8. 千牛怎么注销卖家店铺_新店铺怎么拥有亚马逊购物车?亚马逊小卖家如何尽快获得黄金购物车?...
  9. Button with Round Progress
  10. 自动化运维工具——ansible详解(一)
  11. CSDN 创始人蒋涛:AI 定义的开发者时代
  12. 修改 jtable 显示格式
  13. 设计模式—原型模式及其扩展(思维导图)
  14. mysql集群会备份数据吗_MySQL 集群备份2-功能分析
  15. Django 创建model的一些注意事项
  16. 如何用Primer6批量设计引物(非全cDNA引物)
  17. 第三方系统平台如何对接gooflow2.0
  18. 硬改路由器-MW310R-AR9341篇
  19. 如何快速备份微信聊天记录到电脑
  20. Verilog学习笔记4:关于5M40ZE64C4N接地的问题

热门文章

  1. jquery文档就绪的三种书写方式
  2. Python大数据处理方案
  3. matlab二重定积分_matlab求二重积分
  4. matlab int 求定积分
  5. How to create a DXL attribute using a DXL script
  6. 数学建模软件lingo的基本使用方法
  7. 牛客网最终python笔试题_牛客笔试题之Python
  8. PKM2 - PKManager 基于内容的个人知识管理工具 5M 绿色免费
  9. linux cd 命令详解,Linux命令详解之–cd命令
  10. html 时间控件 只选择年,js时间控件只显示年月