gpt4 book ai didi

linux - Bash - 从脚本中获取未知数量的参数

转载 作者:太空狗 更新时间:2023-10-29 12:31:05 24 4
gpt4 key购买 nike

我正在为我拥有的 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 的代码是 # 中的位

最佳答案

您的设计并不是最好的。您应该考虑将 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!

关于linux - Bash - 从脚本中获取未知数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com