0

this is my array:

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

i have to do a SELECT in a oracle db inside a for loop so i have to read the $orlist tab by tab. I've tried this but doesn't work it takes the whole array not tab by tab:

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

Any suggestions? Thanks in advance

12
  • How come you're not using actual arrays? Commented May 17, 2013 at 9:37
  • in wich sense?sorry i don't understand your question Commented May 17, 2013 at 9:50
  • In the sense of an actual array. var=("element 1" "element 2") Commented May 17, 2013 at 9:52
  • and then?for the loop? Commented May 17, 2013 at 9:58
  • however i've got an error: Syntax error at line 101 : `(' is not expected. Commented May 17, 2013 at 10:01

2 Answers 2

1

Instead of invoking sqlplus many times, make the SQL script contain all the queries:

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}
Sign up to request clarification or add additional context in comments.

Comments

0

Well your problem came because if you write \n, it's not necessarily treated as a new line, somebody have to translate the sequence of \ followed by n as a newline. also same in IFS="\n" IFS needs to be set to something which evaluates to a newline, not a combination of \ and n. Also orlist is a variable, you have not used it as an array, and at the for loop, it is not going to be treated as an array. I did some changes and it seemed to work fine

#!/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[@]}"

Output

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

Update following comments

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

14 Comments

Are you sure that this way goes with ksh? Because i have Syntax errors
mmh ok done. Now my list is a list line by line.. For the for loop? How can i do it?
I checked in ksh, seems to be working same, Sorry, i did not understand list is a list line by line
yes, but before my outpu was : T_TAB1 \nT_TAB2 \nT_TAB3 \nT_TAB4 \n only and not line by line :). The problem is the loop.. Seems the loop doesn't recognize the newline.. it stamp me in the output sql file the whole array: FROM T_TAB1 \nT_TAB2 \nT_TAB3 \nT_TAB4 \n ando not fro the first cicle FROM TAB1 for the second FROM TAB2 etc etc.. understand?
ahh, mean the 2nd loop? here first iteration should create a file with FROM T_TAB1, second one FROM T_TAB2 right? then you already have the array prepared in MYDIR, instead of for arraryIndix in ${orlist[@]}; use for arraryIndix in ${MYDIR[@]};.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.