Hãy thử kịch bản dưới đây. Nó sẽ phát hiện kích thước của thiết bị đầu cuối cho mỗi từ đầu vào, do đó thậm chí sẽ tự động cập nhật nếu bạn thay đổi kích thước thiết bị đầu cuối trong khi nó đang chạy.
#!/usr/bin/env bash
## Change the input file to have one word per line
tr ' ' '\n' < "$1" |
## Read each word
while read word
do
## Get the terminal's dimensions
height=$(tput lines)
width=$(tput cols)
## Clear the terminal
clear
## Set the cursor to the middle of the terminal
tput cup "$((height/2))" "$((width/2))"
## Print the word. I add a newline just to avoid the blinking cursor
printf "%s\n" "$word"
sleep 1
done
Lưu nó dưới dạng ~/bin/foo.sh
, làm cho nó có thể thực thi được ( chmod a+x ~/bin/foo.sh
) và cung cấp cho nó tệp đầu vào của bạn làm đối số đầu tiên:
foo.sh file