I decided to log my commands for good record keeping. Here’s my script.
#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi log=log.txt echo '-------------------------------' >> $log date >> $log echo '---' >> $log cat $1 >> $log echo '---' >> $log /bin/bash $1 2>&1 | tee -a $log |
To run.
./go.sh script.sh |
Line by line explanation.
- Checks for script to run. Exits if there’s no argument.
- Sets the log file.
- Prints dashes.
- Prints the date.
- Print 3 dashes.
- Prints the commands to the log file.
- Print 3 dashes.
- Run the command. Send error and standard out to log file.