1

I am trying to read table names and index names from two different text files and then building the sql files which can be used to do the de-fragmentation and index re-building. It is working fine as of now but generated sql file contains next line after table/index name. PFB script and sample outputs.

BASH Script:

#!/bin/bash

export logfile=/tmp/logfile.log
export tablenamesfile=/tmp/tablenames.txt
export tabledefragfile=/tmp/tabledefrag.sql
export indexnamesfile=/tmp/indexnames.txt
export indexrebuildfile=/tmp/indexrebuild.sql

while read line; do echo "alter table $line enable row movement;" >> $tabledefragfile; echo "alter table $line shrink space;" >> $tabledefragfile; echo "alter table $line disable row movement;" >> $tabledefragfile; done < $tablenamesfile
echo "Please check sql file - $(readlink -f $tabledefragfile)"

while read line; do echo "alter index $line rebuild parameters('tablespace=TBS_3');" >> $indexrebuildfile; done < $indexnamesfile
echo "Please check sql file - $(readlink -f $indexrebuildfile)"

exit

Current output in tabledefrag.sql

alter table APA_DS_DATASTORE
 enable row movement;
alter table APA_DS_DATASTORE
 shrink space;
alter table APA_DS_DATASTORE
 disable row movement;

Current output in indexrebuild.sql

alter index APA_DS_DATASTORE_PK
 rebuild parameters('tablespace=TBS_3');
alter index APA_FS_DEFAULT_FSENTRY_IDX
 rebuild parameters('tablespace=TBS_3');
alter index APA_FS_DEFAULT_FSENTRY_IDX
 rebuild parameters('tablespace=TBS_3');

Expected output:

alter index APA_DS_DATASTORE_PK rebuild parameters('tablespace=TBS_3');
alter index APA_FS_DEFAULT_FSENTRY_IDX rebuild parameters('tablespace=TBS_3');
alter index APA_FS_DEFAULT_FSENTRY_IDX rebuild parameters('tablespace=TBS_3');
4
  • @alecxs input is taken from two different text files which contain table/index names like below. Each table/index name is on new line... APA_DS_DATASTORE_PK APA_FS_DEFAULT_FSENTRY_IDX Commented Jul 13, 2020 at 12:51
  • hm.. right, understand the issue now. don't know if that is expected but seems read does save \n as last byte into $line, so the proper way would be to tell read not to do that (but i don't know how). as workaround you can echo without quotes, or remove the last byte (in general ${line%?} or explicit ${line//$'\n'}) Commented Jul 13, 2020 at 13:45
  • you can edit your question and add the output of printf "%s" "$line" | hexdump -C for debugging purposes Commented Jul 13, 2020 at 13:49
  • @alecxs issue is resolved by putting while IFS=$' \t\n\r' read -r line. Thank you for your time and help. Commented Jul 13, 2020 at 13:56

1 Answer 1

2

echo has a newline by default. You should use printf instead.

You'll also need to avoid the newline coming from the file: while IFS=$' \t\n\r' read -r LINE

Sign up to request clarification or add additional context in comments.

4 Comments

printf didn't resolve the issue. My issue is that there is new line after I print table name using $line in echo. Remaining part of the statement for e.g. 'enable row movement;' should be printed in the same line.
while IFS=$' \t\n\r' read -r line should fix it. I'll edit my answer.
I just added while IFS=$' \t\n\r' read -r line. 'echo' remains as it is. So, maybe you can remove the suggestion to use 'printf' from answer. Now, I am getting the desired output. Thank you for the help.
You would be well advised to use printf in place of echo to avoid issues in future - even of its not the cause of this problem.

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.