Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-04-2005, 04:19 PM
|
#1
|
Member
Registered: Nov 2004
Location: Boston
Posts: 30
Rep:
|
Is there a way in Linux/Unix bash to turn off output or redirect to a file
Hello,
I am currently writting a bash script in which I would like to have the ability to run in a silent mode. I come from the windows world and I was hoping there was something like echo ON/echo OFF type command.
Along with this option I would to log everything that was supposed to go to standard out to go to a file. I tried to use the commands "script filename" and "tee filename" but they don't seem to work correctly.
Any help anyone can provide will be greatly appreciated.
Thank you,
-Jim
|
|
|
01-04-2005, 04:22 PM
|
#2
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
If you want to run a script and direct output to a file it's:
./script_name > scriptlog.txt
If you run a script and wish to append output to a file it's:
.,/script_name >> scriptlog.txt
-twantrd
|
|
|
01-04-2005, 04:28 PM
|
#3
|
Senior Member
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973
Rep:
|
redirect it to /dev/null
|
|
|
01-05-2005, 12:28 AM
|
#4
|
LQ Newbie
Registered: Dec 2004
Posts: 11
Rep:
|
after the command give the redirect to any file.
like ls > null
this will direct the output of ls to a file named null.
|
|
|
01-05-2005, 01:41 AM
|
#5
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Here's how you do it from the script itself.
"echo off" style:
Code:
#!/bin/bash
# redirect stdout(1) and stderr(2) to null:
exec 1>/dev/null 2>/dev/null
...the remainder of your script...
logfile style:
Code:
#!/bin/bash
# redirect stdout(1) and stderr(2) to logfile:
exec 1>/path/to/logfile 2>&1
...the remainder of your script...
Yves.
Last edited by theYinYeti; 01-05-2005 at 01:43 AM.
|
|
|
01-05-2005, 01:51 PM
|
#6
|
Member
Registered: Nov 2004
Location: Boston
Posts: 30
Original Poster
Rep:
|
RE: Is there a way in Linux/Unix bash to turn off output or redirect to a file
Hello all,
I really appreciate everyones responses to my inquire. I think Yves is the route I am going to take, but I was wondering if there is a way to redirect stdout and stderr to screen as well as a file?
Best Regards,
-Jim
|
|
|
01-06-2005, 03:56 AM
|
#7
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
In theory (but I did not try):
Code:
#!/bin/bash
# redirect stdout(1) and stderr(2) to logfile and stdout:
exec 1> >(tee /path/to/logfile) 2>&1
...the remainder of your script...
Yves.
|
|
|
All times are GMT -5. The time now is 06:27 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|