Today I show how to add a timestamp to your log files in your bash scripts. That is easy. In this solution we have to include the function
adddate() {
while IFS= read -r line; do
echo "$(date) $line"
done
}
in your script file and add pipe the command to the function using the systax:
command | adddate
For example:
#!/bin/bash
adddate() {
while IFS= read -r line; do
echo "$(date) $line"
done
}
echo -e "Doing something"
ls -la | adddate
No comments:
Post a Comment