我在Shell脚本中创建一个bot:
# Array with expressions expressions=("Ploink Poink" "I Need Oil" "Some Bytes are Missing!" "Poink Poink" "Piiiip Beeeep!!" "Hello" "Whoops! I'm out of memmory!") # Seed random generator RANDOM=$$$(date +%s) # Loop loop loop loop loop loop ... while [ 1 ] do # Get random expression... selectedexpression=${expressions[$RANDOM % ${#RANDOM[*]}]} # Write to Shell echo $selectedexpression # Wait an half hour sleep 1 # It's one second for debugging,dear SOers done
我希望它每秒从表达式中打印一个随机项.我试过这个,但它不起作用.它每次只打印第一个(Ploink Poink).谁能帮我吗?
谢谢
将您定义selectedexpression的行更改为
selectedexpression = ${expressions [$RANDOM%${#expressions [@]}]}
您希望索引表达式是从0到表达式数组长度的随机数.这样做会.