根據我的理解,您需要在線限制輸出(在生成時)。 這是一個我能想到的功能,對你有用。
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' –