Shell教程(二)--参数传递

Shell 参数传递默认方式

#!/usr/bin/env bash

:<

结果如下:

huiyu@yuhuideMacBook-Pro:~/ideaProject/myCoding/shell$./study02.sh args001 args002 args003
Shell 传递参数实例!
执行的文件名:./study02.sh
第一个参数为:args001
第二个参数为:args002
第三个参数为:args003
第三个参数为:args003
传递到脚本的参数个数 :3
以一个单字符串显示所有向脚本传递的参数 :args001 args002 args003
脚本运行的当前进程ID号 :1968

Shell 参数传递(getopts)方式

#!/usr/bin/env bash

######################
#   执行语句 : ./getoptsShell.sh -n y_DB -s y_table -i y_hive_tableName -d y_partitionValue -t y_extractType
######################

#获取脚本名称
PROGRAM=`basename $0`

#退出程序
usage_andexit()
{
   echo "Usage:$PROGRAM [-n RDBname][-s sourceTableName]  [-i hive_tableName]  [-d PartitionDate] [-t extractType]"
   exit $1
}

#参数超过10个数则退出
if [[ $# -lt 10 ]];then
     usage_andexit 0
fi

#获取参数内容
while getopts 'n:s:i:d:t:' OPT; do
    case $OPT in
         n)
           RDBName=${OPTARG}
           ;;
         s)
           s_tableName=${OPTARG}
           ;;
         i)
           hive_tableName=${OPTARG}
           ;;
         d)
           partitionValue=${OPTARG}
           ;;
         t)
           extractType=${OPTARG}
           ;;
         ?)
           usage_andexit 0 #退出
    esac
done

echo "参数总数有 ==> $# 个!"
echo "输入的所有参数为 ==> $* "
echo "rdb_name:"${RDBName}
echo "s_tablename:"${s_tableName}
echo "hive_table_name:"${hive_tableName}
echo "partitionValue:"${partitionValue}
echo "extractType:"${extractType}

结果如下:

huiyu@yuhuideMacBook-Pro:~/ideaProject/myCoding/shell$./getoptsShell.sh -n y_DB -s y_table -i y_hive_tableName -d y_partitionValue -t y_extractType
参数总数有 ==> 10 个!
输入的所有参数为 ==> -n y_DB -s y_table -i y_hive_tableName -d y_partitionValue -t y_extractType
rdb_name:y_DB
s_tablename:y_table
hive_table_name:y_hive_tableName
partitionValue:y_partitionValue
extractType:y_extractType
北京小辉微信公众号

Shell教程(二)--参数传递_第1张图片

大数据资料分享请关注

你可能感兴趣的:(【工,具】Linux,shell参数传递,getopts)