Last active
June 6, 2024 18:40
-
-
Save alfeugds/4d6e0d7c1bacef1b8a6ae9b327013d05 to your computer and use it in GitHub Desktop.
Bash utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################# | |
# log it! | |
# | |
# Use it to log a command to a file. Useful when you run commands in the terminal and want to keep history of its output. | |
# | |
# usage: log_it [args] | |
# | |
# example: log_it df -h | |
# | |
# the above command will add both stdout and stderr to a log file called 2024-02-20_16-08-53-df_-h.log | |
################# | |
log_it() { | |
DATE=$(date '+%Y-%m-%d_%H-%M-%S') | |
log_file_suffix=$(echo "$@" | sed -e 's/[^A-Za-z0-9._-]/_/g') | |
log_file="$DATE-$log_file_suffix.log" | |
echo "running command: $@" | tee -a "$log_file" | |
echo "logging on $log_file" | tee -a "$log_file" | |
echo "--" | tee -a "$log_file" | |
# stdout and stderr | |
$@ 2>&1 | tee -a "$log_file" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's cool!