KSH problem - how do i redirect three times?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH problem - how do i redirect three times?
# 8  
Old 09-30-2008
Quote:
Originally Posted by mjays
the problem is, i need the script to exit if the ls command fails, but all it does is display the error and then write to the log file.

this is the ls command and the code after it

Code:
ls -lR ${BUILD_CODE} 2>&1 >${REL_FILES}/${RELNO}.txt | tee -a ${LOGFILE}
if [ $? -ne 0 ]
   then
       exit
fi

so you see, the tee command is probably successful which gives me a $status of 0.

how do i interogate the error from the ls command when it's tucked in way back there?
In bash, you can turn on the option "pipefail", and $? will return the result of the last command to fail in the pipeline (otherwise, it will return the result of the last command OF the pipeline). Use "set -o pipefail" to turn this feature on. Observe:
Code:
false | cat; echo $?
0
$ set -o pipefail
$ false | cat; echo $?
1

Otherwise, you have to do tricks like:
Code:
/bin/ls $FILE >$TMP_FILE_2 2>&1
res=$?
cat $TMP_FILE_2 >>$LOGFILE
if [ $res -ne 0 ]; then 
  exit
fi

# 9  
Old 09-30-2008
Quote:
Originally Posted by otheus
In bash, you can turn on the option "pipefail", and $? will return the result of the last command to fail in the pipeline (otherwise, it will return the result of the last command OF the pipeline). Use "set -o pipefail" to turn this feature on. Observe:
Code:
false | cat; echo $?
0
$ set -o pipefail
$ false | cat; echo $?
1

Otherwise, you have to do tricks like:
Code:
/bin/ls $FILE >$TMP_FILE_2 2>&1
res=$?
cat $TMP_FILE_2 >>$LOGFILE
if [ $res -ne 0 ]; then 
  exit
fi

yeah, i've already considered the extra check to see if the file was created. i was hoping there was a more direct solution using a method i've never seen before. a super advanced system admin/unix expert method.

Last edited by mjays; 09-30-2008 at 11:36 AM.. Reason: correcting a typo.
# 10  
Old 09-30-2008
Quote:
Originally Posted by mjays
yeah, i've already considered the extra check to see if the file was created. i was hoping there was a more direct solution using a method i've never seen before. a super advanced system admin/unix expert method.
Odd, is it not?

So can you use bash? Maybe ksh has a similar option?
# 11  
Old 09-30-2008
Quote:
Originally Posted by mjays
a super advanced system admin/unix expert method.
;-))))

In fact there is and i am glad to be able to explain - once in a lifetime - "super advanced system admin expert methods" to the audience: You can redirect output streams with the "exec" command:

exec 2>output.stderr

Will redirect every output of every subsequent command to output.stderr.

This way you do not have to worry about truncating the file at all:


Code:
exec 2>output.stderr     # truncates the file and redirects stderr to it
job1
job2
job3
exec 2>&-                # closes stderr and removes the redirection

is equivalent to

Code:
job1 2> output.stderr
job2 2>> output.stderr
job3 2>> output.stderr

I hope this helps.

bakunin
# 12  
Old 10-01-2008
Quote:
Originally Posted by bakunin
;-))))

In fact there is and i am glad to be able to explain - once in a lifetime - "super advanced system admin expert methods" to the audience: You can redirect output streams with the "exec" command:

exec 2>output.stderr

Will redirect every output of every subsequent command to output.stderr.

This way you do not have to worry about truncating the file at all:


Code:
exec 2>output.stderr     # truncates the file and redirects stderr to it
job1
job2
job3
exec 2>&-                # closes stderr and removes the redirection

is equivalent to

Code:
job1 2> output.stderr
job2 2>> output.stderr
job3 2>> output.stderr

I hope this helps.

bakunin
erm...where does that fit in with my bit of code?

Code:
ls -lR ${BUILD_CODE} 2>&1 >${REL_FILES}/${RELNO}.txt | tee -a ${LOGFILE}
if [ $? -ne 0 ]
   then
       echo "ERROR - release file creation failed 
       exit
fi

i've tried not to be stupid and figure out how to use your solution but i give in.
# 13  
Old 10-01-2008
"super advanced system admin expert methods" means writing answers without first understanding the question. Smilie

Seriously, at the *beginning* of the script, you could do something like this:

Code:
exec >standard.out 2>error.out

Every time there's an error, you stop the script and output the error.out file. However, it doesn't deal with automatically printing the normal output. However, you could go to another terminal and run:
Code:
tail -f standard.out

but I don't think that suits your purpose.
# 14  
Old 10-01-2008
you are using err file (redirecting the error to stderr)wright?
lets take

ls data >err 2>&1 | tee -a somefile
if [ ! -s err ]
then
{do whatever you want}
fi

here as per the code the err file will load with some error message when failure occurs in ls.
is it feasible?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

2. Shell Programming and Scripting

Print one's place for 1 to N times, ksh Perl whatever?

Hello all, I would like to create a for loop or whatever is quick that will print the one’s place of a number for 1-N times say for example a printed page formatting is 132 characters wide, I would like a single line 123456789012345678901234567890... ...012 That is 132 characters long. I... (11 Replies)
Discussion started by: KmJohnson
11 Replies

3. Shell Programming and Scripting

Problem with << redirect - Please help!

I have a script to send an email like below. Problem is, the if ..fi block is not getting executed, and is coming as a part of the email body. Can anyone take a look at this? :confused: Log file shows this: SEND_MAIL.prog: line 64: : command not found echo "Input Parameters" echo... (11 Replies)
Discussion started by: mansmaan
11 Replies

4. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

5. Shell Programming and Scripting

Redirect Problem

Hi, I have perl script which is calling an external command using "system()" with argument. But i am not able to capture the output.Even tried with backtick also with no luck. . . $number=<>; system ("cmd $number >output.txt"); (2 Replies)
Discussion started by: rasingraj
2 Replies

6. Shell Programming and Scripting

KSH - mailx - Redirect the undelivered mail

Hi, I need to create one KSH which will send mail to set of recipients using "mailx" command like below. mailx -s "Test mail" "test@yahoo.com, test@gmail.com" <$output.txt The recipients are in different domains (like yahoo, gmail, etc.). My requirement is, if any mail is undelivered,... (1 Reply)
Discussion started by: Matrix2682
1 Replies

7. Solaris

Cron redirect problem

Hello all, I have a script that I am trying to execute and redirect the output to a file, but I have trouble in redirection. The cron job is running properly as I see it in the mail. This is what I am doing In crontab file, 0 4 * * * somescript.sh > /some_location/`date '+%m%d%y_%H%M'`.log... (9 Replies)
Discussion started by: grajp002
9 Replies

8. Shell Programming and Scripting

Ksh riddle: interpret variable two times?

exam is a ksh script. In command line I enter: exam 3 param_2 param_3 param_4. In exam how can I get the value of the parameter which position is specified by the first argument. Simply doing this DOES NOT work: offset=$1 value=$$offset can you figure out any possible way to interpret a... (5 Replies)
Discussion started by: i27oak
5 Replies

9. Shell Programming and Scripting

Redirect within ksh

I am using ksh on an AIX box. I would like to redirect the stdout and stderr to a file but also show them on the terminal. Is this possible? I have tried tee within my file without success. This is the code I have so far exec > imp.log 2>&1 | tee exec 1>&1 I am new to shell scripting, so... (3 Replies)
Discussion started by: podzach
3 Replies

10. Shell Programming and Scripting

Redirect Problem

I am using the time command in a script however the output of the time command will display on my screen but not my output file. Any Ideas on how to fix this? > cat test.sh ############################# #!/usr/bin/sh for COMMAND in pwd do time ${COMMAND} done | sed "s/^/ ... (4 Replies)
Discussion started by: 2dumb
4 Replies
Login or Register to Ask a Question