| 1 | #!/bin/bash |
| 2 | # reassign-stdout.sh |
| 3 | |
| 4 | LOGFILE=logfile.txt |
| 5 | |
| 6 | exec 6>&1 # Link file descriptor #6 with stdout. |
| 7 | # Saves stdout. |
| 8 | |
| 9 | exec > $LOGFILE # stdout replaced with file "logfile.txt". |
| 10 | |
| 11 | # ----------------------------------------------------------- # |
| 12 | # All output from commands in this block sent to file $LOGFILE. |
| 13 | |
| 14 | echo -n "Logfile: " |
| 15 | date |
| 16 | echo "-------------------------------------" |
| 17 | echo |
| 18 | |
| 19 | echo "Output of \"ls -al\" command" |
| 20 | echo |
| 21 | ls -al |
| 22 | echo; echo |
| 23 | echo "Output of \"df\" command" |
| 24 | echo |
| 25 | df |
| 26 | |
| 27 | # ----------------------------------------------------------- # |
| 28 | |
| 29 | exec 1>&6 6>&- # Restore stdout and close file descriptor #6. |
| 30 | |
| 31 | echo |
| 32 | echo "== stdout now restored to default == " |
| 33 | echo |
| 34 | ls -al |
| 35 | echo |
| 36 | |
| 37 | exit 0 |