Visit The New, Modern Unix Linux Community


ksh Logging


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers ksh Logging
# 1  
ksh Logging

Hi,

I want to run my script and want to ensure it captures all logging.

For e.g I got this from another forum

Code:
# Redirect the current stderr into stdout
exec 2>&1
# Redirect the current stdout into the log file
exec 1>timer_log.log

The script will print stderr onto the putty terminal and log all script output into timer_log.log
How can I also print the stdout to the putty terminal too.
I am using a KSH script.

Thanks.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-21-2019 at 05:45 AM.. Reason: Added CODE tags.
# 2  
I was thinking about going through your commands one step after the other, assuming both stdout and stderr point to /dev/tty originally. But then I found this is completely covered in man bash:
Quote:
Note that the order of redirections is significant. For example, the command

ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.
Do you see the parallel / analogy?
# 3  
I was going to say:
Do the exec's in the other order:
Code:
exec 1>timer_log.log
exec 2>&1

or just use one exec:
Code:
exec 1>timer_log.log 2>&1

and since 1 is the default output file descriptor, one can shorten that to:
Code:
exec >timer_log.log 2>&1

but RudiC beat me to it. Note, however, that since the question is about ksh (not bash), one should probably read the ksh man page (instead of the bash man page) where it might say something like:
Quote:
... ... ... For example:

... 2>&1

means file descriptor 2 is to be opened for writing as a duplicate of
file descriptor 1 and

exec {n}<file

means open file named file for reading and store the file descriptor
number in variable n.

The order in which redirections are specified is significant. The
shell evaluates each redirection in terms of the (file descriptor,
file) association at the time of evaluation. For example:

... 1>fname 2>&1

first associates file descriptor 1 with file fname. It then associates
file descriptor 2 with the file associated with file descriptor 1 (i.e.
fname). If the order of redirections were reversed, file descriptor 2
would be associated with the terminal (assuming file descriptor 1 had
been) and then file descriptor 1 would be associated with file fname.

If a command is followed by & and job control is not active, then the
default standard input for the command is the empty file /dev/null.
Otherwise, the environment for the execution of a command contains the
file descriptors of the invoking shell as modified by input/output
specifications.
But since the basic redirections are the same in ksh and bash, either man page covers this issue. However, if the question were about redirecting output to a co-process or to a specific socket on a server, one should definitely use the man page for the shell that is being used.
# 4  
In addition to what RudiC already said: if you want to redirect larger parts of a script i would - simply for better readability - not shift around output streams in a way so that one depends on the other. I would set them separately and independently like this:

Code:
exec 2>/some/file
exec 1>/some/file

If you later decide to redirect one or the other to somewhere else it won't affect the other redirecction at all.

I hope this helps.

bakunin
# 5  
Quote:
Originally Posted by bakunin
In addition to what RudiC already said: if you want to redirect larger parts of a script i would - simply for better readability - not shift around output streams in a way so that one depends on the other. I would set them separately and independently like this:

Code:
exec 2>/some/file
exec 1>/some/file

If you later decide to redirect one or the other to somewhere else it won't affect the other redirecction at all.

I hope this helps.

bakunin
Hi bakunin,
Sorry, but I must strongly disagree with your suggestion.

Opening these two files this way creates two file descriptors pointing at the same file with both writing data starting at position 1 and destructively overwriting each other's previously written data. For example the script:
Code:
#!/bin/ksh
exec 2>/some/file
exec 1>/some/file
echo ab
echo oops >&2
echo cd

ends up with the following stored in the file named /some/file:
Code:
oopcd

These 2 Users Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #501
Difficulty: Easy
If a piece of code adds two numbers together and stores the result, there should be a comment that explains the process in the source code.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Shell Programming and Scripting

Adding logging feature to ksh script?

Good morning all, I have a ksh script that I would like to log all "Terminal" out to a log file. I have tried several different methods to no avail! Below is my script and any advice or assitance would be great! The script works great but can not figure out how to log all outputs to a log... (4 Replies)
Discussion started by: TAllison
4 Replies

3. Shell Programming and Scripting

ksh - find command with 2 actions attached and error logging

Hi there, I'm encountering problems on an AIX system when using following in my script. find . -name *.edi -type f -exec sh -c 'scp {} $user@$server:$path || exit 5; mv {} $sent || exit 7' \; the error i get is following find: 0652-018 An expression term lacks a required... (4 Replies)
Discussion started by: Kerberos
4 Replies

4. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Constant Logging In (After Logging Out)

Hi Everyone. First, I want to thank all of you for letting me participate in this great group. I am having a bit of a problem. After I get an email from a responder, I login to make my reply. In the mean time I get another response by email from another member, I go to reply to them and I... (6 Replies)
Discussion started by: Ccccc
6 Replies

6. UNIX for Dummies Questions & Answers

need more logging

Hi all! On our current Solaris 8 machine we only have "standard" logging configured, and now i need to put on more. What i specificly need is time in the logfiles. Ex. When a user is logging in, when a user makes it self SU. etc. Regards... dOzY (3 Replies)
Discussion started by: dozy
3 Replies

7. UNIX for Dummies Questions & Answers

Asking about logging in

Hi, just wriiten a sh script and as my script will try to log into another server to delete some files but when i run , it keeps on saying that my files do not exist. It seems to refer to my local directory instead. Below is my script : FTP_HOST=ip_number FTP_USER="user password" ... (1 Reply)
Discussion started by: blueberry80
1 Replies

8. UNIX for Dummies Questions & Answers

logging when someone changes to su

Is there a file that captures info whenever someone logs to su? i know it comes across as standard output on the server, but is it saved anywhere? Time and date info included?? thanks, kym (1 Reply)
Discussion started by: kymberm
1 Replies

9. Shell Programming and Scripting

Logging

G'day Just wondering if anyone out there knows how to log files, using the example I provided in the earlier message / question earlier today: :confused: If I was to backup a file, how could I setup a log file to record the filename, date (This one I've got figured), and that the file was... (4 Replies)
Discussion started by: Aussie_Bloke
4 Replies

10. UNIX for Dummies Questions & Answers

Logging

I have a SCO R5 Open Server Box running at a remote location, and from time to time it seems to "spontaneously" re-boot itself. Is there a specific log file that I can examine to see why the machine is doing this ? Any suggestions gratefully appreciated (1 Reply)
Discussion started by: pcs7088
1 Replies

Featured Tech Videos