
此問題針對 AIX 7
我一直在研究一個可以顯示幾頁文字的顯示器。最初,最終用戶想要一個滾動列表,我為此構建了以下內容:
IFS=''; cat plfeed | while read line; do echo $line; perl -e 'select(undef,undef,undef,.8)'; done
最終使用者決定他們寧願在設定的時間(例如 20 秒)內顯示一頁(24 行)輸出。我知道 more 可以讓我一次顯示一個頁面,但它需要鍵盤輸入,這對於我的用例來說是不可接受的。
太棒了;
如何自動化“更多”命令,或建立一個類似的函數,在頁面之間休眠然後自動前進?
答案1
這個相當標準awk
在 AIX 上應該沒問題
awk '{if(NR>1 && NR%24==1)system("sleep 20");print}'
正如評論中提到的,如果你想在中斷時退出,你可以替換system()
為
{if(system("sleep 20"))exit}
但它可能不適用於您的作業系統。
答案2
#!/usr/bin/env expect
set timeout 20
spawn -noecho man autoexpect
while 1 {
expect {
timeout { send " " }
-ex "(END)" { exit }
}
}
答案3
awk
這使用類似的解決方案解決了性質OP的問題嗯。我做了以下更改:
- Ctrl通過+退出c。
- 用於
$LINES
取得終端的高度。 - 適用於 Linux 和 Mac OSX。
- 新增了文件和解釋。
awk -v x=$LINES 'NR % x == 0 && system("sleep 20"){exit} 1'
# ^^^^^^^^^^ ^ ^^^^^^^^ ^ ^
# | | | | |
# | | | | |
# | | | | +
# | | | | f) pattern-action block which
# | | | | prints the current line.
# | | | | - Pattern is Truethy.
# | | | | - Action is empty
# | | | | defaulting to `{print}`
# | | | |
# | | | +
# | | | d) `system` function returns exit code `0` when
# | | | successful and non-zero on 'ctrl-c'.
# | | |
# | | | e) `0` evaluates to false, so `exit` will not
# | | | execute until `ctrl-c` is triggered.
# | | +
# | | c) When line number is evenly divisible
# | | by x (the terminal height)
# | | sleep for 1 second.
# | |
# | |
# | +
# | b) NR current line number.
# |
# +
# a) Set variable `x` to Bash variable $LINES.
# $LINES is set to height of current terminal.