この記事は約35分23秒で読むことができます。

bashメソッド抜き出しシェルスクリプトを作成した話(しかけ)

tt.shスクリプト

localをはずしてrutineが呼ばれた順にvariableが上書きされていくことを利用しています。

コード表示

[oracle@centos rr]$ cat tt.sh
#!/bin/bash

_arg_prc(){
  ARG_LZ=$1
  ARG_LZ=$(echo "" | awk '
  BEGIN{
    ARG_LZ="'${ARG_LZ}'"
  }
  {
    print ARG_LZ

  }')
  SRC_FNM=$(echo "${ARG_LZ}" | tr ' ' '\n')
}

_cp(){
  FILE_NM=$1
  echo "${FILE_NM}"
  CMD="grep \")\s*{\" "${FILE_NM}" | sed -e 's/(){//g' | xargs -I@ bash -c 'echo _test@.sh' | xargs -t -I@ bash -c 'cp ${FILE_NM} test/@'"
  eval "${CMD}"
}

_while_prc_cp(){
  SRC_FNM="$1"
  echo "${SRC_FNM}"
  echo "${SRC_FNM}" | while read F;do
    _cp "${F}" 
  done
}

_while_prc_lzup(){
  SRC_FNM="$1"
  echo "${SRC_FNM}" | while read F;do
    _lzup "${F}" 
  done
}

_lzup(){
  SRC_FNM="$1"
  TGT_FNM=$(grep ")\s*{" ${SRC_FNM} | sed -e 's/(){//g' | xargs -I@ bash -c 'echo _test@.sh')
  echo "${TGT_FNM}"
  RUTINE_NM=$(echo "${TGT_FNM//_test/}" | sed -e 's/.sh//g')
  echo "${RUTINE_NM}"
}

_arg_prc $*
_while_prc_cp "${SRC_FNM}"
_while_prc_lzup "${SRC_FNM}"

t.shスクリプト

コード表示

[oracle@centos rr]$ cat t.sh
#!/bin/bash

_build_cmd(){
  START_RN=$1
  END_RN=$2
  FILE_NM=$3
  BUILD_CMD="sed -i -n '""${START_RN}"",""${END_RN}""p""'"" ${FILE_NM}"
}

_first_filter(){
  START_STR=$1
  END_STR=$2
  FILE_NM=$3
  GET_START_RN_CMD="sed -n '/${START_STR}/=' ${FILE_NM} | tail -n 1"
  GET_END_RN_CMD="sed -n '/${END_STR}/=' ${FILE_NM} | tail -n 1"
  START_RN=$(eval "${GET_START_RN_CMD}")
  END_RN=$(eval "${GET_END_RN_CMD}")
}

_second_filter(){
  START_STR=$1
  END_STR=$2
  FILE_NM=$3
  GET_START_RN_CMD="sed -n '/${START_STR}/=' ${FILE_NM} | tail -n 1"
  GET_END_RN_CMD="sed -n '/${END_STR}/=' ${FILE_NM} | head -n 1"
  START_RN=$(eval "${GET_START_RN_CMD}")
  END_RN=$(eval "${GET_END_RN_CMD}")
}

_exec_cmd(){
  BUILD_CMD="$1"
  eval ${BUILD_CMD}
}

_insert_shebang(){
  FILE_NM=$1
  sed -i '1i#!/bin/bash\n' ${FILE_NM}
}

_main(){
  START_STR=$1
  END_STR=$2
  FILE_NM=$3
  _first_filter ${START_STR} ${END_STR} ${FILE_NM}
  _build_cmd ${START_RN} ${END_RN} ${FILE_NM}
  _exec_cmd "${BUILD_CMD}"
  _second_filter ${START_STR} ${END_STR} ${FILE_NM}
  _build_cmd ${START_RN} ${END_RN} ${FILE_NM}
  _exec_cmd "${BUILD_CMD}"
  _insert_shebang ${FILE_NM}
}

_main $1 $2 $3

tt.sh実行

コード表示

[oracle@centos rr]$ ./tt.sh "test/make/\\x2a.sh\\x20util/\\x2a.sh"
test/make/*.sh
util/*.sh
test/make/*.sh
bash -c cp test/make/*.sh test/_test_call_dynamic_cmd.sh 
bash -c cp test/make/*.sh test/_test_cmd_test_clean.sh 
bash -c cp test/make/*.sh test/_test_grp.sh 
bash -c cp test/make/*.sh test/_test_join.sh 
bash -c cp test/make/*.sh test/_test_split.sh 
bash -c cp test/make/*.sh test/_test_replica_cmd.sh 
bash -c cp test/make/*.sh test/_test_sort_cmd_with_str.sh 
bash -c cp test/make/*.sh test/_test_sort_cmd_with_numkey.sh 
bash -c cp test/make/*.sh test/_test_replace_cmd.sh 
bash -c cp test/make/*.sh test/_test_grp_rn.sh 
bash -c cp test/make/*.sh test/_test_mk_decopd_dynamic_cmd_non_tac.sh 
bash -c cp test/make/*.sh test/_test_mk_decopd_dynamic_cmd_tac.sh 
bash -c cp test/make/*.sh test/_test_mk_pd_dynamic_cmd_tac.sh 
bash -c cp test/make/*.sh test/_test_mk_pd_dynamic_cmd_non_tac.sh 
bash -c cp test/make/*.sh test/_test_mk_ascii_dynamic_cmd.sh 
bash -c cp test/make/*.sh test/_test_mk_ascii_code_non_lf.sh 
bash -c cp test/make/*.sh test/_test_mk_ascii_code.sh 
bash -c cp test/make/*.sh test/_test_mk_ascii_char_non_lf.sh 
bash -c cp test/make/*.sh test/_test_mk_ascii_char.sh 
bash -c cp test/make/*.sh test/_test_radical_replica_str.sh 
bash -c cp test/make/*.sh test/_test_gradual_non_tac_replica_str.sh 
bash -c cp test/make/*.sh test/_test_gradual_tac_replica_str.sh 
bash -c cp test/make/*.sh test/_test_mk_pd_tac_cmd.sh 
util/*.sh
bash -c cp util/*.sh test/_test_lpd.sh 
bash -c cp util/*.sh test/_test_rpd.sh 
bash -c cp util/*.sh test/_test_deco_ful_rev.sh 
bash -c cp util/*.sh test/_test_deco_ful.sh 
bash -c cp util/*.sh test/_test_deco_ful_pd.sh 
bash -c cp util/*.sh test/_test_deco_lft_lpd.sh 
bash -c cp util/*.sh test/_test_deco_lft.sh 
bash -c cp util/*.sh test/_test_deco_rgt_rpd.sh 
bash -c cp util/*.sh test/_test_deco_rgt.sh 
_test_call_dynamic_cmd.sh
_test_cmd_test_clean.sh
_test_grp.sh
_test_join.sh
_test_split.sh
_test_replica_cmd.sh
_test_sort_cmd_with_str.sh
_test_sort_cmd_with_numkey.sh
_test_replace_cmd.sh
_test_grp_rn.sh
_test_mk_decopd_dynamic_cmd_non_tac.sh
_test_mk_decopd_dynamic_cmd_tac.sh
_test_mk_pd_dynamic_cmd_tac.sh
_test_mk_pd_dynamic_cmd_non_tac.sh
_test_mk_ascii_dynamic_cmd.sh
_test_mk_ascii_code_non_lf.sh
_test_mk_ascii_code.sh
_test_mk_ascii_char_non_lf.sh
_test_mk_ascii_char.sh
_test_radical_replica_str.sh
_test_gradual_non_tac_replica_str.sh
_test_gradual_tac_replica_str.sh
_test_mk_pd_tac_cmd.sh
_call_dynamic_cmd
_cmd_clean
_grp
_join
_split
_replica_cmd
_sort_cmd_with_str
_sort_cmd_with_numkey
_replace_cmd
_grp_rn
_mk_decopd_dynamic_cmd_non_tac
_mk_decopd_dynamic_cmd_tac
_mk_pd_dynamic_cmd_tac
_mk_pd_dynamic_cmd_non_tac
_mk_ascii_dynamic_cmd
_mk_ascii_code_non_lf
_mk_ascii_code
_mk_ascii_char_non_lf
_mk_ascii_char
_radical_replica_str
_gradual_non_tac_replica_str
_gradual_tac_replica_str
_mk_pd_tac_cmd
_test_lpd.sh
_test_rpd.sh
_test_deco_ful_rev.sh
_test_deco_ful.sh
_test_deco_ful_pd.sh
_test_deco_lft_lpd.sh
_test_deco_lft.sh
_test_deco_rgt_rpd.sh
_test_deco_rgt.sh
_lpd
_rpd
_deco_ful_rev
_deco_ful
_deco_ful_pd
_deco_lft_lpd
_deco_lft
_deco_rgt_rpd
_deco_rgt
[oracle@centos rr]$ tree test
test
├── __test.sh
├── _test_call_dynamic_cmd.sh
├── _test_cmd_test_clean.sh
├── _test_deco_ful.sh
├── _test_deco_ful_pd.sh
├── _test_deco_ful_rev.sh
├── _test_deco_lft.sh
├── _test_deco_lft_lpd.sh
├── _test_deco_rgt.sh
├── _test_deco_rgt_rpd.sh
├── _test_gradual_non_tac_replica_str.sh
├── _test_gradual_tac_replica_str.sh
├── _test_grp.sh
├── _test_grp_rn.sh
├── _test_join.sh
├── _test_lpd.sh
├── _test_mk_ascii_char.sh
├── _test_mk_ascii_char_non_lf.sh
├── _test_mk_ascii_code.sh
├── _test_mk_ascii_code_non_lf.sh
├── _test_mk_ascii_dynamic_cmd.sh
├── _test_mk_decopd_dynamic_cmd_non_tac.sh
├── _test_mk_decopd_dynamic_cmd_tac.sh
├── _test_mk_pd_dynamic_cmd_non_tac.sh
├── _test_mk_pd_dynamic_cmd_tac.sh
├── _test_mk_pd_tac_cmd.sh
├── _test_radical_replica_str.sh
├── _test_replace_cmd.sh
├── _test_replica_cmd.sh
├── _test_rpd.sh
├── _test_sort_cmd_with_numkey.sh
├── _test_sort_cmd_with_str.sh
├── _test_split.sh
├── make
│   └── mk_exe_cmd.sh
├── oput
└── tmp

3 directories, 34 files

t.sh実行

とりま一つだけ

コード表示

[oracle@centos rr]$ cat test/_test_split.sh
#!/bin/bash

_call_dynamic_cmd(){
  while read cmd;do
    eval ${cmd};
  done < <(cat $(pwd)/test/oput/mk_dynamic_cmd);
}

_cmd_test_clean(){
  rm -rf $(pwd)/test/{tmp,oput}/*
}

_grp(){
  local RN=$1
  local GRP=$2
  while read LINE;do
     echo ${LINE} | sed -e s/GRP/${GRP}/ | bash;
  done < <(seq ${RN} | xargs -I@ bash -c 'echo echo $\(\(@%GRP\)\)')
}

_join(){
  local LFT=$1
  local RGT=$2
  local TGT_FILE=$3
  paste -d ' ' ${LFT} ${RGT} > ${TGT_FILE}
}

_split(){
  local SRC_FILE=$1
  local TGT_FILE=$2
  cat ${SRC_FILE} | awk '
  BEGIN{
    TGT_FILE="'${TGT_FILE}'"
  }
  {
    print > TGT_FILE"_split_"$1
  }
  '
}

_replica_cmd(){
  local LINE_START=$1
  local LINE_END=$2
  local SRC_FILE=$3
  local TGT_FILE=$4
  seq ${LINE_START} ${LINE_END} | while read RN;do
    cat ${SRC_FILE}
  done > ${TGT_FILE}
}

_sort_cmd_with_str(){
  local SORT_KEY=$1
  local SRC_FILE=$2
  local TGT_FILE=$3
  sort -k ${SORT_KEY},${SORT_KEY} ${SRC_FILE} >> ${TGT_FILE}
}

_sort_cmd_with_numkey(){
  local SORT_KEY=$1
  local SRC_FILE=$2
  local TGT_FILE=$3
  sort -n -k ${SORT_KEY},${SORT_KEY} ${SRC_FILE} >> ${TGT_FILE}
}

_replace_cmd(){
  local LINE_START=$1
  local LINE_END=$2
  local TGT_FILE=$3
  while read F;do
    cat ${F} | nl | awk '
    BEGIN{
    }
    {
      print $3"\x20"$1"\x20"$5"\x20"$6;
    }
    '
  done < <((find $(pwd)/test/tmp -type f -name "*split*")) > ${TGT_FILE}
}

_grp_rn(){
  local LINE_START=$1
  local LINE_END=$2
  local REPLICA=$3
  local ZERO_PAD=$4
  local PLAIN_FILE=$(pwd)/test/tmp/mk_dynamic_cmd
  local POSTREPLICA_FILE=$(pwd)/test/tmp/mk_postreplica_dynamic_cmd
  local POSTSORT_FILE=$(pwd)/test/tmp/mk_postsort_dynamic_cmd
  local GRPTMP_FILE=$(pwd)/test/tmp/grptmp
  local POSTGRP_FILE=$(pwd)/test/tmp/mk_postgrp_dynamic_cmd
  local POSTREPLACE_FILE=$(pwd)/test/tmp/mk_postreplace_dynamic_cmd
  local OPUT_FILE=$(pwd)/test/oput/mk_dynamic_cmd
  _replica_cmd ${LINE_START} ${LINE_END} ${PLAIN_FILE} ${POSTREPLICA_FILE}
  _sort_cmd_with_str ${ZERO_PAD} ${POSTREPLICA_FILE} ${POSTSORT_FILE}
  printf "%0${ZERO_PAD}g\n" $(_grp $(cat ${POSTSORT_FILE} | wc -l) $(($(cat ${POSTSORT_FILE} | wc -l)/${REPLICA}))) | sort > ${GRPTMP_FILE}
  _join ${GRPTMP_FILE} ${POSTSORT_FILE} ${POSTGRP_FILE}
  _split ${POSTGRP_FILE} ${POSTGRP_FILE}
  _replace_cmd  ${LINE_START} ${LINE_END} ${POSTREPLACE_FILE}
  cp ${POSTREPLACE_FILE} ${OPUT_FILE}
}

_mk_decopd_dynamic_cmd_non_tac(){
  local LINE_START=$1
  local LINE_END=$2
  local MX_PAD=$3
  local CMD=$4
  local ASCII=$5
  local STR=$6
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
      MX_PAD="'${MX_PAD}'"
      CMD="'${CMD}'"
      ASCII="'${ASCII}'"
      STR="'${STR}'"
    }
    {
      print CMD"\x20"RN"\x20"MX_PAD"\x20\x27"STR"\x27\x20\x27"ASCII"\x27"
    }
    '
  done < <(seq ${LINE_START} ${LINE_END}) >> $(pwd)/test/oput/mk_dynamic_cmd
}

_mk_decopd_dynamic_cmd_tac(){
  local LINE_START=$1
  local LINE_END=$2
  local MX_PAD=$3
  local CMD=$4
  local ASCII=$5
  local STR=$6
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
      MX_PAD="'${MX_PAD}'"
      CMD="'${CMD}'"
      ASCII="'${ASCII}'"
      STR="'${STR}'"
    }
    {
      print CMD"\x20"RN"\x20"MX_PAD"\x20\x27"STR"\x27\x20\x27"ASCII"\x27"
    }
    '
  done < <(seq ${LINE_START} ${LINE_END} | tac) >> $(pwd)/test/oput/mk_dynamic_cmd
}

_mk_pd_dynamic_cmd_tac(){
  local LINE_START=$1
  local LINE_END=$2
  local CMD=$3
  local STR=$4
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
      CMD="'${CMD}'"
      STR="'${STR}'"
    }
    {
      print CMD"\x20"RN"\x20\x27"STR"\x27"
    }
    '
  done < <(seq ${LINE_START} ${LINE_END} | tac) >> $(pwd)/test/oput/mk_dynamic_cmd
}

_mk_pd_dynamic_cmd_non_tac(){
  local LINE_START=$1
  local LINE_END=$2
  local CMD=$3
  local STR=$4
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
      CMD="'${CMD}'"
      STR="'${STR}'"
    }
    {
      print CMD"\x20"RN"\x20\x27"STR"\x27"
    }
    '
  done < <(seq ${LINE_START} ${LINE_END}) >> $(pwd)/test/oput/mk_dynamic_cmd
}

_mk_ascii_dynamic_cmd(){
  local LINE_START=$1
  local LINE_END=$2
  local CMD=$3
  local STR=$4
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
      CMD="'${CMD}'"
      STR="'${STR}'"
    }
    {
      ASCII=sprintf("%c",strtonum(RN))
      print CMD"\x20""RNN""\x20\x27"ASCII"\x27\x20\x27"STR"\x27"
    }
    '
  done < <(seq ${LINE_START} ${LINE_END}) >> $(pwd)/test/tmp/mk_dynamic_cmd
}

_mk_ascii_code_non_lf(){
  local LINE_START=$1
  local LINE_END=$2
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
    }
    {
      printf("\\x%x",strtonum(RN))
    }
    '
  done < <(seq ${LINE_START} ${LINE_END})
}

_mk_ascii_code(){
  local LINE_START=$1
  local LINE_END=$2
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
    }
    {
      printf("\\x%x\n",strtonum(RN))
    }
    '
  done < <(seq ${LINE_START} ${LINE_END})
}

_mk_ascii_char_non_lf(){
  local LINE_START=$1
  local LINE_END=$2
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
    }
    {
      printf("%c",strtonum(RN))
    }
    '
  done < <(seq ${LINE_START} ${LINE_END})
}

_mk_ascii_char(){
  local LINE_START=$1
  local LINE_END=$2
  while read RN;do
    echo "" | awk '
    BEGIN{
      RN="'${RN}'"
    }
    {
      ASCII=sprintf("%c",strtonum(RN))
      print ASCII
    }
    '
  done < <(seq ${LINE_START} ${LINE_END})
}


_radical_replica_str(){
  local STR=$1
  local START_RN=$2
  local INTERVAL=$3
  local END_RN=$4
  local LN=$(while read RN;do
    printf "%${RN}s" ${STR};
  done < <(seq ${START_RN} ${INTERVAL} ${END_RN}))
  echo "'"${LN}"'" | sed -e 's/ //g';
}

_gradual_non_tac_replica_str(){
  local STR=$1
  local START_RN=$2
  local INTERVAL=$3
  local END_RN=$4
  while read RN;do
    TMP_LN=$(printf "%$((RN==1?1:RN))s" "@");
    REP=$(echo "${TMP_LN}" | sed -e 's/ /@/g');
    echo "'"${REP//@/${STR}}"'";
  done < <(seq ${START_RN} ${INTERVAL} ${END_RN})
}

_gradual_tac_replica_str(){
  local STR=$1
  local START_RN=$2
  local INTERVAL=$3
  local END_RN=$4
  while read RN;do
    TMP_LN=$(printf "%$((RN==1?1:RN))s" "@");
    REP=$(echo "${TMP_LN}" | sed -e 's/ /@/g');
    echo "'"${REP//@/${STR}}"'";
  done < <(seq ${START_RN} ${INTERVAL} ${END_RN} | tac)
}

_mk_pd_tac_cmd(){
  STR=$1
  START_RN=$2
  INTERVAL=$3
  END_RN=$4
  DYN_CMD=$5
  PAD_CMD=$6
  REP_CMD=$7
  while read RN;do
    TMP=$( sed -e 's/ /@/g' <<<$(${REP_CMD} ${STR} ${START_RN} ${INTERVAL} ${RN}))
    echo ${TMP}
    #TODO  --opiton -a -b -c
    #${DYN_CMD} ${START_RN} ${RN} ${PAD_CMD} ${TMP##*@} 
    #${DYN_CMD} ${START_RN} ${END_RN} ${PAD_CMD} ${TMP##*@} 
    #${DYN_CMD} ${END_RN} ${END_RN} ${PAD_CMD} ${TMP##*@} 
  done < <(seq ${START_RN} ${INTERVAL} ${END_RN})
}
[oracle@centos rr]$ ./t.sh "_split(){" "^}" "test/_test_split.sh"
[oracle@centos rr]$ cat test/_test_split.sh
#!/bin/bash

_split(){
  local SRC_FILE=$1
  local TGT_FILE=$2
  cat ${SRC_FILE} | awk '
  BEGIN{
    TGT_FILE="'${TGT_FILE}'"
  }
  {
    print > TGT_FILE"_split_"$1
  }
  '
}

Leave a Reply

Your email address will not be published. Required fields are marked *