美文网首页
note_9.1.2_shell脚本参数传递

note_9.1.2_shell脚本参数传递

作者: 人間失格_430b | 来源:发表于2019-02-17 14:50 被阅读0次

    向脚本传递参数:

      位置参数变量

      xxxx.sh argu1 argu2
        引用方式:
        $1, $2, ..., ${10}, ${11}, ...

      轮替:
        shift [n]:位置参数轮替

    练习:写一脚本,通过命令传递两个文本文件路径给脚本,计算其空白行数之和;

    [root@localhost ~]# bash /scripts/add2file.sh /etc/fstab /etc/rc.d/init.d/functions 
    92
    [root@localhost ~]# cat /scripts/add2file.sh
    #!/bin/bash
    #description:input 2 file path and output the number of the sum of blanks in 2 files
    blank1=$(grep "^[[:space:]]*$" $1 | wc -l)
    shift
    blank2=$(grep "^[[:space:]]*$" $1 | wc -l)
    echo $[$blank1+$blank2]
    

    特殊变量:

    $0:脚本文件路径本身
    $#:脚本参数的个数
    $*:所有参数单独输出
    $@:所有参数输出一个字符串

    过程式编程语言的代码执行顺序:

      - 顺序执行:逐条运行;
      - 选择执行:
        代码有一个分支:条件满足时才会执行;
        两个或以上的分支:只会执行其中一个满足条件的分支;
      - 循环执行:
        代码片断(循环体)要执行0、1或多个来回;

    • 选择执行:
        单分支的if语句:
            if  测试条件              if 测试条件; then
            then              or         代码
                代码分支              fi
            fi
    

      双分支的if语句:

                    if  测试条件; then
                        条件为真时执行的分支
                    else
                        条件为假时执行的分支
                    fi
    

    示例:通过参数传递一个用户名给脚本,此用户不存时,则添加之;

            #!/bin/bash
            #ver1.1
            if ! grep "^$1\>" /etc/passwd &> /dev/null; then
                useradd $1
                echo $1 | passwd --stdin $1 &> /dev/null
                echo "Add user $1 finished."
            fi  
            
            #!/bin/bash
            #ver1.2
            if [ $# -lt 1 ]; then
                echo "At least one username."
                exit 2
            fi
    
            if ! grep "^$1\>" /etc/passwd &> /dev/null; then
                useradd $1
                echo $1 | passwd --stdin $1 &> /dev/null
                echo "Add user $1 finished."
            fi      
                
            #!/bin/bash
            #ver1.3
            if [ $# -lt 1 ]; then
                echo "At least one username."
                exit 2
            fi
    
            if grep "^$1\>" /etc/passwd &> /dev/null; then
                echo "User $1 exists."
            else
                useradd $1
                echo $1 | passwd --stdin $1 &> /dev/null
                echo "Add user $1 finished."
            fi
    

    练习

    1.通过命令行参数给定两个数字,输出其中较大的数值;
    2.通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数;
    3.通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;
    都存在时返回每个文件的行数,并说明其中行数较多的文件;

    A1
    [root@localhost ~]# bash /scripts/compare.sh 10 2
    the great number is :
    10
    [root@localhost ~]# bash /scripts/compare.sh 1 2
    the great number is :
    2
    [root@localhost ~]# cat /scripts/compare.sh
    #!/bin/bash
    echo "the great number is :"
    if [ $1 -gt $2 ];then
        echo $1
    else 
        echo $2
    fi
    
    A2
    [root@niroo ~]# cat /etc/passwd
    ...
    gentoo:x:4003:4003::/users/gentoo:/bin/bash
    fedora:x:4004:4004::/users/fedora:/bin/bash
    ...
    
    [root@localhost ~]# bash /scripts/exer9_2_2.sh gentoo
    gentoo\'s id is odd number
    [root@localhost ~]# bash /scripts/exer9_2_2.sh fedora
    fedora\'s id is even number
    [root@localhost ~]# cat /scripts/exer9_2_2.sh
    #!/bin/bash
    usrId=$(id -u $1)
    if [ $[$usrId % 2 ] -eq 0 ];then
        echo "$1\'s id is even number"
    else
        echo "$1\'s id is odd number"
    fi
    
    A3
    [root@niroo ~]# bash /scripts/exer9_2_3.sh /etc/passwd /etc/fstab
    23 12
    passwd has much more lines
    [root@niroo ~]# cat /scripts/exer9_2_3.sh
    #!/bin/bash
    if [ -e $1 -a -e $2 ];then
        lnum1=$(wc -l $1|cut -d" " -f1)
        lnum2=$(wc -l $2|cut -d" " -f1)
        echo $lnum1 $lnum2
        
    else
        exit 1
    fi
    
    if [ ${lnum1} -gt ${lnum2} ];then
        echo "`basename $1` has much more lines"
    else
        echo "`basename $2` has much more lines"
    fi
    

    相关文章

      网友评论

          本文标题:note_9.1.2_shell脚本参数传递

          本文链接:https://www.haomeiwen.com/subject/ndjgeqtx.html