I need help sending the output (stdin and stdout) from system commands to a bash function, while still accepting input from arguments. Something like the example that follow
Thanks to people who posted their responses. I came up with my version which will add timestamp once per message.
#!/bin/bash
CURRENT_PID=$$
PROCESS_NAME=$(basename $0)
LOGFILE=/var/log/backup-monitor.log
function log_message {
if [ -n "$1" ]; then
MESSAGE="$1"
echo -e "$(date -Iseconds)\t$PROCESS_NAME\t$CURRENT_PID\t$MESSAGE" | tee -a $LOGFILE
else
MESSAGE=$(tee)
echo -e "$(date -Iseconds)\t$PROCESS_NAME\t$CURRENT_PID\t$MESSAGE" | tee -a $LOGFILE
fi
}
log_message "Direct arguments are working!!"
echo "stdin also working" | log_message