zoukankan      html  css  js  c++  java
  • 和菜鸟一起深入学习国嵌实验之脚本编程

    1、shell脚本数据输入、条件判断、大小比较

    #!/bin/bash
    #将第一个命令行参数传递给变量1,第二个命令行参数传递给变量b
    a=$1
    b=$2
     
    #判断a或者b变量是否为空,只要有一个为空就打印提示语句并退出
    if [ -z $a ] || [ -z $b ]
    then
       echo "please enter 2 numbers"
       exit 1
    fi
     
    #判断a和b的大小,并根据判断打印结果
    if [ $a -eq $b ]
       then
           echo "number a = number b"
    else if [ $a -gt $b ]
       then
           echo "number a > number b"
    elif [ $a -lt $b ] 
       then
           echo "number a < number b"
       fi
    fi

     

    运行结果如下:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test1.sh
    please enter 2 numbers
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test1.sh 3 3
    number a = number b
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test1.sh 3 5
    number a < number b
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test1.sh 7 5
    number a > number b

     

    2、shell脚本循环控制、文件属性判断


    #!/bin/bash
     
    #变量counter用于统计文件的数目
    counter=0
     
    #变量files遍历一遍当前文件夹
    for files in *
    do
           #判断的files是否为文件,如果是,counter的值就加1并赋值给自己
       if [ -f "$files" ]
       then
           counter=`expr $counter + 1`
       fi
    done
     
    #输出结果
    echo "There are $counter files in`pwd`"


    运行结果:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test2.sh
    There are 3 files in/home/eastmoon/work/guoqian/1/1.1

     

    3、shell脚本循环控制语句

     

    #!/bin/bash
     
    echo -n "Please enter number:"
    #读入输入的值放到变量n中
    read n   
     
    sd=0
    rev=""
    on=$n
     
    echo "You put number is $n"
     
    while [ $n -gt 0 ]
    do
       sd=$(($n%10))   #求余
       n=$(($n/10))      #去掉最后一位
       rev="$rev$sd"    #将当前的最后一位放在字符串之后
    done
     
    echo "$on in a reverse order$rev"

     

    运行结果如下:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test3.sh
    Please enter number:98345
    You put number is 98345
    98345 in a reverse order 54389


    4、shell脚本移动一个文件,若其存在,则监视该文件,直到文件被删除了再移动。


    #!/bin/bash
     
    #判断命令行是否带有两个文件名的参数
    if [ "$1" = "" ] || ["$2" = "" ]
    then
       echo "Please enter file name"
       exit 1
    fi
     
    #判断目标文件是否存在
    if [ -e $2 ]
    then
       echo "The file already exists"
       until [ ! -f $2 ]          #监视该文件是否被删除
       do
           sleep 1
       done
       echo "The file have been deleted"
    fi
     
    #执行源文件移动为目标文件的命令
    if [ ! `mv $1 $2` ]
    then
       echo "mv sucessful"
    else
       echo "mv error"
    fi

     

    运行结果如下:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test4.sh  a b
    The file already exists
    The file have been deleted
    mv sucessful
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.1$sh test4.sh   b c
    mv sucessful


     

  • 相关阅读:
    前端TypeScript编写的代码发布后怎么在浏览器中调试
    oracle中的执行计划
    oracle中的物化视图
    oracle中的exists 和not exists 用法
    Oracle中的索引详解
    Oracle中动态SQL拼接
    oracle 中sql优化的几种方法
    oracle中常用函数大全
    Oracle中游标的用法
    oracle中表分区的实现
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300026.html
Copyright © 2011-2022 走看看