...
- redirect standard output to a file, overwriting any exiting contents:
echo "Output text" > out.txt
echo "Output
text" 1> out.txt - redirect standard output to a file, appending to any exiting contents:
echo "More text" >> out.txt
echo "More text" 1>> out.txt - redirect standard error output to a file, overwriting any exiting contents:
echo "Error
text" 2> err.txt - redirect standard error to standard output:
ls ~ xxxx > ls.log 2>&1
- redirect standard output to standard error:
echo "
Output that will go to standard error
" 1>&2
...
Code Block | ||
---|---|---|
| ||
#!/bin/bash # Script version global variable. Edit this whenever changes are made. __ADVANCED_BASH_VERSION__="step_02" # ======================================================================= # Helper functions # ======================================================================= # Echo's its arguments to std error echo_se() { echo "$@" 1>&2; } # Sets up auto-logging to a log file in the current directory # using the specified logFileTag (arg 1) in the log file name. auto_log() { local logFileTag="$1" if [[ "$logFileTag" != "" ]]; then local logFilePath="./autoLog_${logFileTag}.log" echo_se ".. logging to $logFilePath" exec 1> >(tee "$logFilePath") 2>&1 else echo_se "** ERROR in autoLog: no logFile argument provided" exit 255 fi } # ======================================================================= # Command processing functions # ======================================================================= # function that says "Hello World!" and displays user-specified text. function helloWorld() { local txt1=$1 local txt2=$2 shift; shift local rest=$@ echo "Hello World!" echo " text 1: '$txt1'" echo " text 2: '$txt2'" echo " rest: '$rest'" } # function that displays its 1st argument on standard output and # its 2nd argument on standard error function stdStreams() { local outTxt=${1:-"text for standard output"} local errTxt=${2:-"text for standard error"} echo "to standard output: '$outTxt'" echo_se "to standard error: '$errTxt'" } # function that illustrates auto-logging and capturing function output # arg 1 - (required) tag to identify the logfile # arg 2 - (optional) text for standard output # arg 3 - (optional) text for standard error function testAutolog() { local logFileTag="$1" local outTxt=${2:-"text for standard output"} local errTxt=${3:-"text for standard error"} auto_log "$logFileTag" echo -e "\n1) Call stdStreams with output and error text:" stdStreams "$outTxt" "$outErr" echo -e "\n2) Capture stdStreams output in a variable and display it:" local output=`stdStreams "$outTxt" "$outErr"` echo -e "\tstdStreams output was:\n$output" echo -e "\n3) Call echo_se with some text:" echo_se "Some random text" echo -e "\n4)Capture echo_se function output in a variable and display it:" output=`echo_se "Some random text"` echo -e "echo_se output was: '$output'" } # ======================================================================= # Main script command-line processing # ======================================================================= function usage() { echo " advanced_bash.sh, version $__ADVANCED_BASH_VERSION__ Usage: advanced_bash.sh <command> [arg1 arg2...] Commands: helloWorld [text to display] stdStreams [text for stdout] [text for stderr] testAutolog <logFileTag> [text for stdout] [text for stderr] " exit 255 } CMD=$1 # initially $1 will be the command shift # after "shift", $1 will be the 2nd 1stcommand-line argargument; $2 the 2nd3rd, etc. case "$CMD" in helloWorld) helloWorld "$@" ;; stdStreams) stdStreams "$1" "$2" ;; testAutolog) testAutolog "$1" "$2" "$3" ;; *) usage ;; esac |
...
- exec 1> causes redirection of all standard output for the duration of the current shell environment (here the script).
- the 2>&1 at the end is our normal "redirect standard error to standard output" idiom
- >(tee "my_logfile.log") is the magic that says to write standard output (which now includes standard error text) to a file via tee.
- the >( ) syntax also sends standard output to a sub-shell (more on this later), which appears to be necessary because otherwise tee would just act on its piped-in standard input.
...
If you arrange for such a tag to be uniquely associated with a specific invocation of a script (e.g. the output file prefix used for all Fastq FASTQ alignment files), this will create a uniquely named log file for each different script invocation (. This is useful, for example, when calling the same script with different arguments in a file of batch commands at TACC).
Importantly, auto_log reports an error and terminates script execution if no tag string is specified (we'll see much more on error handling shortly).
...
So why is echo -e used in the testAutoLog function? The Because the -e argument to echo enables interpretation of backslash escapes. For example, "\n" will be interpreted as a newline, and "\t" as a tab character.
exercise 3
...