zoukankan      html  css  js  c++  java
  • 二、Shell变量二之位置参数变量变量

    在Shell中存在一些特殊且重要的变量,例如:01、$#,我们称之为特殊位置参数变量。要从命令行、函数或脚本执行等处传递参数时,就需要在Shell脚本中使用位置参数变量。

    $0:
    获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包含脚本的路径。
    
    $n:
    获取当前执行的shell脚本的第n个参数值,n=1..9,当n=0时表示脚本的文件名;如果n>9,则用大括号括起来,如${11} ${12},2个参数中间使用空格隔开。
    
    $#
    获取当前执行的shell脚本后面接的参数的总个数。
    
    $*
    获取当前shell脚本所有传参的参数,不加引号和$@相同;如果给$*加上双引号,如:"$*",则表示将所有的参数视为单个字符串,相当于"$1 $2 $3"
    
    $@
    获取当前shell脚本所有传参的参数,不加引号和$*相同;如果给$@加上双引号,如:"$@",则表示将所有的参数视为不同的独立字符串,相当于"$1" "$2" "$3" "..."。这是将多参数传递给其它程序的最佳方式,因为它会保留所有的内嵌的每个参数丽的任何空白。当"$@""$*"都加双引号时,两者是有区别的;都不加双引号时,两者无区别。

    案例:

    $1$2…​$9​${10}${11}..特殊变量实践

    [root@node1 tmp]# cat /tmp/test.sh 
    #! /bin/bash
    echo $1
    
    [root@node1 tmp]# bash /tmp/test.sh ywx1 ywx2
    ywx1
    #只显示$1的值
    [root@node1 tmp]# bash /tmp/test.sh "ywx1 ywx2"
    ywx1 ywx2
    #把ywx1和ywx2用双引号括起来,则表示一个参数
    [root@node1 tmp]# cat test2.sh 
    #! /bin/bash
    echo $1
    echo $2
    
    [root@node1 tmp]# bash /tmp/test2.sh ywx1 ywx2 ywx3
    ywx1
    ywx2
    #在显示$1 $2的值
    
    [root@node1 tmp]# bash /tmp/test2.sh ywx  "king seal" kaka
    ywx
    king seal
    #"king seal"被双引号引起作为$2

    设置15个位置参数(15),用于接收命令行传递的15个参数。

    [root@node1 tmp]# cat test3.sh 
    #! /bin/bash
    echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
    
    [root@node1 tmp]# echo {a..z}
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    
    [root@node1 tmp]# bash /tmp/test3.sh {a..z}
    a b c d e f g h i a0 a1 a2 a3 a4 a5
    #在$9之后显示错误了
    
    #更改test3.sh脚本
    [root@node1 tmp]# cat test3.sh
    #! /bin/bash
    echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
    
    [root@node1 tmp]# bash /tmp/test3.sh {a..z}
    a b c d e f g h i j k l m n o
    #加上大括号则正常

    $0特殊变量的作用及变量实践

    [root@node1 tmp]# cat test.sh 
    #! /bin/bash
    echo $0
    
    [root@node1 tmp]# bash /tmp/test.sh ywx1 ywx2
    /tmp/test.sh
    #显示文件名

    dirname及basename命令自身的功能和用法

    dirname取脚本的路径
    [root@node1 tmp]# dirname /tmp/test.sh
    /tmp
    
    basename取脚本的名字
    [root@node1 tmp]# basename /tmp/test.sh 
    test.sh

    $#特殊变量获取脚本传参个数的实践

    [root@node1 tmp]# cat /tmp/test.sh 
    #! /bin/bash
    echo $#
    
    [root@node1 tmp]# sh /tmp/test.sh ywx1 ywx2 ywx3
    3

    $*和$@特殊变量功能及区别说明

    利用set设置位置参数(同命令行脚本的传参)。

    [root@node1 tmp]# set -- "I am" handsome ywx #<==通过set设置三个字符串参数,“——”表示清除所有的参数变量,重新设置后面的参数变量。
    [root@node1 tmp]# echo $#
    3
    [root@node1 tmp]# echo $1
    I am
    [root@node1 tmp]# echo $2
    handsome
    [root@node1 tmp]# echo $3
    ywx
    #$*和$@不加引号
    [root@node1 tmp]# echo $*
    I am handsome ywx
    [root@node1 tmp]# echo $@
    I am handsome ywx
    #$*和$@不加引号,使用for循环打印参数
    [root@node1 tmp]# for i in $*;do echo $i;done 
    I
    am
    handsome
    ywx
    [root@node1 tmp]# for i in $@;do echo $i;done 
    I
    am
    handsome
    ywx
    #不加双引号,会输出所有的内容,第一个参数$1="I am"会被拆开
    
    #$*和$@加引号
    [root@node1 tmp]# echo "$*"
    I am handsome ywx
    [root@node1 tmp]# echo "$@"
    I am handsome ywx
    #$*和$@加引号,使用for循环打印参数
    [root@node1 tmp]# for i in "$*"; do echo $i ;done
    I am handsome ywx #"$*" 输出的参数"$1 $2 $3..."
    [root@node1 tmp]# for i in "$@"; do echo $i ;done
    I am
    handsome
    ywx #"$@" 输出的参数是"$1" "$2" "$3" ...

    shift向右偏移n个变量

    [root@node1 tmp]# cat test.sh 
    #! /bin/bash
    echo $1
    shift 1
    echo $1
    shift 2
    echo $1
    
    [root@node1 tmp]# sh /tmp/test.sh ywx1 ywx2 ywx3 ywx4
    ywx1
    ywx2 #在ywx1 ywx2 ywx3 ywx4上向右偏移1个,跳过ywx1,偏移后的$1=ywx2
    ywx4 #在ywx2 ywx3 ywx4上向右偏移2个,跳过ywx2 ywx3,偏移后的$1=ywx4
    I have a dream so I study hard!!!
  • 相关阅读:
    Java小细节
    LinkedHashMap的accessOrder的作用
    异或运算及其应用
    什么是FullStack设计
    Java正则表达式收藏
    offsetLeft,Left,clientLeft的区别
    java文件常用操作(2) 从文件末尾开始读取文件
    java文件常用操作
    各种排序方法
    2013微软暑期实习笔试&面试总结
  • 原文地址:https://www.cnblogs.com/yaokaka/p/13766414.html
Copyright © 2011-2022 走看看