TIP: Click on subject to list as thread! ANSI
echo: fidosoft.husky
to: Alan Ianson
from: mark lewis
date: 2015-07-25 09:59:44
subject: CFRoute

24 Jul 15 19:17, you wrote to me:

 AI> I tried to redirect the output to a file to import here but I have
 AI> forgotten how to redirect STDERR. :)

there's several ways, really... i use one of two different methods with
some of our scripts... the easiest one is like this one we use when we
update sbbs from the repository...

  ./updatesbbs 2>&1 | tee /sbbs/updatesbbs_$(date +'%Y%m%d%H%M').log

the "2>&1" part redirects STDERR to STDOUT... that's what
you were asking about... all that output is then all piped to the
"tee" utility which displays everything on the screen as well as
logging it all to the named file... in this case, the named file contains
the date and time of the update script's execution... we could use

  ./updatesbbs > /sbbs/updatesbbs_$(date +'%Y%m%d%H%M').log 2>&1

to do the same thing without displaying any output to the screen... note
how the "2>&1" redirection is at the end of the line in
this one...

there are several other methods for the command line... i use/teach the
above because they are more indicative of what is being done... there are
some shorter methods available in some newer shells, though... this page on
bash shell redirection is a good one to bookmark and consult when necessary
;)

  http://www.tldp.org/LDP/abs/html/io-redirection.html


from within scripts, i use the following method so that i don't have to
remember or lookup the command line variant...


==== Begin "sample_logging_script"  ====
#!/bin/bash

# first set the logging and current directory variables
LOGFILE=$0.log
CWD=$PWD

# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
exec > >(tee $LOGFILE)

# Without this, only stdout would be captured - i.e. your
# log file would not contain any error messages.
exec 2>&1


## do your script work commands here
### set your script's necessary variables
VAR1=some_value
VAR2=another_value

### run your commands
cd somedir
run_some_command
cd some_other_dir
run_another_command
## end of script work commands


# return to the directory we were in when we started the script
cd $CWD
==== End "sample_logging_script" ====

i have the above in a file which i use as a template when i start a new
script... i just copy it to the new script's name and then edit the section
between the two "##" lines... this one sets the log file to the
same name as the name of the script with ".log" as the
extension... it could easily be adjusted to place the date and time in the
log's name as the above command line versions do...

hope this helps you and others following along ;)

)\/(ark

... Sevareid's Rule:  "The chief cause of problems is solutions."
---
* Origin: (1:3634/12.73)
SEEN-BY: 109/500 116/116 123/5 52 57 140 500 789 6502 124/5013 5014 135/371
SEEN-BY: 140/1 153/757 154/0 10 701 702 203/0 226/600 227/51 101 201 229/426
SEEN-BY: 230/0 240/1661 5832 249/303 261/38 280/464 5003 292/854 320/119
SEEN-BY: 322/759 342/11 423/120 633/267 280 640/384 712/550 848 770/1 3634/12
SEEN-BY: 3634/24 27 50
@PATH: 3634/12 123/500 154/10 280/464 712/848 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.