我正在尝试使用我的Minecraft服务器,基本上它会在聊天中查找某个单词,然后对其进行响应。
这是我的代码:
echo [INFO] Started
while true; do
tail -n 1 logs/latest.log > lookin.txt
if grep -q ".df" lookin.txt
then
echo [$(date +'%r')][CMD] Toggledownfall
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "toggledownfall\015"'
sleep 0.1
fi
if grep -q '.day' lookin.txt
then
echo [$(date +'%r')][CMD] Time set day
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "time set day\015"'
sleep 0.1
fi
if grep -q '.night' lookin.txt
then
echo [$(date +'%r')][CMD] Time set night
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "time set night\015"'
sleep 0.1
fi
if grep -q '.gm 1' lookin.txt
then
echo [$(date +'%r')][CMD] Gamemode 1 creeper_prey
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "gamemode 1 creeper_prey\015"'
sleep 0.1
fi
if grep -q '.gm 0' lookin.txt
then
echo [$(date +'%r')][CMD] Gamemode 1 creeper_prey
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "gamemode 1 creeper_prey\015"'
sleep 0.1
fi
if grep -q '.gm' lookin.txt
then
echo [$(date +'%r')][CMD][ATTEMPT] Gamemode
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "say Only ops are allow to use gamemode\015"'
sleep 0.1
fi
###############################################
if grep -q '.test' lookin.txt
then
echo $1
echo ' ' > lookin.txt
screen -S mc -X eval 'stuff "say argumentsGoHere\015"'
sleep 0.1
fi
################################################
done
我想要做的是获取用'.test'给出的参数并将它们打印在'ArgumentsGoHere'的位置。带.test的代码是#的
中的位答案 0 :(得分:1)
您的设计并不是最好的。您应该考虑将tail
与-f
开关(如果适用于您的版本)一起使用,如下所示:
while read -r line; do
# do stuff with line
done < <(tail -f -n0 logs/latest.log)
这将避免在每次迭代时完全读取整个文件,并且您不会需要sleep 0.1
技巧。这也避免了使用辅助文件looking.txt
。
现在,我想你想用命令检查行是否启动。为此,您应该使用case
:
case $line in
(.df)
echo "[$(date +'%r')][CMD] Toggledownfall"
screen -S mc -X eval 'stuff "toggledownfall\015"' # I'm not sure what you're trying to achieve here
;;
(.day)
...
esac
现在,关于测试的参数:相应的case语句应为:
(.test *)
read -r -a args <<< "$line"
# At this point you have an array args that contains all the arguments.
screen -S mc -X eval 'stuff "say ${args[@]:1}\015"' # This looks very dangerous to me
;;
现在,请非常小心,因为您使用eval
未经过用户输入!用户可以写.test $(rm / -rf)
或任何其他邪恶的命令! 此处不要使用eval
!