I am running some commands in a function. I would like to take logs of it by adding date and time for every command output of that function. However, in the terminal when executed, I want to show only the main normal output without any date or time.
So, how can I pipe all of it exec 3>&1 1>>${log} 2>&1
via adddate
function like
./script | adddate >>$log
so that I can get date
in my log output at each line, but at the same time I can show the output on the terminal.
I do not want to show date and time in tty, I want to save them only in log. I am doing it because I want to catch all the invalid parameters may have been passed by users at any specific time so on and so forth but at the same time, I want to show the output to the user who executed the script to his tty.
So far, I have gone this far as follows:
#!/usr/bin/env bash
log=logs.txt
exec 3>&1 1>>${log} 2>&1 ## works but without date
then I tried something like this shown below. Not really super clear how to execute it properly
3>$1 1>> | adddate ${log} 2>&1 ###(!) doesnt work
exec 3>&1 | adddate 1>> ${log} 2>&1 ###(!) doesnt work
adddate
, main
function looks like this:
adddate() {
while IFS= read -r line; do
printf "%s %s\n" "$(date)" "$line";
done
}
main()
{
case $arg in
--version)
[[ "$arg2" == "" ]] && { --version; } || { error; } ;;
build)
[[ "$arg2" == "oneshot" ]] && {
build oneshot; } || {
build;
} ;;
update)
[[ "$arg2" == "" ]] && { update; } || { error; } ;;
*)
error
esac
}
arg=$1;
arg2=$2;
shift;
main | tee /dev/fd/3; ## so that it goes to tty, not sure if there is other better way?!?
So, in short: logs should be something like the following: for each command, there should be the date and time written in the log before the command output for a main
function like
main()
{
printf "Hello\n";
printf "there!\n";
}
the log out should be:
Mon Jun 17 20:00:02 JST 2019 Hello
Mon Jun 17 20:00:02 JST 2019 there!
and tty should not show logs but only show the main output, which is for example:
Hello
there!
logs.txt
and what should be shown in the tty? Ideally, show us a minimal example of your script, including someecho
calls and tell us exactly what you want to see as output. – terdon Jun 17 '19 at 10:42