一度に1文字を画面に印刷する方法を尋ねる場合(ハリウッド映画でよく見られる「ハッカー」スタイル)、次のスクリプトで十分です(入力はから取得しますstdin)。
でbash:
#!/bin/bash
while IFS= read -r line; do
length="${#line}"
bol=1
for (( offset = 0 ; offset < length ; offset++ )); do
char="${line:offset:1}"
printf '%s' "$char"
if (( bol )) && [[ "$char" == " " ]]; then
continue
fi
bol=0
sleep 0.05
done
if (( length == 0 )); then
sleep 0.$(( RANDOM % 3 + 2 ))
else
sleep 0.$(( RANDOM % 7 + 3 ))
fi
printf '\n'
done
または、Cのより単純なバージョン:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
char buf[1];
int len;
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
if (write(STDOUT_FILENO, buf, len) != len) {
perror("write");
return EXIT_FAILURE;
}
usleep(50000);
}
if (len != 0) {
perror("read");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
dmesgたとえば、入力として使用する場合:
dmesg | hollywood
./configure && make