While read line only reads first line in HP-UX B.11.11 system

Hi All,

I am facing a strange problem with my code, I have a script which does housekeeping of oracle DB generated files. We have multiple DB's running with different OS users, so we are executing the script as root user, which will generate a dynamic script for each DB, and it will switch to the DB owner (OS user) and execute the dynamically generated cleanup script.

This script is working fine in our HP-UX B.11.31 & Linux servers but fails to execute as expected on HP-UX B.11.11 servers.

When i investigated it further, i found that the script is breaking out of while loop after processing first element in the loop.

Flow of the Script is as follows:

1) List out the DB's running on the server along with OS account under which it is running.
2) Generate a dynamic script for oracle realated cleanup activity.
3) Get into a while loop with list of DB's running
4) switching to DB owner (os user) and execute the dynamic script generated in step 2.

eg: su - abcora -c /tmp/runtime_DB1.ksh

5)repeat step 4 for all DB's running on the server and then exit out of the while loop

In case of our HP-UX B.11.11 system, after first iteration of switching user, it comes out of the loop.

There are no error messages displayed on the screen or captured in the log file, but script is not executing as expected.

Can you guys please help me in resolving this issue. If you need more info please let me know.

Regards,
Veeresham

su - abcora processes the shell start scripts of user abcora. (You did not say which login shell.)
There is certainly something that reads from stdin, that does "read away" the loop's input.
Work-arounds:

  1. redirect the stdin of suspicious commands
su - abcora -c /tmp/runtime_DB1.ksh </dev/null
  1. harden the loop with the following file descriptor magic
while read line <&3;do
 ... # stdin will read from &1 - while the loop reads from &3
done 3< list.txt

Hi MadeInGermany,

The user login shell is /usr/bin/sh

I am executing my script in ksh shell

I didn't understand the hardening loop section which you mentioned. Can you please elobarate?

Regards,
Veeresham

Please post the while loop that is breaking out!
(In your post replace eventual E-mail addresses or passwords by placeholders.)

here is the code

$CAT $TMP_FILE1|while read sid
do
fecho "Processing $sid for cleanup"
fecho "-----------------------------------"
export lv_INSTANCE_OWNER=$($PS -ef|$GREP pmon|$GREP $sid|$GREP -v grep|$AWK '{print$1}')
fecho "Instance Owner: $lv_INSTANCE_OWNER"

###############################################################
# CREATING RUNTIME SCRIPT TO EXECUTE AS ORACLE INSTANCE OWNER
###############################################################

$CAT > /tmp/runtime_${sid}.ksh <<EOF2
#!/usr/bin/ksh

. $SDDC_SCRIPT_LOC/sddc_ora_Housekeep_SetEnv.ksh
. $SDDC_SCRIPT_LOC/sddc_ora_Housekeep_FunctionLib.ksh #----Calling Function Script

rc=0
OUTFILE="/tmp/sddc_ora_${gv_host}_Housekeeping_${lv_TIMESTAMP}.log"
fecho "Executing /tmp/runtime_${sid}.ksh..."
chk_db_version $sid  #----Checking DB Version

if [ ${lv_ANY_DAY:-'FALSE'} = 'TRUE' ];then
        db_diag_cleanup \$mdb_ver "force"
else
        db_diag_cleanup \$mdb_ver
fi
fecho "Result code of /tmp/runtime_${sid}.ksh: \$rc"
fecho " "
EOF2

$CHMOD 755 /tmp/runtime_${sid}.ksh

$ECHO "Switching to $sid Instance Owner $lv_INSTANCE_OWNER"
$SU - $lv_INSTANCE_OWNER -c /tmp/runtime_${sid}.ksh > /dev/null
#rm_if_exists /tmp/runtime_${sid}.ksh
done

So the work-arounds are
1.

$SU - $lv_INSTANCE_OWNER -c /tmp/runtime_${sid}.ksh > /dev/null </dev/null

or
2.

while read sid <&3 # no $CAT $TMP_FILE1 |
do
 ...
done 3< $TMP_FILE1
1 Like

Hi MadeInGermany,

I will try your suggestions and get back to you.

Thanks for your time & suggestions!

Regards,
Veeresham

Hi MadeInGermany,

Your suggestion worked for me!
Thanks a lot for your help.
Just a question, what is the difference between below two ways of handling loops

$CAT $TMP_FILE1 |while read sid
do
..
done
while read sid <&3 # 
do
 ...
done 3< $TMP_FILE1

is the later one is more accurate?

It's not a question of "accurate", the computer does exactly what you tell it to with 100% precision.

The point is avoiding file descriptor #1, because something inside your loop "eats" it, ending the loop early.

while read LINE
do
        cat # This also reads from 'inputfile', so this loop will only run once!
done < inputfile

while read sid <&3 # 
do
        cat # It doesn't read from 'inputfile' since that's not standard input here.
done 3< inputfile