LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2005, 07:55 AM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
shell script q's (io, error exiting )


Hi
I have a few questions about bash scripting.

1. how can i send output to stdout and redirect same output to a log file in the same line of code ??

2. I have a script which runs various commands and compiles programs, whats the most efficient piece of code to attempt to run/compile the program and in case of error to write a error message to stout, and the log and then redirect stderror to the log. In the case there is no problem i want the script to carry on running

thanks
 
Old 02-14-2005, 08:55 AM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
this would send any errors during "make" to a text file:

Code:
make 2> errors.txt
 
Old 02-14-2005, 08:57 AM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
a handy tool for your first problem is 'tee'. 'tee' takes standard input, and prints it to standard output and a file you specify. eg.

cat file.txt | tee file2.txt

by redirecting the stdout of cat to stdin of tee with a pipe, tee will write stdin to stdout and a file named file2.txt
 
Old 02-14-2005, 08:58 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

1) Use the tee command:

command | tee outfile : This will sent stdout to terminal and to outfile

If you want to redirect stderr to outfile too:

command 2>&1 | tee outfile

2) I'm not sure what it is you want:

Quote:
write a error message to stout, and the log and then redirect stderror to the log
Maybe the following does what you want:

command 1>/tmp/out 2>&1 || { echo "Oops..." ; cat /tmp/out ; exit 1 ; }
command2


The output (stdout and stderr) is redirected to /tmp/out. If command fails everything after || is executed, if command is
ok then command 2 will be executed.

If the part after || is executed the following happens:

An 'error' is displayed ( echo "Oops..." ),
All the output is shown ( cat /tmp/out ), remember both stderr and stdout are in this file,
The script exits with an appropriate exit code (1 in this case).

Hope this helps.
 
Old 02-14-2005, 10:37 AM   #5
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
Hi, thanks for the replies -especially druuma

Finally i used tee with echo and ||. Just as a side note what do you all use for shell scripting ?? Im using vim now, but debugging is a pain, what do you all recommend ?
 
Old 02-14-2005, 10:48 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I'm a vi(m) addict, so there's nothing else for me

To make life a bit easier you can (if it isn't already) turn on syntax support and highlighting in vim. It's not real dubugging, but it does show incomplete/wrong syntax while you type.

The set -x switch that comes with bash/ksh can also help during test execution of the new script.

Again, hope this helps.
 
Old 02-17-2005, 01:24 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If i'm really stuck on a bash script I use
set -xv
at the top:
-v Print shell input lines as they are read.
-x After expanding each simple command, for command, case
command, select command, or arithmetic for command,
display the expanded value of PS4, followed by the com-
mand and its expanded arguments or associated word
list.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing JBuilder, but get error regarding Shell Script Sir Nerdalot Linux - Software 1 07-12-2005 09:21 AM
Exiting the Shell script with Error MEssage sumitarun Programming 5 04-14-2005 01:10 AM
Help With A Shell Script Error PhantomFX Linux - Newbie 3 02-17-2005 04:42 AM
Shell Script Error Gerardoj Linux - General 2 12-03-2003 09:44 AM
Shell script error. chuck77 Linux - General 3 11-29-2001 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration