Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/bash
- # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
- # 脚本名称: blink.sh #
- # 脚本用途: 打字机输出仿真 #
- # 开发作者: 木哥 #
- # 当前版本: 1.0 #
- # 更新日志: <版本号> | <更新时间> | <更新内容> #
- # 1.0 | 2023-06-05 23:00:00 | 脚本初始化 #
- # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
- SCALES=2000
- DELAY_SLEEP=200 # 光标闪烁的时间间隔
- DELAY_START=1000 # 输入前等待时间
- DELAY_END=2000 # 输入完毕后等待时间
- # 打字间隔最高时间随机取值
- DELAY_TYPES=(2 2 3 3 5 5 5 8 8 8 8 10 10 10 10 10 10 20 20 20)
- # 控制并发命名管道
- lock_fifo="/tmp/blink_$$.lock.fifo"
- mkdir -p $(dirname "$lock_fifo")
- mkfifo $lock_fifo
- exec 10<>$lock_fifo
- echo >&10
- # ------------------------------------------------------- #
- sleep_interval() {
- sleep $(echo "$1/$SCALES" | bc -l)
- }
- interrupt() {
- clean
- exit 1
- }
- clean() {
- tput cnorm # 恢复光标
- exec &>>/dev/null
- kill $pid_cursor
- kill $pid_type
- rm $lock_fifo
- }
- # ------------------------------------------------------- #
- trap interrupt SIGINT SIGHUP
- tput civis # 隐藏光标
- # 光标闪烁进程
- {
- while :; do
- # 光标闪烁 - 下划线
- sleep_interval $DELAY_SLEEP
- read -u 10
- {
- if [ "$s" = "e" ]; then
- break
- fi
- echo -n "█"
- echo 1 >&10
- }
- # 光标闪烁 - 空置
- sleep_interval $DELAY_SLEEP
- read -u 10 s
- {
- [ "$s" = 1 ] && echo -ne "\b"
- if [ "$s" = "e" ]; then
- break
- fi
- echo -ne " \b"
- echo 0 >&10
- }
- done
- } 2>/dev/null &
- pid_cursor=$!
- # 稍作停顿
- sleep_interval $DELAY_START
- # 打字进程
- {
- while IFS= read -r line; do
- # 空行
- if ((${#line} == 0)); then
- read -u 10
- {
- echo
- echo 2 >&10
- }
- fi
- # 遍历每个字符
- for ((i = 0; i < ${#line}; i++)); do
- cur_char=${line:$i:1}
- if [[ ! "$cur_char" =~ ^[[:space:]]*$ ]]; then
- delay_type_select=${DELAY_TYPES[ $((RANDOM % ${#DELAY_TYPES[@]})) ]}
- sleep_interval $((RANDOM % delay_type_select))
- fi
- read -u 10 s
- {
- [ "$s" = 1 ] && echo -ne "\b"
- echo -n "${line:$i:1}"
- # 如输出至一行中最后一个字符,直接输出"\n"
- ((i == ${#line} - 1)) && echo
- echo 2 >&10
- }
- done
- done
- } 2>/dev/null
- pid_type=$!
- clean
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement