Redirection question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirection question
# 1  
Old 05-03-2011
Redirection question

I want to redirect stderr and have the following peice of code

Code:
 
$ cat t1.ksh
#!/bin/ksh
func2()
{
  diff /tmp/jdlkwjdlkejew /tmp/djlkwejdlewdjew >$OUTPUT_FILE 2>>$ERR_FILE
}

func1()
{
  let counter=0
  while [ $counter -lt 5 ]
  do
   print -u2 "Error: In main function"
   func2
   let counter=$counter+1
  done

} >$OUTPUT_FILE 2>$ERR_FILE

OUTPUT_FILE=/tmp/junk.out
ERR_FILE=/tmp/junk.err
rm -rf $OUTPUT_FILE $ERR_FILE
func1

When I run the code this is what my junk.err contains

Code:
 
$ cat junk.err
Error: In main function
Error: In main function
Error: In main function
Error: In main function
Error: In main function
rectory
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/jdlkwjdlkejew: No such file or directory

How can I make my errors appear in the correct order

Code:
 
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
...
...

Secondly, it looks like I got part of an erorr. I need to redirect the
func1 as shown because that is a standard template but I need to
capture errors from other functions. Can somebody show me how
to fix this issue

Last edited by radoulov; 05-03-2011 at 05:28 PM.. Reason: Code tags!
# 2  
Old 05-03-2011
You could remove the redirect inside func2:

Code:
zsh-4.3.11[t]% cat t1.ksh 
#!/bin/ksh
func2()
{
  diff /tmp/jdlkwjdlkejew /tmp/djlkwejdlewdjew
}

func1()
{
  let counter=0
  while [ $counter -lt 5 ]
  do
   print -u2 "Error: In main function"
   func2
   let counter=$counter+1
  done

} >$OUTPUT_FILE 2>$ERR_FILE

OUTPUT_FILE=/tmp/junk.out
ERR_FILE=/tmp/junk.err
rm -rf $OUTPUT_FILE $ERR_FILE
func1
zsh-4.3.11[t]% ./t1.ksh 
zsh-4.3.11[t]% cat /tmp/junk.err 
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/djlkwejdlewdjew: No such file or directory
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/djlkwejdlewdjew: No such file or directory
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/djlkwejdlewdjew: No such file or directory
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/djlkwejdlewdjew: No such file or directory
Error: In main function
diff: /tmp/jdlkwjdlkejew: No such file or directory
diff: /tmp/djlkwejdlewdjew: No such file or directory
zsh-4.3.11[t]%

This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on bash redirection

Hi, Can I get some explanation around this bash redirection? From what I have read, x < y means call the shell to redirect the output of y into x. Does this mean that this sequence of commands is executed from right to left? diff <(sort testfile.txt) <(sort testfile2.txt) Thanks, edit... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

Shell script output redirection question

OS : AIX 6.1 Shell : Korn in the url https://forums.oracle.com/forums/thread.jspa?threadID=361463&tstart=0 I came across a crontab entry example 00 23 * * 1,3,5 <complete shell script path> 1> <log file> 2>&1 From googling , I gathered that 0 - stdin 1 - stdout 2 - stderr I... (5 Replies)
Discussion started by: polavan
5 Replies

3. Shell Programming and Scripting

output redirection to existing file question

So I have a existing file that I used the uniq command on and I need to save the output to the same file without changing the file name. I have tried $ uniq filename > filename then when I cat the file it then becomes blank like there is nothing inside. any help would be much appreciated... (0 Replies)
Discussion started by: drew211
0 Replies

4. Shell Programming and Scripting

Error redirection question

Hi gurus, I have a question, need some of your inputs... I have a command like this : export LINE_COUNT=`wc -l test.dat | awk '{print $1}'` echo $LINE_COUNT --- > gives me 2 which is fine as the file has 2 lines. This works fine if the file test.dat is present but in case... (3 Replies)
Discussion started by: calredd
3 Replies

5. Shell Programming and Scripting

Question about IO redirection

for shell operators like <, >, <<, and >> do the LHS always have to be a process or device and the RHS a file or device? Is it possible for the RHS to be a process? (1 Reply)
Discussion started by: stevenswj
1 Replies

6. Shell Programming and Scripting

Redirection Question

I just wondered if I'm understanding this command line correctly cat 2>save1 0<memo | sort 2>save2 1>letter This means that stdin will read from memo and if there is an error then stderr will write to save1. Am I correct in saying that the sort command will sort the memo file and write... (2 Replies)
Discussion started by: snag49ers
2 Replies

7. Shell Programming and Scripting

BASH Problem / Question regarding redirection

Hi all, Maybe someone is able to help: Need to redirect the output of a command in realtime to a second command. Command-A executes a remote shell to another host, and outputs its results. Command-B displays a "dialog" with the outputs of Command-A. Command-A Output: Updating FileA... (2 Replies)
Discussion started by: mharald
2 Replies

8. Shell Programming and Scripting

File redirection question

Hi all, I am working with the Grinder tool (unrelated to my question) to redirect the output of a program to a file as follows: java -cp $CLASSPATH net.grinder.TCPProxy > grinder.txt This is a proxy server which pipes output to a file. When I do something on my proxy, more and more goes to... (1 Reply)
Discussion started by: Annorax
1 Replies

9. Shell Programming and Scripting

input redirection question

Hi, in my script I need to execute the following command: query $id 456 432 but it waits for a RETURN character from keyboard and therefore, it fails. I tried something like: query $id 456 432 << '\n' but, i'ts clear it is not correct. Is there any way to do this? Thxs. (0 Replies)
Discussion started by: luistid
0 Replies

10. Shell Programming and Scripting

question on redirection (<<)

I came across the following problem, where file contents are overwritten using redirection. Can somebody please explain what cat << BAR seems to be doing and say why this is a problem? Explain the contents and relation between the two filenames used before the cat command. thanks /tmp# echo... (2 Replies)
Discussion started by: newkidintown
2 Replies
Login or Register to Ask a Question