zoukankan      html  css  js  c++  java
  • shell脚本结构

    echo $? 代表上一次命令的状态返回值,‘0’则代表为真《执行成功》,‘非零’则代表为假《执行失败》。

    shell脚本: <判断老男孩的年纪>

    [root@bogon mnt]# vim oldboy_age.sh
    #!/bin/bash
    oldboy_age=34
    
    read -p 'his age is :' age
    
    if [ $age -eq $oldboy_age ];then
        echo 'I always believe you'
    elif [ $age -gt $oldboy_age ];then
        echo 'try smaller again'
    else
        echo 'try bigger again'
    fi
    ~      
    [root@bogon mnt]# chmod +x userdel.sh
    [root@bogon mnt]# ./oldboy_age.sh 
    his age is :12
    try bigger again

    Part 循环结构

    while 循环 

    while (条件)                       

    do
    动作
    done

    需要无限循环时我们会选择while :

    : 永远为真,作为条件可无限循环

    《while循环来判断上面老男孩的年纪》

    [root@bogon mnt]# vim oldboy_age.sh
    #!/bin/bash
    oldboy_age=34
    while :
    do
    read -p 'his age is :' age

        if [ -z $age ];then   #如果输入为空,则继续输入.
           continue
        fi

    if [ $age -eq $oldboy_age ];then
        echo 'I always believe you'
        break
    elif [ $age -gt $oldboy_age ];then
        echo 'try smaller again'
    else
        echo 'try bigger again'
    fi
    done
    ~                                                                               
    [root@bogon mnt]# ./oldboy_age.sh 
    his age is :34
    I always believe you

    FOR循环  -------- 《批量创建系统用户》

    [root@bogon mnt]# vim useradd.sh
    #!/bin/bash
    for i in {1..10}
    do
        useradd user$i
    done
    [root@bogon mnt]# chmod +x useradd.sh
    [root@bogon mnt]# ./useradd.sh

    --------------------《批量删除用户》

    [root@bogon mnt]# vim userdel.sh
    
    !/bin/bash
    for i in {1..10}
    do
        userdel user$i
    done
    
    [root@bogon mnt]# chmod +x userdel.sh
    [root@bogon mnt]# chmod +x userdel.sh
    [root@bogon mnt]# ./userdel.sh

    练习题:

    (1) 测试那些IP能ping通网络

    [root@bogon mnt]# ./ping.sh
    #!/bin/bash
    for i in {1..50};
    do
        ping -c1 192.168.61.$i &> /dev/null    #将ping的过程记录到该文件夹下,不显示在桌面
        if [ $? -eq 0 ];then                  #判断上次命令是否ping成功,$?表示返回上次的命令值
            echo "192.168.61.$i successful"
            echo "192.168.61.$i" >> /tmp/a.txt  #将ping通的IP记录到/tmp/a.txt下
        fi
    done
    [root@bogon mnt]# chmod +x ping.sh
    [root@bogon mnt]# ./ping.sh

    (2)  查看特定目录下的文件类型《目录文件/链接文件/普通文件有多少?》

    [root@bogon mnt]# vim file.sh
     if
            [ -z file_name ];then
            continue
        else
            break
        fi
    done
    for i in $(ls $file_name)
    do
     if [ -h $file_name/$i ];then
            ((link_file+=1))
        elif [ -f $file_name/$i ];then
            ((regular_file+=1))
        elif [ -d $file_name/$i ];then
            ((directory_file+=1))
        fi
    done
    echo "lianjiewenjian: $link_file"
    echo "putongwenjian:  $regular_file"
    echo "directorywenjian: $directory_file"
    [root@bogon mnt]# chmod +x file.sh
    [root@bogon mnt]# ./file.sh

    简单的for循环:

    [root@bogon mnt]# vim for.sh
    
    #!/bin/bash
    for ((i=1;i<=9;i++)) 
    do
        echo $i
    done
    [root@bogon mnt]# ./for.sh

    用户简单登录的测试

    [root@bogon mnt]# vim mingling.sh
    
    #!/bin/bash
    username='jason'
    password='123456'
    tag='true'
    while $tag
    do
        read -p 'username:' name
        read -p 'password:' passwd
        if [[ $name = $username ]] && [[ $passwd = $password ]];then
            echo 'login sucessful'
            while $tag
                do
                read -p '>>> :' cmd
              if
                  [[ $cmd = 'quit' ]];then
                tag=false
              else
                  $cmd
              fi
            done
    fi
    done
         
    [root@bogon mnt]# chmod +x  mingling.sh
    [root@bogon mnt]# ./mingling.sh
            

     九九乘法表

    [root@bogon mnt]# vim chengfa.sh
    #!/bin/bash
    for ((i=1;i<=9;i++))
    do
           for  ((j=1;j<=i;j++))
           do
           echo -n "$i*$j=$[j*i] "
           done
        echo 
    done
    
    [root@bogon mnt]# chmod +x chengfa.sh                                                                         
    [root@bogon mnt]# ./chengfa.sh
  • 相关阅读:
    Aoj 418 ACM 排名
    HDU 3308 LCIS
    HDU 1540 Tunnel Warfare
    HDU 4417 Super Mario
    HDU 1754 I hate it
    HDU 1166 敌兵布阵
    Codeforces 1257D Yet Another Monster Killing Problem
    Codeforces 1257D Yet Another Monster Killing Problem
    CCF CSP 201709-4 通信网络
    CCF CSP 201709-4 通信网络
  • 原文地址:https://www.cnblogs.com/junxiansheng/p/6946698.html
Copyright © 2011-2022 走看看