Unix shell - 在for循环中读取数组内容

时间:2013-05-17 09:21:25

标签: arrays shell for-loop ksh

这是我的阵列:

orlist=""
orlist="T_TAB1 \n"
orlist=$orlist"T_TAB2 \n"
orlist=$orlist"T_TAB3 \n"
orlist=$orlist"T_TAB4 \n"
echo $orlist
arrIdx=0
OLD_IFS=$IFS;
IFS="\n"
for IndixList in ${orlist[@]};
do
     echo $IndxList
     MYDIR[${arraryIndix}]=$IndixList
    (( arraryIndix = $arraryIndix+ 1 ))
done
IFS=$OLD_IFS

我必须在for循环中的oracle db中执行SELECT,所以我必须通过选项卡读取$ orlist选项卡。我试过这个但是不起作用它需要整个数组不是逐个标签:

for arraryIndix in ${orlist[@]};
do

  echo "SET HEADING OFF"       >> ${FILEOR_SQL}
  echo "SET TERMOUT OFF"       >> ${FILEOR_SQL}
  echo "SET PAGESIZE 0"        >> ${FILEOR_SQL}
  echo "SET LINESIZE 1000"     >> ${FILEOR_SQL}
  echo "SET FEEDBACK OFF"      >> ${FILEOR_SQL}
  echo "SET TRIMSPOOL ON"      >> ${FILEOR_SQL}
  echo "SPOOL ${FILE_DAT}"     >> ${FILEOR_SQL}
  echo "SELECT * "             >> ${FILEOR_SQL}
  echo "FROM ${orlist[@]}"     >> ${FILEOR_SQL}
  echo "WHERE REP_ARG = 2; "   >> ${FILEOR_SQL}
  echo "SPOOL OFF"             >> ${FILEOR_SQL}
  echo "COMMIT;"               >> ${FILEOR_SQL}
  echo "SET HEADING ON"        >> ${FILEOR_SQL}
  echo "SET TERMOUT ON"        >> ${FILEOR_SQL}
  echo "SET PAGESIZE 14"       >> ${FILEOR_SQL}
  echo "SET FEEDBACK ON"       >> ${FILEOR_SQL}
  echo "SET TRIMSPOOL OFF"     >> ${FILEOR_SQL}
  echo "EXIT;"                 >> ${FILEOR_SQL}

  sqlplus -S -L ${Connection} @${FILEOR_SQL} #connection is a var for connect with `sqlplus`

done

有什么建议吗?提前致谢

2 个答案:

答案 0 :(得分:1)

不要多次调用sqlplus,而是让SQL脚本包含所有查询:

cat < END1 > ${FILEOR_SQL}
SET HEADING OFF
SET TERMOUT OFF
SET PAGESIZE 0
SET LINESIZE 1000
SET FEEDBACK OFF
SET TRIMSPOOL ON
SPOOL ${FILE_DAT}
END1

orlist=(T_TAB1 T_TAB2 T_TAB3 T_TAB4)

for table in "${orlist[@]}"; do
    echo "SELECT * FROM $table WHERE REP_ARG = 2;" >> ${FILEOR_SQL}
done

echo "QUIT" >> ${FILEOR_SQL}
sqlplus -S -L ${Connection} @${FILEOR_SQL}

答案 1 :(得分:0)

好吧你的问题来了,因为如果你写\n,它不一定被视为新行,有人必须将\后跟n的序列翻译为换行符。同样在IFS="\n"中,IFS需要设置为评估为换行符的内容,而不是\n的组合。 orlist也是一个变量,你没有将它用作数组,而在for循环中,它不会被视为一个数组。 我做了一些改动,似乎工作正常

#!/usr/local/bin/ksh
orlist=""
orlist="T_TAB1 \n"
orlist=$orlist"T_TAB2 \n"
orlist=$orlist"T_TAB3 \n"
orlist=$orlist"T_TAB4 \n"
echo  $orlist
arrIdx=0
OLD_IFS=$IFS;
IFS=$'\n'
#IFS=""
arraryIndix=0
for IndxList in `echo -e $orlist`
do
     echo "Hello $IndxList "
     MYDIR[${arraryIndix}]=$IndxList
    ((arraryIndix++))
done
IFS=$OLD_IFS
echo "Finally ${MYDIR[@]}"

输出

T_TAB1 \nT_TAB2 \nT_TAB3 \nT_TAB4 \n
Hello T_TAB1
Hello T_TAB2
Hello T_TAB3
Hello T_TAB4
Finally T_TAB1  T_TAB2  T_TAB3  T_TAB4


$ ksh --version
version         sh (AT&T Research) 93t+ 2010-02-02

更新以下评论

FILEOR_SQL=""
func() {
  echo "SET HEADING OFF"       >> ${FILEOR_SQL}
  echo "SET TERMOUT OFF"       >> ${FILEOR_SQL}
  echo "SET PAGESIZE 0"        >> ${FILEOR_SQL}
  echo "SET LINESIZE 1000"     >> ${FILEOR_SQL}
  echo "SET FEEDBACK OFF"      >> ${FILEOR_SQL}
  echo "SET TRIMSPOOL ON"      >> ${FILEOR_SQL}
  echo "SPOOL random     "     >> ${FILEOR_SQL}
  echo "SELECT * "             >> ${FILEOR_SQL}
  echo "FROM $1"               >> ${FILEOR_SQL}
  echo "WHERE REP_ARG = 2; "   >> ${FILEOR_SQL}
  echo "SPOOL OFF"             >> ${FILEOR_SQL}
  echo "COMMIT;"               >> ${FILEOR_SQL}
  echo "SET HEADING ON"        >> ${FILEOR_SQL}
  echo "SET TERMOUT ON"        >> ${FILEOR_SQL}
  echo "SET PAGESIZE 14"       >> ${FILEOR_SQL}
  echo "SET FEEDBACK ON"       >> ${FILEOR_SQL}
  echo "SET TRIMSPOOL OFF"     >> ${FILEOR_SQL}
  echo "EXIT;"
}
orlist=""
orlist="T_TAB1 \n"
orlist=$orlist"T_TAB2 \n"
orlist=$orlist"T_TAB3 \n"
orlist=$orlist"T_TAB4 \n"
echo  $orlist
OLD_IFS=$IFS;
IFS=$'\n'
arraryIndix=0;
for IndxList in `echo -e $orlist`
do
  FILEOR_SQL="testfilesql"$arraryIndix
  func $IndxList
  ((arraryIndix++))
done
IFS=$OLD_IFS