echo to stdout and append to file

六眼飞鱼酱① 提交于 2021-01-27 05:49:58

问题


I have this:

echo "all done creating tables" >> ${SUMAN_DEBUG_LOG_PATH}

but that should only append to the file, not write to stdout. How can I write to stdout and append to a file in the same bash line?


回答1:


Something like this?

echo "all done creating tables" | tee -a  "${SUMAN_DEBUG_LOG_PATH}"



回答2:


Use the tee command

$ echo hi | tee -a foo.txt
hi
$ cat foo.txt
hi



回答3:


Normally tee is used, however a version using just bash:

#!/bin/bash

function mytee (){
    fn=$1
    shift
    IFS= read -r LINE
    printf '%s\n' "$LINE"
    printf '%s\n' "$LINE" >> "$fn"
}


SUMAN_DEBUG_LOG_PATH=/tmp/abc
echo "all done creating tables" | mytee "${SUMAN_DEBUG_LOG_PATH}"


来源:https://stackoverflow.com/questions/44576935/echo-to-stdout-and-append-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!