Надеюсь, кому-то станет лучше.
Сначала я использовал команду ts
из moreutils
.
#!/usr/bin/env bash
command -v ts > /dev/null 2>&1 [[ $? -ne 0 ]] && { sudo apt install moreutils -y; };
log="./testlog.txt"
comm()
{
printf "Hi\n";
echo "here is a wrong command"
csdf;
echo "bye"
}
exec 3>&1 1>>${log} 2>&1;
comm | tee /dev/fd/3 | ts;
И безts
:
#!/usr/bin/env bash
log="./testlog.txt"
comm()
{
printf "Hi\n";
echo "here is a wrong command"
csdf;
echo "bye"
}
dddate()
{
while IFS= read -r line; do
printf '%s %s\n' "$(date)" "$line"
done
}
exec 3>&1 1>>${log} 2>&1;
comm | tee /dev/fd/3 | dddate;
в файле журнала:
./test.sh: line 10: csdf: command not found
Mon Jul 8 12:23:44 UTC 2019 Hi,
Mon Jul 8 12:23:44 UTC 2019 here is a wrong command
Mon Jul 8 12:23:44 UTC 2019 thanks
В телетайпе:
Hi,
here is a wrong command
thanks