-2

Problem 1: I need to make a loop and if I use for it loops forever. Below I have put the part of the script that works and at the bottom the section I need to loop

#!/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"

Up to here works from here is the loop that doesn't

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

I need this to loop for every player I enter eg 3. Then I need to output each players data in separate variables to use below command:

#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

Some sites suggest foreach instead off for

While I'm on the subject of asking for help, I need this piece of code to check if the input matches up, down or opposite:

Problem 2:

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

Problem 3:

I need the input name to match the names in the database list otherwise it won't update MYSQL properly

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

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

also tried:

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

Appreciate some insight.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    which script? bash? – Suraj Rao Nov 28 '18 at 16:48
  • Commas aren't used to separate list items in bash. – Charles Duffy Nov 28 '18 at 17:03
  • ...and while you're doing array-based *assignments*, you aren't using array expansion syntax to refer to the values at all. Which is to say, right now, your "arrays" only have a single entry each -- consisting of a comma-separated list of all your values; and when you refer to `$Player1Results`, you're expanding only that single initial element. – Charles Duffy Nov 28 '18 at 17:06
  • I suggest you start with a [good bash tutorial](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html) or maybe [this bash tutorial](https://www.linuxjournal.com/content/bash-arrays) on arrays. – KamilCuk Nov 28 '18 at 17:10
  • Thanks Charles, the bottom half with the values and printf is pointing to actual values, I couldn't add them as the entire script has 4000 lines off code. Without the loop it works, but is 4000 lines long and when I make a change on 1 player's values, I have to change 24 other player's values, hence the need for a loop. I'm currently using the shell (bash v4.4.19) on Ubuntu. – Dirk le Roux Nov 28 '18 at 18:43
  • We do ask that you keep it to one question per question. With respect to comparing a string against multiple correct values, one example of a preexisting (and already-answered!) question on the topic would be https://stackoverflow.com/questions/21157435/bash-string-compare-to-multiple-correct-values – Charles Duffy Nov 28 '18 at 20:52
  • BTW, with respect to looping over 24 players, I'd suggest using indirect assignments and lookups so you don't need to have separate code for Player1 and Player2, but can just refer to whichever player is current. See [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006). – Charles Duffy Nov 28 '18 at 20:53
  • ...as for reading content into a proper array, BTW, you might also want to see [BashFAQ #5](https://mywiki.wooledge.org/BashFAQ/005), or the [relevant section of the BashGuide](https://mywiki.wooledge.org/BashGuide/Arrays). – Charles Duffy Nov 28 '18 at 20:54

1 Answers1

-2

I don't know what language you're trying to write, but I'm guessing this line:

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

Should include

i += 1

At some point. At the moment you're not increasing i, which means for 'i' in $WarPlayerList stays at the first iteration.

Mark
  • 6,112
  • 4
  • 21
  • 46