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

bash配列の間接参照

スクリプト

インデックスに注目

コード表示

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

prt_ary_ok(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(echo ${ARY} | sed -e 's/ /\n/g')
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

prt_ary_ng(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ="${ARY}"
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(echo {a..c} | sed -e 's/ /\n/g')
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

prt_ary_ng ary
prt_ary_ok ary

実行例

インデックスに注目

コード表示

[oracle@centos rr]$ ./t.sh
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
prt_ary_ng
a b c
1
a
b
c
0-->a b c
____________i_____________
prt_ary_ok
a b c
3
a
b
c
0-->a
1-->b
2-->c

こっちのほうがおしゃれかな

コード表示

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

prt_ary_ok(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<$(echo ${ARY}))
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

prt_ary_ng(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ="${ARY}"
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<$(echo {a..c}))
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

prt_ary_ng ary
prt_ary_ok ary

こっちのほうもおしゃれかな

コード表示

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

prt_ary_ok(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

prt_ary_ng(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ="${ARY}"
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<$(echo {a..c}))
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

prt_ary_ng $*
prt_ary_ok $*

[oracle@centos rr]$ ./t.sh ary
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
prt_ary_ng
a b c
1
a
b
c
0-->a b c
____________i_____________
prt_ary_ok
a b c
3
a
b
c
0-->a
1-->b
2-->c

こっちのほうもおしゃれかな

コード表示

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

prt_ary_ok(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

prt_ary_ng(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ="${ARY}"
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<"$1")
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

prt_ary_ng ary
prt_ary_ok ary
[oracle@centos rr]$ ./t.sh "$(echo {a..c})"
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
prt_ary_ng
a b c
1
a
b
c
0-->a b c
____________i_____________
prt_ary_ok
a b c
3
a
b
c
0-->a
1-->b
2-->c

こっちのほうもおしゃれかな

コード表示

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

prt_ary_ok(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

prt_ary_ng(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ="${ARY}"
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<"$2")
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

prt_ary_ng "$1" "$2"
prt_ary_ok "$1" "$2"
[oracle@centos rr]$ ./t.sh ary "$(echo {a..c})"
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
prt_ary_ng
a b c
1
a
b
c
0-->a b c
____________i_____________
prt_ary_ok
a b c
3
a
b
c
0-->a
1-->b
2-->c

こっちのほうもおしゃれかな

コード表示

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

set_ary(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

call_ary(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<"$2")
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

set_ary "$1" "$2"
call_ary "LZ"
[oracle@centos rr]$ ./t.sh ary "$(echo {a..c})"
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
set_ary
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
call_ary

0

こっちのほうもおしゃれかな

localはずすと見えるね

コード表示

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

set_ary(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

call_ary(){
  echo "____________i_____________"
  echo "${FUNCNAME[0]}"
  local ARYNM=$1
  eval ARY=\${$ARYNM[@]}
  local LZ=();
  while read LN;do
    LZ+=(${LN});
  done < <(sed -e 's/ /\n/g' <<<${ARY})
  echo ${LZ[@]}
  echo ${#LZ[@]}
  for i in ${LZ[@]};do
    echo $i;
  done
  for i in ${!LZ[@]};do
    echo $i'-->'${LZ[$i]};
  done
}

echo "____________e_____________"
ary=();
while read LN;do
  ary+=(${LN});
done < <(sed -e 's/ /\n/g' <<<"$2")
echo ${ary[@]}
echo ${#ary[@]}
for i in ${ary[@]};do
  echo $i;
done
for i in ${!ary[@]};do
  echo $i'-->'${ary[$i]};
done

set_ary "$1" "$2"
call_ary "LZ"
[oracle@centos rr]$ ./t.sh ary "$(echo {a..c})"
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
set_ary
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
call_ary
a b c
3
a
b
c
0-->a
1-->b
2-->c

最後に

local外したシェルスクリプトを別ファイルのshellからcallできるようになる

コード表示

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

source ./t.sh ary "$(echo {a..c})"
source ./t.sh ary "$(echo {A..C})"
[oracle@centos rr]$ ./tt.sh
____________e_____________
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
set_ary
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________i_____________
call_ary
a b c
3
a
b
c
0-->a
1-->b
2-->c
____________e_____________
A B C
3
A
B
C
0-->A
1-->B
2-->C
____________i_____________
set_ary
A B C
3
A
B
C
0-->A
1-->B
2-->C
____________i_____________
call_ary
A B C
3
A
B
C
0-->A
1-->B
2-->C

Leave a Reply

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