加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

shell 二维数组实现及使用实例

(2017-08-26 16:16:06)
分类: linux操作

-------------------------------------------------------------------------

-使用实例(example.sh)----------------------------------------------------

-------------------------------------------------------------------------

#/bin/bash

set -u #你使用未初始化的变量时,让bash自动退出
set -e #任何一个语句返回非真的值,则退出bash

G_WORK_PATH=`pwd`
source $G_WORK_PATH/com_func.sh

G_TIME_FOR_PATH=`date '+20%y%m%d_%Hh'`
G_TIME=`date '+20%y%m%d_%Hh%Mm%Ss'`

G_StepINDEX=1

G_rpmbuild_PATH=~/rpmbuild
G_rpmbuild_SOURCES=$G_rpmbuild_PATH/SOURCES
G_rpmbuild_SPECT=$G_rpmbuild_PATH/SPECS
G_rpmbuild_RPMS=$G_rpmbuild_PATH/RPMS/x86_64

G_output_RPMS=$G_WORK_PATH/../rpms/
mkdir -p $G_output_RPMS

G_output_RequiresArrLen=0
G_output_RequiresArr=()

#jdk rpm包制作函数
function func_jdk_rpm_make()
{
 tmpSOURCE="jdk-8u121-linux-x64.tar.gz"
 tmpSPEC="jdk_8u121-linux-x64_forCR.spec"
 
 
 #清空rpmbuild目录:先删除,再生成
 rm -rf $G_rpmbuild_PATH
 rpmdev-setuptree
 
 #拷贝源文件和spec文件
 cp -rf $G_WORK_PATH/sources/$tmpSOURCE $G_rpmbuild_SOURCES/
 cp -rf $G_WORK_PATH/specs/$tmpSPEC $G_rpmbuild_SPECT/
 
 #执行rpmbuild命令创建rpm包
 cd $G_rpmbuild_PATH
 rpmbuild -bb  SPECS/$tmpSPEC
 
 #将结果拷贝到rpms目录下
 tmpResult=`ls $G_rpmbuild_RPMS`
 mv $G_rpmbuild_RPMS/* $G_output_RPMS
 
 echo ""
 echo "Output=$tmpResult"
 echo "" 
}

#cr_bigdataframe rpm包制作函数
function func_cr_bigdataframe_rpm_make()
{
 tmpSOURCE=""
 tmpSPEC="cr_bigdataframe.spec"
 
 #清空rpmbuild目录:先删除,再生成
 rm -rf $G_rpmbuild_PATH
 rpmdev-setuptree
 
 #拷贝源文件和spec文件
 cp -rf $G_WORK_PATH/specs/$tmpSPEC $G_rpmbuild_SPECT/
 
 #执行rpmbuild命令创建rpm包
 cd $G_rpmbuild_PATH
 rpmbuild -bb  SPECS/$tmpSPEC
 
 #将结果拷贝到rpms目录下
 tmpResult=`ls $G_rpmbuild_RPMS`
 mv $G_rpmbuild_RPMS/* $G_output_RPMS
 
 echo ""
 echo "Output=$tmpResult"
 echo ""
}

##脚本使用方法说明
function func_Usage()
{
 tmpCMDArray=`func_2D_ArrayGetCloum 1 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"`
 echo "Usage"
 echo "   sudo $1 all"
 for element in ${tmpCMDArray[*]}; do
  echo "   sudo $1 $element"
 done

}


##显示依赖
function func_ShowRequires()
{
 echo ""
 for element in ${G_output_RequiresArr[*]}; do
  echo "Requires:$element"
 done
 echo ""
}

#显示G_CmdRouterArray二维数组:func_2D_ArrayShow 2 "${G_CmdRouterArray[@]}" 
#获得所有的对应的处理函数:    func_2D_ArrayGetCloum 2 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"
#获得jdk对应的处理函数:       func_2D_ArrayCloumSelect 1 "jdk" 2 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"
G_SOURCE_CLOUM=2 
G_CmdRouterArray=(
         "jdk             func_jdk_rpm_make                 "
         "cr_bigdataframe func_cr_bigdataframe_rpm_make     "
         )
  
#处理入口  
set +u
G_ParaCMD="$1"
set -u

if [ "$G_ParaCMD" == "" ]; then 
 #显示脚本用法
 func_Usage $0
 exit
elif [ "$G_ParaCMD" == "all" ]; then 
 #制作所有的rpm
 tmpCMDArray=`func_2D_ArrayGetCloum 2 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"`
 for element in ${tmpCMDArray[*]}; do
  func_SetpInfo "Building rpm while running $element"
  $element
 done
 G_output_RequiresArr=`func_2D_ArrayGetCloum 1 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"` 
else 
 #制作制定的rpm
 G_ParaCMD2Func=`func_2D_ArrayCloumSelect 1 "$G_ParaCMD" 2 $G_SOURCE_CLOUM "${G_CmdRouterArray[@]}"`
 if [ "$G_ParaCMD2Func" != "" ]; then
  func_SetpInfo "Building rpm while running $G_ParaCMD2Func"
  $G_ParaCMD2Func
  G_output_RequiresArr[$G_output_RequiresArrLen]="$G_ParaCMD"
  G_output_RequiresArrLen=$(($G_output_RequiresArrLen+1))
 else
  func_Usage $0
  exit
 fi
fi

##显示依赖
func_ShowRequires

 

-------------------------------------------------------------------------

-实现实例(example.sh)----------------------------------------------------

-------------------------------------------------------------------------

#/bin/bash

###创建BuildRun所需的目录结构
function func_rpmdev_setuptree()
{
 tmp_rpmbuild_PATH=$1
 mkdir -p $tmp_rpmbuild_PATH
 cd $tmp_rpmbuild_PATH && mkdir -pv {BUILD,BUILDROOT,RPMS,SOURCE,SOURCES,SPECS,SRPMS}
}

######################################################################################
###二维数组2D_Array相关操作:start####################################################
######################################################################################
##G_SOURCE_CLOUM=2 
##2D_Array=(
##         "jdk             func_jdk_rpm_make"
##         "cr_bigdataframe func_cr_bigdataframe_rpm_make"
##         
##func_2D_ArrayShow 2 "${2D_Array[@]}" 
##

##
##对齐显示2D数组
##参数: $1--列数   $2--${2D_Array[@]}
##用法: func_2D_ArrayShow 2 "${2D_Array[@]}"
##
function func_2D_ArrayShow()
{
   tmp_SOURCE_CLOUM=$1
   tmp_KOWN_Para_counter=1
   tmp_loop=0
   
   ###各列最大长度清零
   cloum_index=0
   while [ "$cloum_index" -lt "$tmp_SOURCE_CLOUM" ]
   do 
      cloum_index=$(($cloum_index+1))
      each_cloum_max_char_number[$cloum_index]=0
      #echo "each_cloum_max_char_number[$cloum_index]=${each_cloum_max_char_number[$cloum_index]}"
   done
  
   ###获得每列的最大长度,为对齐做准备
   tmp_loop=0
   cloum_index=1
   for tmpvar in $*
   do
        #如果tmp_loop小于1,说明是你一个参数(列数),跳过)
  if [ "$tmp_loop" -lt "$tmp_KOWN_Para_counter" ]; then
   tmp_loop=$(($tmp_loop+1))
   continue
  fi
  
  #获得每列的最大长度
        countOfChar=`echo "$tmpvar" | wc -m`
        if [ "${each_cloum_max_char_number[$cloum_index]}" -lt "$countOfChar" ];then
            each_cloum_max_char_number[$cloum_index]=$(($countOfChar))
        fi 
        
        if [ "$cloum_index" -eq "$tmp_SOURCE_CLOUM" ];  then
            cloum_index=1
        else
            cloum_index=$(($cloum_index+1))
        fi
   done 
  
   ###逐行显示
   tmp_loop=0
   cloum_index=1
   tmpString=""
   echo ""
   for tmpvar in $*
   do
        #如果tmp_loop小于1,说明是你一个参数(列数),跳过)
  if [ "$tmp_loop" -lt "$tmp_KOWN_Para_counter" ]; then
   tmp_loop=$(($tmp_loop+1))
   continue
  fi
        tmpString+=" $cloum_index." 
        tmpString+=$tmpvar 
        
        countOfChar=`echo "$tmpvar" | wc -m`     
        while [ "$countOfChar" -lt "${each_cloum_max_char_number[$cloum_index]}" ]
        do 
           countOfChar=$(($countOfChar+1))
           tmpString+="\`" 
        done
           
        if  [ "$cloum_index" -eq "$tmp_SOURCE_CLOUM" ]; then    
           echo $tmpString
           cloum_index=1
           tmpString=""
        else
           cloum_index=$(($cloum_index+1))
        fi
   done
   echo ""
}

##在2D_ArrayShow中
##根据this_TermCloumIndex和this_TermCloumValue,查找第this_EXPECT_CLOUM_Index列的值
##参数: $1=this_TermCloumIndex $2=this_TermCloumValue $3=this_EXPECT_CLOUM_Index $4=this_SOURCE_CLOUM列数   $5=${2D_Array[@]}   
##用法: func_2D_ArrayCloumSelect 1 "jdk" 2 2 "${2D_Array[@]}"
##
function func_2D_ArrayCloumSelect()
{
 this_TermCloumIndex=$1
 this_TermCloumValue=$2
 this_EXPECT_CLOUM_Index=$3
 this_SOURCE_CLOUM=$4
 this_KOWN_Para_counter=4
 this_CursorIndex=0
    tmpColumString=""
 
    #echo "func_2D_ArrayCloumSelect=[$* ]"
    #将二维数组转换成一维数组this_Array,并获取其长度this_Counter
 tmpParaArray=($*)
 tmpParaArrayLen=${#tmpParaArray[@]}
 tmpLoop=0
 this_Counter=0
 for((tmpLoop=$this_KOWN_Para_counter;tmpLoop<$tmpParaArrayLen;tmpLoop++));do
  this_Array[$this_Counter]=${tmpParaArray[$tmpLoop]}
  this_Counter=$(($this_Counter+1))
 done
 
    ##查找匹配的行起始索引
    startLineIndex=0
    endLineIndex=$this_Counter
    ##循环获得第一个匹配的数据
    tmp_index=$startLineIndex
    while [ "$tmp_index" -lt "$endLineIndex" ]
    do
       this_CursorIndex=$(($tmp_index+$this_TermCloumIndex-1))
       if [ ${this_Array[$((this_CursorIndex))]} == "$this_TermCloumValue" ]; then
          this_ElementIndex=$(($tmp_index+$this_EXPECT_CLOUM_Index-1))
          tmpColumString+=${this_Array[$((this_ElementIndex))]}
          tmpColumString+=" "
    echo $tmpColumString
    return
       fi
       tmp_index=$(($this_SOURCE_CLOUM+$tmp_index))
    done  
    echo ""
}

##获取2D_ArrayShow中第this_EXPECT_CLOUM_Index列的所有值
##参数: $1=this_EXPECT_CLOUM_Index $2=this_SOURCE_CLOUM列数   $3=${2D_Array[@]} 
##用法: func_2D_ArrayGetCloum 2 $SOURCE_CLOUM "${2D_Array[@]}"
##
function func_2D_ArrayGetCloum()
{
 this_EXPECT_CLOUM_Index=$1
 this_SOURCE_CLOUM=$2
 this_KOWN_Para_counter=2
 this_CursorIndex=0
    tmpColumString=""
 
    #echo "func_2D_ArrayCloumSelect=[$* ]"
    #将二维数组转换成一维数组this_Array,并获取其长度this_Counter
 tmpParaArray=($*)
 tmpParaArrayLen=${#tmpParaArray[@]}
 tmpLoop=0
 this_Counter=0
 for((tmpLoop=$this_KOWN_Para_counter;tmpLoop<$tmpParaArrayLen;tmpLoop++));do
  this_Array[$this_Counter]=${tmpParaArray[$tmpLoop]}
  this_Counter=$(($this_Counter+1))
 done
 
    ##查找匹配的行起始索引
    startLineIndex=0
    endLineIndex=$this_Counter
    ##循环获得第this_EXPECT_CLOUM_Index列的所有值
    tmp_index=$startLineIndex
    while [ "$tmp_index" -lt "$endLineIndex" ]
    do
       this_ElementIndex=$(($tmp_index+$this_EXPECT_CLOUM_Index-1))
       tmpColumString+=${this_Array[$((this_ElementIndex))]}
       tmpColumString+=" "

       tmp_index=$(($this_SOURCE_CLOUM+$tmp_index))
    done  
    echo $tmpColumString
}

######################################################################################
###二维数组2D_Array相关操作:End######################################################
######################################################################################


##判断文件或路径是否存在
function func_isFileOrPathExist()
{
 tmpFilePath=$1
 if [ ! -d "$tmpFilePath" ] && [ ! -f "$tmpFilePath" ]; then
  echo "-------------------------------------------"
  echo "-->ERR:$tmpFilePath does not exist!"
  echo "-------------------------------------------"
  exit
 else 
  echo "OK:$tmpFilePath"
 fi
}

##打印步骤信息
function func_SetpInfo()
{
 echo ""
 echo "##step $G_StepINDEX $1"
 ((G_StepINDEX++))
}

##数组显示
##调用方法:#func_ShowArr "${G_packagesArray[*]}"
function func_ShowArr()
{
  #return
  i=0
  arr=$1
  for element in ${arr[*]}; do
 ((i++))
    echo $i $element
  done
}


##脚本使用方法说明
function func_Usage()
{
 echo "Usage"
 echo "   sudo ./main_makeiso.sh mount_copy"
 echo "   sudo ./main_makeiso.sh copy2iso"
 echo "   sudo ./main_makeiso.sh genISO"
 echo "   sudo ./main_makeiso.sh genInfrastructure"
}

 

 

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有