24小时热门版块排行榜    

CyRhmU.jpeg
查看: 299  |  回复: 0

probao

铁虫 (初入文坛)

[交流] 2016-01-24总结

位置参数
位置参数指的是Shell脚本的命令行参数,同时也表示在Shell函数内的函数参数。

echo first arg is $1
echo first arg is ${10}

example
test.sh
#! /bin/bash


if [ -n "$1" ]
        then
        echo "parameter #1 is $1"
        fi
if [ -z "$1" ]
        then
        echo "error"
        fi

$ test.sh '1'
$ parameter #1 is 1
$ test.sh
$ error


$#        参数总数
$*        将所有命令行参数视为单个字符串。等同于"$1 $2 $3..."
$@        将所有命令行参数视为单独的个体。等同于"$1" "$2" "$3" ...
example
test.sh
#! /bin/bash


if [ -n "$1" ]
        then
        echo "parameter #1 is $1"
        echo "the number of parameter is $#"
        echo "parameters are $*"
        fi
if [ -z "$1" ]
        then
        echo "error"
        fi

$ test.sh '1' '2'
$ parameter #1 is 1
$ the number of parameter is 2
$ parameters are 1 2
回复此楼
哈哈
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 probao 的主题更新
普通表情 高级回复(可上传附件)
信息提示
请填处理意见