• shell脚本中的函数


          如果你学过开发,肯定知道函数的作用。如果你是刚刚接触到这个概念的话,也没有关系,其实很好理解的。函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。有时候脚本中的某段代总是重复使用,如果写成函数,每次用到时直接用函数名代替即可,这样就节省了时间还节省了空间。

    #! /bin/bash
    ## author:Xiong Xuehao
    ## Use fun in this script.
    function sum(){
      sum=$[$1 + $2]
      echo $sum
    }
    sum $1 $2

    fun.sh 中的sum() 为自定义的函数,在shell脚本中要用

    function 函数名() {

    command

    }

    这样的格式去定义函数。

    上个脚本执行过程如下:

    提醒你一下,在shell脚本中,函数一定要写在最前面,不能出现在中间或者最后,因为函数是要被调用的,如果还没有出现就被调用,肯定是会出错的。

    案例:shell脚本实现中国传统数学里面的9×9乘法口诀表

    中国传统数学里面的9*9乘法口诀表:

    #! /bin/bash
    ## author:Xiong Xuehao
    ## 9 * 9 multiplication formula table of traditional Chinese mathematics.
    function MultiplicationFormula(){
      for x in `seq 1 9`; do
        for y in `seq 1 9`; do
          if [ $y -le $x ];then
            echo -n "$y × $x = $[$x*$y] "
          else
            echo ""
            break
          fi
        done
      done
    
      echo ""
    }
    
    echo "中国传统数学里面的9*9乘法口诀表:"
    MultiplicationFormula;

    执行结果如图:

    前面及章节的训练脚本: sbin-myfirstbash.tar.gz


    【练习题】

    shell脚本的练习题,你最好不要偷懒。

    1. 编写shell脚本,计算1-100的和;

    2. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;

    3. 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;

    4. 编写shell脚本,批量建立用户user_00, user_01, … ,user_100并且所有用户同属于users组;

    5. 编写shell脚本,截取文件test.log中包含关键词’abc’的行中的第一列(假设分隔符为”:”),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;

    6. 编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255,="" 0<n3<255,="" 0<n4<255<="" span="">)。

    以下为练习题答案:

    1. #! /bin/bash

    sum=0

    for i in `seq 1 100`; do

    sum=$[$i+$sum]

    done

    echo $sum

    2. #! /bin/bash

    n=0

    while [ $n -lt "1" ]; do

    read -p "Please input a number, it must greater than "1":" n

    done

    sum=0

    for i in `seq 1 $n`; do

    sum=$[$i+$sum]

    done

    echo $sum

    3. #! /bin/bash

    for f in `ls /root/`; do

    if [ -d $f ] ; then

    cp -r $f /tmp/

    fi

    done

    4. #! /bin/bash

    groupadd users

    for i in `seq 0 9`; do

    useradd -g users user_0$i

    done

    for j in `seq 10 100`; do

    useradd -g users user_$j

    done

    5. #! /bin/bash

    awk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txt

    sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt

    awk '$1>10 {print $2}' /tmp/n2.txt

    6. #! /bin/bash

    checkip() {

    if echo $1 |egrep -q '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$' ; then

    a=`echo $1 | awk -F. '{print $1}'`

    b=`echo $1 | awk -F. '{print $2}'`

    c=`echo $1 | awk -F. '{print $3}'`

    d=`echo $1 | awk -F. '{print $4}'`

    for n in $a $b $c $d; do

    if [ $n -ge 255 ] || [ $n -le 0 ]; then

    echo "the number of the IP should less than 255 and greate than 0"

    return 2

    fi

    done

    else

    echo "The IP you input is something wrong, the format is like 192.168.100.1"

    return 1

    fi

    }

    rs=1

    while [ $rs -gt 0 ]; do

    read -p "Please input the ip:" ip

    checkip $ip

    rs=`echo $?`

    done

    echo "The IP is right!"

    【完】

  • 相关阅读:
    将文件导入到SQL server数据库表中的字段中
    查看端口是否启用
    JS去除字符串左右两端的空格
    css常见问题
    iframe之局部刷新
    iframe局部刷新的二种实现方法
    模式识别复习目录
    linux下文件内容查找 转
    LaTeX技巧10:LaTeX数学公式输入初级入门
    matlab中高维数组怎么做PCA?
  • 原文地址:https://www.cnblogs.com/xiongzaiqiren/p/15104616.html
Copyright © 2020-2023  润新知