根据我的理解,您需要在线限制输出(在生成时)。 这是一个我能想到的功能,对你有用。
limit_output() {
FullLogFile="./output.log" # Log file to keep the input content
typeset -i MAX=15 # number or lines from head, from tail
typeset -i LINES=0 # number of lines displayed
# tee will save the copy of the input into a log file
tee "$FullLogFile" | {
# The pipe will cause this part to be executed in a subshell
# The command keeps LINES from losing it's value before if
while read -r Line; do
if [[ $LINES -lt $MAX ]]; then
LINES=LINES+1
echo "$Line" # Display first few lines on screen
elif [[ $LINES -lt $(($MAX*2)) ]]; then
LINES=LINES+1 # Count the lines for a little longer
echo -n "." # Reduce line output to single dot
else
echo -n "." # Reduce line output to single dot
fi
done
echo "" # Finish with the dots
# Tail last few lines, not found in head and not more then max
if [[ $LINES -gt $MAX ]]; then
tail -n $(($LINES-$MAX)) "$FullLogFile"
fi
}
}
在脚本中使用它,将其加载到当前shell或放入.bash_profile以在用户会话中加载。
使用示例:cat /var/log/messages | limit_output
或./configure | limit_output
功能将读取的标准输入,将其保存到日志文件,显示第一MAX线,然后在屏幕上每行减少到一个点,然后(。)最后显示最后的MAX行(如果输出短于MAX * 2,则输出更小)。
'[-f command.log.gz] && gunzip command.log.gz; somecommand> tmp &&(($(wc -l 100))&& {head -n100 tmp; tail -n100 tmp;} || cat tmp; cat tmp >> command.log; gzip command.log; rm tmp' –