Skip to content

Shell脚本入参

(18条消息) shell中脚本参数传递的两种方式_随风丶逆风的博客-CSDN博客_shell 传递参数

方式1

bash
#!/bin/bash  
#echo "参数1 $0"  
#echo "参数2 $1"  
git tag -d "$1" && git push origin :refs/tags/"$1"
#!/bin/bash  
#echo "参数1 $0"  
#echo "参数2 $1"  
git tag -d "$1" && git push origin :refs/tags/"$1"

$1 是第一参数 $2是第二个参数

$0 应该是这个脚本的名字

方式2

使用getopts 参考: Shell脚本中的while getopts用法小结 - 散尽浮华 - 博客园

bash
func() {  
    echo "Usage:"  
    echo "test.sh [-j S_DIR] [-m D_DIR]"  
    echo "Description:"  
    echo "S_DIR,the path of source."  
    echo "D_DIR,the path of destination."  
    exit -1  
}  
  
upload="false"  
  
while getopts 'h:j:m:u' OPT; do  
    case $OPT in  
        j) S_DIR="$OPTARG";;  
        m) D_DIR="$OPTARG";;  
        u) upload="true";;  
        h) func;;  
        ?) func;;  
    esacdone  
  
echo $S_DIR  
echo $D_DIR  
echo $upload
func() {  
    echo "Usage:"  
    echo "test.sh [-j S_DIR] [-m D_DIR]"  
    echo "Description:"  
    echo "S_DIR,the path of source."  
    echo "D_DIR,the path of destination."  
    exit -1  
}  
  
upload="false"  
  
while getopts 'h:j:m:u' OPT; do  
    case $OPT in  
        j) S_DIR="$OPTARG";;  
        m) D_DIR="$OPTARG";;  
        u) upload="true";;  
        h) func;;  
        ?) func;;  
    esacdone  
  
echo $S_DIR  
echo $D_DIR  
echo $upload