LoginSignup
8
8

More than 5 years have passed since last update.

進捗どうですかブーム???にShellScriptで挑むフリしてExpectに逃げる

Last updated at Posted at 2015-07-16

「進捗・どう・です・か」をランダムに表示し「進捗どうですか」が完成したら煽ってくるClojure
「進捗・どう・です・か」をランダムに表示し「進捗どうですか」が完成したら煽ってくるプログラム
「進捗・どう・です・か」をHaskellで書いてみた
進捗どうですかブーム?にrubyでのっかってみる
進捗どうですかブーム??にPythonでのりかかってみる
1145148101919ブームにShellScriptで普通に挑む

え?ちょっとぐらい外部コマンド使っていいでしょ?

bash用
list=(進捗 どう です か)
while rand=$(od -vAn -N1 -tu1 /dev/urandom | tr -d ' '); do
    echo -n ${list[(($rand % ${#list[*]}))]}
done | expect -c 'expect '$(IFS=;echo "${list[*]}")' {
    set mes $expect_out(buffer)$expect_out(0,string)???
    puts $mes
    puts [string length $mes]文字で煽られました
}'
zsh用
list=(進捗 どう です か)
while rand=$(od -vAn -N1 -tu1 /dev/urandom | tr -d ' '); do
    echo -n ${list[(($rand % ${#list[*]} + 1))]}
done | expect -c 'expect '$(IFS=;echo "${list[*]}")' {
    set mes $expect_out(buffer)$expect_out(0,string)???
    puts $mes
    puts [string length $mes]文字で煽られました
}'

追記:

C++とRubyのコードを読んでおきながら動きを全く理解してなかったけどさっき1145148101919とにらめっこしててようやく理解出来た…非常に短いコードであるにも関わらず「人が書いたコードが全く読めない」状況に陥ったので焦った…

せっかくなのでこいつも書きなおしてみる。

bash用
list=(進捗 どう です か)
while 
    rand=$(($(od -vAn -N1 -tu1 /dev/urandom | tr -d ' ') % ${#list[@]}))
    str=${list[$rand]}
    echo -n $str
    size=$((${size:-0} + ${#str}))
    count=$(($rand > ${last:-${#list[@]}} ? $count + 1 : 0))
    last=$rand
(($count != ${#list[@]} - 1)); do :; done
echo -e "???\n$((${size} + 3))文字で煽られました"
zsh用
list=(進捗 どう です か)
while 
    rand=$(($(od -vAn -N1 -tu1 /dev/urandom | tr -d ' ') % ${#list[@]}))
    str=${list[$(($rand + 1))]}
    echo -n $str
    size=$((${size:-0} + ${#str}))
    count=$(($rand > ${last:-${#list[@]}} ? $count + 1 : 0))
    last=$rand
(($count != ${#list[@]} - 1)); do :; done
echo -e "???\n$((${size} + 3))文字で煽られました"
8
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8