redirect stdout and stderr to file wrong order problem with subshell

Hello

I read a lot of post related to this topic, but nothing helped me. :mad:

I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors.

#!/bin/ksh
...
readinput < $QCHAT_INPUT |&
 while read -p line
 do
   echo  $line
    ...
 fi
 done | ldapadd -x -h $HOST  -p $PORT -D cn=$USER -w $PASSWD -c
 

sttout output:

adding new entry "qchatUserId=21999013000010,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"
ldap_add: Already exists (68)

 adding new entry  "qchatUserId=000000mig1,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"

ldap_add: Already exists (68)

 ldapadd: invalid format \(line 1\) entry:  ""

I would like to have the same output in the file.

I tried such redirecting:

done | ( ldapadd -x -h $HOST -p $PORT -D cn=$USER -w $PASSWD -c 2>&3  1>&3 ) 3>log
done | (ldapadd -x -h $HOST -p $PORT -D cn=$USER -w  $PASSWD -c 2>&1) | tee log
done | ldapadd -x -h $HOST -p $PORT -D cn=$USER -w  $PASSWD -c > log 2>&1

All have the same useless output:
$ cat log

ldap_add: Already exists (68)
ldap_add: Already exists (68)
ldapadd: invalid format (line 1) entry: ""
adding new entry "qchatUserId=21999013000010,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"

 adding new entry  "qchatUserId=000000mig1,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"

I red that stdout is buffered and stderr not. But none solutions were working.

So is there a way how to force correct order in the file?
out1
err1
out2
err2
out3

No amount of redirection tricks are going to change the order in which the program prints things. I'm afraid my prev solution doesn't work since I slightly misunderstood the problem -- ldap runs all at once, not once for each individual line.

Which might be how you have to do it: run ldap once for each individual statement, instead of one big batch, so you can guarantee everything's done and printed every time ldap finishes one statement. Save its stdout and stderr into temp files then cat them in the order you want.

Thanks for try.

I still don't understand why it the order in the output is correct when it is send to /dev/tty.

I found a way how to store it to the file. To start script command

>script
>./check.sh
>exit
>cat typescript
Instance 00. Processing input : qchatUserIdAliasList.txt00. Start check old alias and add new alias
adding new entry "qchatUserId=21999013000010,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"
ldap_add: Already exists (68)

adding new entry "qchatUserId=000000mig1,ran=cdma,key=qchatUserIdNewTest,o=sprintpcs,o=sprintpcs.com"
ldap_add: Already exists (68)

end

To run it from command line:

>script -c ./check.sh -a log

Finally I have what I need, it took me 2 days. But I still like my shell :smiley:

Well, some programs check when they're attached to a tty and change their behavior. So it may see that you're not on a tty and not bother flushing its output for efficiency. I think (but aren't positive) that script uses a PTY to convince things its a terminal...