shell分割参数为数组并循环执行jar,异常时退出循环

前端之家收集整理的这篇文章主要介绍了shell分割参数为数组并循环执行jar,异常时退出循环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用shell,将逗号分隔的多个id分割成数组,并遍历数组将id作为参数传入java的jar进行处理。出现异常时中断循环退出

#!/bin/bash
echo "args:$*"
if [ "$#" != "1" ]
then
  echo "must have 1 args,but only has $# args."
  exit 1
fi
echo "set var"
#shell params
ruleId=$1
echo "ruleId="$ruleId
#split ruleId by ","
OLD_IFS="$IFS"
IFS=","
ruleIds=($ruleId)
IFS="$OLD_IFS"
len=${#ruleIds[@]}
echo "has $len ruleId"
#begin
#do same thing
echo "begin..."
echo "executeJar"
for id in ${ruleIds[@]}
  do
    javacmd="java -jar -Dfile.encoding=gbk jar_name.jar $id $dateId $latnId"
    echo $javacmd 
    $javacmd
    if [ $? -eq 0 ]
    then
      echo $?
      echo 'ok'
    else
      echo $?
      #强制退出
      exit 1;
    fi
    echo "executeJar end ruleId:$id"
  done
  echo "End executeJar all ruleIds."     

echo "END"



[root@bd129106 shell]# ./show.sh 1,2,3 args:1,3 set var ruleId=1,3 has 3 ruleId begin... executeJar java -jar -Dfile.encoding=gbk jar_name.jar 1 Error: Unable to access jarfile jar_name.jar 1

猜你在找的Bash相关文章