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 |
this would send any errors during "make" to a text file:
Code:
make 2> errors.txt |
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 |
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:
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. |
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 ? |
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. |
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. |
All times are GMT -5. The time now is 04:06 AM. |