-1

I asked A few days ago about an error in a bash script. In the meantime I had rebuilded the code and now I still have 1 problem. I want to assign the value of "waarde to "vorigewaarde" in the if loop that looks how much your score is. while I tried to find the problem with bash -x (filename),it says that the value of "vorigewaarde" empty is, but I don't see how that can happen.

The code (I have marked the line where the fault is, it is after the until code line)

#!/bin/bash
score=0
uitkomst=juist
waardegok=0



Number=$(($RANDOM % 52))

KAARTEN=(Harten1 Harten2 Harten3 Harten4 Harten5 Harten6 Harten7 Harten8
Harten9 Harten10 Harten11 Harten12 Harten13 Klaver1 Klaver2 Klaver3
Klaver4 Klaver5 Klaver6 Klaver7 Klaver8 Klaver9 Klaver10 Klaver11 Klaver12
Klaver13 Schop1 Schop2 Schop3 Schop4 Schop5 Schop6 Schop7 Schop8 Schop9 
Schop10 Schop11 Schop12 Schop13 Ruit1 Ruit2 Ruit3 Ruit4 Ruit5 Ruit6 Ruit7
Ruit8 Ruit9 Ruit10 Ruit11 Ruit12 Ruit13)


Gokkaart=${KAARTEN[$Number]}
echo " "
if (( "$Number" >= 0 )) && (( "$Number" <= 12 ));then
  case $Gokkaart in
  Harten1) waarde=1;;
  Harten2) waarde=2;;
  Harten3) waarde=3;;
  Harten4) waarde=4;;
  Harten5) waarde=5;;
  Harten6) waarde=6;;
  Harten7) waarde=7;;
  Harten8) waarde=8;;
  Harten9) waarde=9;;
  Harten10) waarde=10;;
  Harten11) waarde=11;;
  Harten12) waarde=12;;
  Harten13) waarde=13;;
esac

elif (( "$Number" >= 13 )) && (( "$Number" <= 25 ));then
case $Gokkaart in
  ##same for Harten only now for Klaveren
esac

elif (( "$Number" >= 26 )) && (( "$Number" <= 38 ));then
case $Gokkaart in
  ##same for Klaveren only now for Schoppen
esac

elif (( "$Number" >= 39 )) && (( "$Number" <= 52 ));then
case $Gokkaart in
  ##Same for Schoppen only now for Ruiten
esac
fi
echo "De eerste kaart is een $Gokkaart"
echo " "
echo "$waarde"
until [ $uitkomst != juist ]
do
  echo " "
  echo -n "hoger of lager? type [H of L] > "
  read gok
  echo "gok: $gok"

  if [ "$gok" = "H" ];then

    echo "Uw gok is hoger"
    waardegok=14

  elif [ "$gok" = "L" ];then
    echo "Uw gok is lager"
    waardegok=0
  fi

  if (( "$score" >= 1 ));then

    vorigekaart=$Gokkaart2
    vorigewaarde=$waarde2
    echo "De vorige kaart was een $Gokkaart2"
    echo " "
  elif (( "$score" == 0 ));then **#Here is the error**

    vorigekaart=$Gokkaart
    vorigewaarde=$waarde
    echo "Laten we beginnen"
    echo " "
  fi

  Number2=$(($RANDOM % 52))

  KAARTEN2=(#same that of array as KAARTEN but now for KAARTEN2 and Gokkaart2)

  Gokkaart2=${KAARTEN2[$Number2]}

  if (( "$Number2"=="$Number" ));then

    Number2=$(($RANDOM % 52))
    Gokkaart2=${KAARTEN2[$Number2]}

  elif (( "$Number2" >= 0 )) && (( "$Number2" <= 12 ));then

    echo "De volgende kaart is een harten, namelijk $Gokkaart2"
    case $Gokkaart2 in
    Harten1) waarde2=1;;
    Harten2) waarde2=2;;
    Harten3) waarde2=3;;
    Harten4) waarde2=4;;
    Harten5) waarde2=5;;
    Harten6) waarde2=6;;
    Harten7) waarde2=7;;
    Harten8) waarde2=8;;
    Harten9) waarde2=9;;
    Harten10) waarde2=10;;
    Harten11) waarde2=11;;
    Harten12) waarde2=12;;
    Harten13) waarde2=13;;
    esac

  if (( "$waardegok" > "$waarde2" )) && (( "$waarde2" > "$vorigewaarde" ));then

    echo "$Gokkaart2"
    echo "Goed gegokt!"
    score=$((score+1))
    echo "Uw score is nu : "$score""
    echo " "

  elif (( "$waardegok" < "$waarde2" )) && (( "$waarde2" < "$vorigewaarde" ));then

    echo "$Gokkaart2"
    echo "Goed gegokt!"
    score=$((score+1))
    echo "Uw score is nu : "$score""
    echo " "

  else
    echo "Fout, maar goed geprobeert!"
    uitkomst=false
  fi

##this is also the same way for number higher than 12 all the way to 52
done
echo " uw score was $score"

I have also edited the code a bit more, now I check everything at the end but it still gives the same error.

elif (( "$Number2" >= 39 )) && (( "$Number2" <= 52 ));then
case $Gokkaart2 in
##same as harten but now for Ruiten
esac
controlewaarde=$waarde2

fi

echo " "
echo -n "hoger of lager? type [H of L] > "
read -r gok
echo "gok: $gok"

if (( "$score" >= 1 ));then

  vorigewaarde=$waarde2

  echo "De vorige kaart was een $Gokkaart2"
  echo " "
elif (( "$score" == 0 ));then

  vorigewaarde=$waarde
  echo "Laten we beginnen"
  echo "De eerste kaart is een $Gokkaart"
  echo " "
fi

if [ "$gok" = "H" ];then

  echo "Uw gok is hoger"
  waardegok=14

elif [ "$gok" = "L" ];then
  echo "Uw gok is lager"
  waardegok=0
fi

if (( "$Number2" >= 0 )) && (( "$Number2" <= 12 ));then

  echo "De volgende kaart is een harten, namelijk $Gokkaart2"

elif (( "$Number2" >= 13 )) && (( "$Number2" <= 25 ));then
  echo "De volgende kaart is een klaver, namelijk $Gokkaart2"

elif (( "$Number2" >= 26 )) &&  (( "$Number2" <= 38 ));then
  echo "De volgende kaart is een Schoppen, namelijk $Gokkaart2"

elif (( "$Number2" >= 39 )) && (( "$Number2" <= 52 ));then
  echo "De volgende kaart is een Ruiten, namelijk $Gokkaart2"
fi

if (( "$waardegok" > "$waarde2" )) && (( "$controlewaarde" > "$vorigewaarde" ));then

  echo "$Gokkaart2"
  echo "Goed gegokt!"
  score=$((score+1))
  echo "Uw score is nu : $score"
  echo " "

elif (( "$waardegok" < "$waarde2" )) && (( "$controlewaarde" < "$vorigewaarde"   ));then

  echo "$Gokkaart2"
  echo "Goed gegokt!"
  score=$((score+1))
  echo "Uw score is nu : $score"
  echo " "

else
  echo "Fout, maar goed geprobeert!"
  uitkomst=false
  fi
done
echo " uw score was $score"
9
  • 5
    Please remove any code that isn't necessary to reproduce your problem. This isn't close to a minimal example. Commented Dec 23, 2016 at 12:48
  • 3
    And please indent your code too. Commented Dec 23, 2016 at 12:52
  • Now normally you only see the code that is needed. is the fault in mij if loop that checks the value of the score or is the fault in how I check the value of "gokkaart" in the first cases? Commented Dec 23, 2016 at 13:01
  • At some point you have this line: vorigewaarde=$waarde2 and waarde2 is defined later. Commented Dec 23, 2016 at 13:01
  • 1
    Your script has multiple syntax issues, couple of missing fi and done, use shellcheck.net to fix the syntaxes Commented Dec 23, 2016 at 13:01

2 Answers 2

1

As gniourf_gniourf pointed out, your problem is that you assign an undefined variable to vorigewaarde. You do this:

vorigewaarde=$waarde2

but the case the assign a value to waarde2 is further down the code so it's undefined at that moment in your code. If you wish to see it directly, add

echo $waarde2 

right before your assignation and it will show and empty line

You could assign $waarde since this one was defined in your 1st case but might not be what you want or you need to move your 2nd case up so that waarde2 has a value when you try you value assignation. You could also move your assignation after the 2nd case but then again, it might not be what you want to do

Sign up to request clarification or add additional context in comments.

9 Comments

even if I do my assignation at the end of the code, it still does the same problem.
I don't know why it didn't occur to me when i first look at our code, but since your are working with numbers, it would be preferable that you work with -lt (lesser than), -eq (equals) or -gt (greater than) if your various if. There is no magic there, if vorigewaarde remains empty, that means that this bloc:
can't edit my own comment apparently so here's the rest: this bloc: if (( "$score" >= 1 ));then vorigewaarde=$waarde2 echo "De vorige kaart was een $Gokkaart2" echo " " elif (( "$score" == 0 ));then vorigewaarde=$waarde echo "Laten we beginnen" echo "De eerste kaart is een $Gokkaart" echo " " fi never resolve to true
but it works with ">=" and "<=", but if I check it with bash -x then it says that my "controlewaarde" empty is even when I want to use it at the end of the code.
Sorry, I didn't saw it. But I have found the problem. while I was replacing the "[" for "((" I saw that in the if cases at Schoppen and Ruiten, I check for Schoppen1 or Ruiten1 but not like the one in the array that is Schop1 or Ruit1. I have fixed that and it works now as it should.
|
0

I have found the problem. while I was replacing the "[" for "((" I saw that in the if cases at Schoppen and Ruiten, I check for Schoppen1 or Ruiten1 but not like the one in the array that is Schop1 or Ruit1. I have fixed that and it works now as it should. – Michael

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.