The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Cronjob - Redirect mail output to file, LINES & COLUMNS jharvey Shell Programming and Scripting 0 04-14-2008 03:19 PM
Redirect grep output into file !!!!! hanu_oracle Shell Programming and Scripting 2 04-10-2008 09:02 AM
redirect output to log file kalyanraj Shell Programming and Scripting 2 06-07-2007 12:40 AM
Redirect output to a file jimmyc UNIX for Dummies Questions & Answers 11 01-31-2007 11:34 AM
redirect output to file? slackware Shell Programming and Scripting 1 09-12-2003 02:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-29-2006
xadamz23 xadamz23 is offline
Registered User
  
 

Join Date: Aug 2003
Posts: 31
redirect output to file

Hello,

I have a question about redirecting stdout and stderr. I have searched on Google and this forum and cant find the answer to my question.

Normally when I write scripts, I specify the redirection on each individual command like so:

echo hello > /some/file 2>&1
ls /home > /some/file 2>&1

What I want to do is have all the commands in my script output to a file without having to specify it on each individual command.

I know one solution is to just specify the redirection in the crontab like so:

* * * * * /some/script > /some/file 2>&1

I am just wondering if you can accomplish the same thing inside the script itself with some kind of global variable at the beginning of the script.

Anyone know if this is possible and how?
  #2 (permalink)  
Old 06-29-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
If I understand what you want try putting this near the top of the script:
Code:
exec 2>&1
  #3 (permalink)  
Old 06-29-2006
xadamz23 xadamz23 is offline
Registered User
  
 

Join Date: Aug 2003
Posts: 31
Quote:
Originally Posted by jim mcnamara
If I understand what you want try putting this near the top of the script:
Code:
exec 2>&1
I dont think that is the answer I am looking for but thanks.

Right now I am doing this: (just example commands)

Script:

echo hello > /some/file
ls /home > /some/file

What I want to do is this:

Script:

echo hello
ls /home

I dont want to have to specify on each command where the output should go. I want to globally define it somewhere so that all commands output is redirected to where ever I define it.
  #4 (permalink)  
Old 06-29-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Just put

Code:
exec >/some/file
near the beginning of the script.

Another thought -- if you are running multiple sequential commands and want them all redirected to /some/file, you could do:

Code:
{
command1
command2
command3
} > /some/file
  #5 (permalink)  
Old 06-29-2006
xadamz23 xadamz23 is offline
Registered User
  
 

Join Date: Aug 2003
Posts: 31
Thanks guys.

I didnt think the "exec" command would do what I wanted, but it does. Thanks again
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0