Linux shell 脚本,循环解析命令行传入的所有参数,并按照不同的传参实现对不同的 java jar文件 进行测试执行。
[root@localhost demo]# cat connTest.sh
#!/bin/bash
# Linux shell for qftool java jar test
# modes
DEFAULT_MODE=2
jarfiles[1]=common-1.0-SNAPSHOT-shaded-with-dependencies.jar
jarfiles[2]=dbconnector-1.0-SNAPSHOT-shaded-with-dependencies.jar
jarfiles[3]=dbfreader-1.0-SNAPSHOT-shaded-with-dependencies.jar
jarfiles[4]=sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar
jarfiles[0]=${jarfiles[$DEFAULT_MODE]}
maxMode=${#jarfiles[@]}
#echo "default jarfile: ${jarfiles[0]}"
#echo "maxMode: $maxMode"
classMains=([1]=com.xxx.qftools.common.Main [2]=com.xxx.qftools.dbconnector.Main [3]=com.xxx.qftools.dbfreader.Main [4]=com.xxx.qftools.sm4.Main)
classMains[0]=${classMains[$DEFAULT_MODE]}
#echo "default classMain: ${classMains[@]}"
h_idx=-1
m_idx=-1
f_idx=-1
c_idx=-1
while getopts "hm:f:c:" optname
do
case "$optname" in
"h")
echo -e "***********************************************************************\n"
echo -e "Use $0 [ -h | [ -m mode [ -f jarfile -c classMain ] ] ] \n"
echo -e " -h: show help"
echo -e " -m: mode. -m 0 must used with -f and -c args. Supported modes:"
echo -e " 0: use input jarfile(-f ...) and input classMain(-c ...)"
echo -e " 1: use -f common-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.common.Main"
echo -e " 2: use -f dbconnector-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.dbconnector.Main"
echo -e " 3: use -f dbfreader-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.dbfreader.Main"
echo -e " 4: use -f sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.sm4.Main"
echo -e " -f: jarfile, the filepath of jar file. used with -m 0"
echo -e " -c: classMain, the main class. used with -m 0"
echo -e "\n egs: "
echo -e " $0 # for default test. The effect is equivalent to exec: $0 -m 3"
echo -e " $0 -m 0 -f ./hello.jar -c com.xxx.qftools.hello.Main \n"
echo -e " $0 -m 1 # for common test"
echo -e " $0 -m 2 # for dbconnector test (default mode)"
echo -e " $0 -m 3 # for dbfreader test"
echo -e " $0 -m 4 # for sm4 test"
echo -e "***********************************************************************\n"
h_idx=$OPTIND
;;
"m")
#echo "m_idx: $OPTIND, m_val: $OPTARG"
m_idx=$OPTIND
m_val=$OPTARG
;;
"f")
echo "f_idx: $OPTIND, f_val: $OPTARG"
f_idx=$OPTIND
f_val=$OPTARG
;;
"c")
echo "c_idx: $OPTIND, c_val: $OPTARG"
c_idx=$OPTIND
c_val=$OPTARG
;;
"*")
echo "Unsupport opt: current.idx: $OPTIND"
;;
esac
done
## -jar : jar build 打包时如果已经指定了 ... 则直接使用 -jar,无需再指定 mainClass,使用jar中定义的mainClass
# java -Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar -Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar -jar dbconnector-1.0-SNAPSHOT-shaded-with-dependencies.jar
## -cp : jar build 打包时如果未指定 ... 则使用 -cp,执行的时候要指定 mainClass
# java -Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar -Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar -cp dbconnector-1.0-SNAPSHOT-shaded-with-dependencies.jar $classMain
if [ $# -eq 0 ]; then
# with no args, use default mode
file=${jarfiles[$DEFAULT_MODE]}
class=${classMains[$DEFAULT_MODE]}
elif [ $h_idx -ge 0 ]; then
# return success for "-h" show help
echo "for help"
exit 0
elif [ $m_idx -ge 0 ]; then
#if [ $m_val -eq 0 ]&&[ $f_idx -ge 1 ]&&[ -n "$f_val" ]&&[ $c_idx -gt 1 ]&&[ -n "$c_val" ]; then
if [ $m_val -eq 0 ]&&[ $f_idx -ge 0 ]&&[ -n "$f_val" ]&&[ $c_idx -ge 0 ]&&[ -n "$c_val" ]; then
# -m 0 -f xxx -c xxx
jarfiles[0]=$f_val
classMains[0]=$c_val
file=${jarfiles[0]}
class=${classMains[0]}
echo "input is: -m 0 -f $file -c $class"
elif [ $m_val -ge 1 ]&&[ $m_val -le 4 ]; then
# -m 1 or -m 2 or -m3 or -m 4
file=${jarfiles[$m_val]}
class=${classMains[$m_val]}
echo "input is: -m $m_val"
else
echo "m_idx: $m_idx, f_idx: $f_idx, c_idx: $c_idx; f_val: $f_val, c_val: $c_val"
echo "ERROR: input invalid! mode invalid, not match [0, 4]. For help: $0 -h"
exit 0
fi
else
echo "ERROR: input invalid! For help: $0 -h"
exit 0
fi
args=-Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar
args="$args":-Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar
java $args -cp $file $class
echo -e "\nFinished test for: \nargs: $args \nfile: $file \nclass: $class \n"
echo -e "\n------------------------------ END ------------------------------------\n"
执行效果:
[root@localhost demo]# ./connTest.sh -h
[root@localhost demo]# ./connTest.sh
[main:31] === dbconnector in ===
[main:33] GBase8a conn test:
[connTest4GBase8a:48] ====================SSL01
Ssl_cipher-----AES256-SHA
Ssl_cipher_list-----ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM
Ssl_verify_depth-----18446744073709551615
Ssl_verify_mode-----5
Ssl_version-----TLSv1
Finished test for:
args: -Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar:-Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar
file: dbconnector-1.0-SNAPSHOT-shaded-with-dependencies.jar
class: com.xxx.qftools.dbconnector.Main
------------------------------ END ------------------------------------
[root@localhost demo]# ./connTest.sh -m 4
input is: -m 4
[encAndDecTest:33] ====================================
[encAndDecTest:34] Padding: PKCS7Padding
[encAndDecTest:37] Timestamp: 1701116992221
[encAndDecTest:45] 秘钥 :66787063323140313231350000000000
[encAndDecTest:46] ---------------
[encAndDecTest:53] Timestamp: 1701116992226,con(ECB加密耗时ms):5
[encAndDecTest:57] Timestamp: 1701116992235,con(ECB解密耗时ms):9
[encAndDecTest:59] ----------------
[encAndDecTest:66] Timestamp: 1701116992236,con(CBC加密耗时ms):1
[encAndDecTest:67] 明文字符长度:22,原文(hex)字符长度:132,加密结果字符长度:160
[encAndDecTest:71] Timestamp: 1701116992240,con(CBC解密耗时ms):4
Finished test for:
args: -Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar:-Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar
file: sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar
class: com.xxx.qftools.sm4.Main
------------------------------ END ------------------------------------
[root@localhost demo]# ./connTest.sh -m 0
m_idx: 3, f_idx: -1, c_idx: -1; f_val: , c_val:
ERROR: input invalid! mode invalid, not match [0, 4]. For help: ./connTest.sh -h
[root@localhost demo]#
[root@localhost demo]# ./connTest.sh -m 0 -f sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.sm4.Main
f_idx: 5, f_val: sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar
c_idx: 7, c_val: com.xxx.qftools.sm4.Main
input is: -m 0 -f sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar -c com.xxx.qftools.sm4.Main
[encAndDecTest:33] ====================================
[encAndDecTest:34] Padding: PKCS7Padding
[encAndDecTest:37] Timestamp: 1701116992221
[encAndDecTest:45] 秘钥 :66787063323140313231350000000000
[encAndDecTest:46] ---------------
[encAndDecTest:53] Timestamp: 1701116992226,con(ECB加密耗时ms):5
[encAndDecTest:57] Timestamp: 1701116992235,con(ECB解密耗时ms):9
[encAndDecTest:59] ----------------
[encAndDecTest:66] Timestamp: 1701116992236,con(CBC加密耗时ms):1
[encAndDecTest:67] 明文字符长度:22,原文(hex)字符长度:132,加密结果字符长度:160
[encAndDecTest:71] Timestamp: 1701116992240,con(CBC解密耗时ms):4
Finished test for:
args: -Xbootclasspath/a:lib/gbase-connector-java-9.5.0.1-build1-bin.jar:-Xbootclasspath/a:lib/mysql-connector-java-5.1.35-bin.jar
file: sm4-1.0-SNAPSHOT-shaded-with-dependencies.jar
class: com.xxx.qftools.sm4.Main
------------------------------ END ------------------------------------