坚持循环

时间:2018-11-28 16:45:49

标签: shell

问题1:我需要做一个循环,如果我用它循环永远。在下面,我将脚本的有效部分放到了底部,在该部分的底部,我需要循环

#!/bin/bash

#Input War Players

read -r -p "Number of Players " Players

read -r -p "Player 1 Name " Player1
if [ "$Players" -gt 1 ]; then
read -r -p "Player 2 Name " Player2
fi
if [ "$Players" -gt 2 ]; then
read -r -p "Player 3 Name " Player3
fi

WarPlayerList="$Player1","$Player2","$Player3"

从这里到现在为止的工作原理是不起作用的循环

for i in $WarPlayerList
do
TH=""
while [[ ! "$TH" =~ ^[0-9] ]]; do
echo "Enter Player TH Level "
read TH
done
done

我需要这个循环进入我输入的每个玩家,例如3。 然后我需要将每个玩家数据输出到单独的变量中,以使用以下命令:

#Output Results to File

Headers=("Player","Attacks","Stars","Damage","Stars Earned","3 Star Rate","Points")

Player1Results=("$Player1","$Attacks1","$StarResult1","$DamageResult1","$StarEarnedResult1","$StarRate1","$PointsResult1",)
Player2Results=("$Player2","$Attacks2","$StarResult2","$DamageResult2","$StarEarnedResult2","$StarRate2","$PointsResult2",)
Player3Results=("$Player3","$Attacks3","$StarResult3","$DamageResult3","$StarEarnedResult3","$StarRate3","$PointsResult3",)
printf "%s\n" "$Headers" "$Player1Results" "$Player2Results" "$Player3Results" > WarResults.csv

一些网站建议foreach而不是

在寻求帮助的同时,我需要这段代码来检查输入是否向上,向下或相反:

问题2:

PositionA=""
while [[ ! "$PositionA" =~ "up |  ! "$PositionA" =~ "down" |  ! "$PositionA" =~ "opposite" ]]; do
echo "Enter Player Attack up, down or opposite "
read PositionA
done

问题3:

我需要输入名称以匹配数据库列表中的名称,否则它将无法正确更新MYSQL

Playerlist=$( awk -F "," '{ print $2 }'  Database_input.csv )

Player1=""
while [[  ! "$Player1" =~ "$Playerlist" ]]; do
echo "Enter Player 1 Name "
read Player1
done

也尝试过:

if [ "$Player1" != .\* "$Playerlist"\ .* ]; then
read -r -p "Retype Player 1 Name " Player1
fi

欣赏一些见识。

1 个答案:

答案 0 :(得分:-2)

我不知道您要尝试使用哪种语言,但是我猜这行:

for i in $WarPlayerList do TH="" while [[ ! "$TH" =~ ^[0-9] ]]; do echo "Enter Player TH Level " read TH done done

应包括

i += 1

在某个时候。目前,您没有增加i,这意味着$ WarPlayerList中的“ i”将保留在第一次迭代中。