Bash脚本莎士比亚侮辱发生器

时间:2015-04-30 06:24:32

标签: bash shell

我试图通过从文件中读入并输出短语来制作侮辱发生器。文件(" myfile"有3列侮辱,我希望输出读取"你是(insult1)(insult2)(insult3)"

我到目前为止......

while read line 
do

Column1=$(cut -d" " -f 1)    
Column2=$(cut -d" " -f 2)
Column3=$(cut -d" " -f 3)

Insult1=${Column1[word]}
Insult2=${Column2[word]}
Insult3=${Column3[word]}

echo "Thou art a" $Insult1 $Insult2 $Insult3
done < myfile

我非常接近这一点,我只是没有&#34;你是一个&#34;在每个短语的开头,只是第一个短语。它在到达字符串末尾之前阅读所有的侮辱。

1 个答案:

答案 0 :(得分:1)

shell完全能够在空格上分割输入。 (得到它这样做可能是一个挑战!)

while read first second third; do
    echo "Thou art a $first $second $third"
done <myfile

当然,伪装只是sed 's/^/Thou art a /' myfile。如果你想要包含某种随机选择,我建议不同地组织输入 - 可能是三个不同的文件,或者是一个包含三个部分的文件。

first=$(shuf -n 1 adjectives.txt)
second=$(shuf -n 1 adjectives.txt)
third=$(shuf -n 1 nouns.txt)
echo Thou art a $first $second $third

(两次得到相同形容词的可能性很小。如果你是一只肮脏的脏狗,也许它甚至不是问题。)