I have been working with Debian for a couple of months for school and because it seems really handy to work with. but now I want to make a game for in the terminal. A Higher or Lower card game (also for school but I wanted to make it before I was given this task). You have to pick a Random card and then it asked if you want to bet High or Low. When you choose you bet, it wil created another random card and it wil then check if the new card is higer or lower. I still got a few problems that I can't find myself and hopefully you guys can lead me to the right direction.
The error is my if loops that it cannot find a command, but even after redoing the if loops, it is still the same error. I don't really see what my fault is. In the terminal, it says that my errors ar at line 58,49,97 an 107 and I don't really see my fault or what is wrong.
(if you don't understand a sentence, it is written in dutch)
"Here is the compleet code for the card game:
#!/bin/bash
score=0
uitkomst=juist
waardegok=0
echo "Hallo, welkom bij het spel {Hoger of Lager}"
echo "Het doel van het spel is om juist te kunnen gokken..."
echo "...Met kaarten."
echo "Je krijgt een kaart en jij moet gokken of de volgende kaart"
echo "hoger of lager in waarde is."
echo "Als je juist hebt geraden wordt uw juiste gok opgeslagen."
echo "veel succes!!"
echo "Ik ben niet verantwoordelijk voor eventuele verslavingen."
until [ $uitkomst != juist ]
do
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 " "
echo $Gokkaart
waardekaart=$((Number+1))
echo -n "hoger of lager? type [H of L] > "
read gok
echo "gok: $gok"
if [ "$gok" = "H" ];
then
echo "Uw gok is hoger"
waardegok=54
elif [ "$gok" = "L" ];
then
echo "Uw gok is lager"
waardegok=-1
fi
Number2=$(($RANDOM % 52))
KAARTEN2=(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 )
Gokkaart2=${KAARTEN2[$Number2]}
waardekaart2=$((Number2+1))
if [$Number2 -ge 0 ] && [ $Number2 -le 12 ];
then
echo "De volgende kaart is een harten, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 13 ] && [ $Number2 -le 25 ];
then
echo "De volgende kaart is een klaver, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 26 ] && [ $Number2 -le 38 ];
then
echo "De volgende kaart is een Schoppen, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 39 ] && [ $Number2 -le 52 ];
then
echo "De volgende kaart is een Ruiten, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
fi
done
echo " uw score is $score"
Related
Can anyone help me with my code? When I run my script I always get Grade F.
#!/bin/bash
echo "Enter your percentage to see grade letter"
read grade
if [ $grade -ge 60 ]; then
echo "Your Grade F"
elif [ $grade -ge 60 ] || [ $grade -le 69 ]; then
echo "Your grade D"
elif [ $grade -ge 70 ] && [ $grade -le 79 ]; then
echo "Your grade C"
elif [ $grade -ge 80 ] && [ $grade -le 89 ]; then
echo "Your grade B"
elif [ $grade -ge 90 ] && [ $grade -le 100 ]; then
echo "Your Grade is A"
else
echo "try again"
fi
You have a logic error in your code. Your first test is wrong. It tests all values that are greater than or equal to 60, and makes them an F. This prevents the rest of the tests from happening for those values.
Your second test is also wrong; you're using an OR (||) instead of an AND (&&).
Your last test could be reduced, because all values greater than or equal to 90 are an A, so the second test isn't necessary.
I personally would test for the higher vales and work down if I was writing the code, but that's your choice. The below should work.
#!/bin/bash
echo "Enter your percentage to see grade letter"
read grade
if [ $grade -le 59 ]; then
echo "Your Grade F"
elif [ $grade -ge 60 ] && [ $grade -le 69 ]; then
echo "Your grade D"
elif [ $grade -ge 70 ] && [ $grade -le 79 ]; then
echo "Your grade C"
elif [ $grade -ge 80 ] && [ $grade -le 89 ]; then
echo "Your grade B"
elif [ $grade -ge 90 ]; then
echo "Your Grade is A"
else
echo "try again"
fi
An alternative method using case statement could be
read -r -p 'Enter your percentage to see grade letter: ' percent
case $percent in
[0-9] | [0-5][0-9] ) echo 'Your Grade F';;
6[0-9] ) echo 'Your Grade D';;
7[0-9] ) echo 'Your Grade C';;
8[0-9] ) echo 'Your Grade B';;
9[0-9] | 100 ) echo 'Your Grade A';;
*) echo 'Try again'
esac
Here's how I'd do it.
#!/bin/bash
# Get the grade
read -rp "Enter your percentage to see grade letter: " grade
# Test the input to make sure it's a number,
# and exit with an error message if it isn't
[[ "$grade" =~ ^[0-9]+$ ]] || { echo "Please enter an integer." ; exit; }
# Test the number, print the grade, and exit
[[ "$grade" -le 49 ]] && { echo "You want to go home and re-think your life." ; exit; }
[[ "$grade" -le 59 ]] && { echo "Your Grade: F" ; exit; }
[[ "$grade" -le 69 ]] && { echo "Your grade: D" ; exit; }
[[ "$grade" -le 79 ]] && { echo "Your grade: C" ; exit; }
[[ "$grade" -le 89 ]] && { echo "Your grade: B" ; exit; }
[[ "$grade" -ge 90 ]] && { echo "Your Grade: A" ; exit; }
I'm not sure if it's more or less efficient than using if-elif-fi or case. I'm sure it's not as pretty. But it only tests $grade until it gets a hit and then it stops, and something about that appeals to me.
I was tempted to put the "Is this a number?" test in a loop so you wouldn't have to invoke the script again if you flubbed the input, but I resisted the temptation.
(I'm a rookie with bash scripting, so I welcome criticism.)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
First of all, I am new here and I am a total n00b in bash scripting so I hope I won't mess up this first question of mine. This all has to do with reading out the data of a save game file, it's part of a larger script where I can select a character and keep the game char selection menu clean as well as makes backups on exiting the game. This will add the information on the character to the selection menu. (Diablo II LoD and more than 40 characters doesn't always do well when searching for them in the char selection menu)
I have looked for answers before signing up to Stackoverflow, but I only see answers where there are only two outcomes: Yes or No, 1 or 1, etc. From this I have tried with the below script, but yeah... doesn't work that way. :(
The Script:
#!/bin/bash
d2s=barfive.d2s
charclass="$(xxd -u -p -l1 -s 40 "$d2s")"
if [ "$charclass" == "00" ]; then
echo "Amazon";
else [ "$charclass" == "01" ];
echo "Sorceress";
elif [ "$charclass" == "02" ];
echo "Necromancer";
elif [ "$charclass" == "03" ];
echo "Paladin";
else [ "$charclass" == "04" ];
echo "Barbarian";
else [ "$charclass" == "05" ];
echo "Druid";
else [ "$charclass" == "06" ];
echo "Assassin";
fi
What I want to do:
Basically the outcome range from 00 to 06, each having a different outcome as described above, and I want to echo the correct value to the list. So if the outcome of the xxd command is 00, Amazon should be the output. If 06, Assassin should be the output.
The error it gives now:
./test.sh: line 26: syntax error near unexpected token `elif'
./test.sh: line 26: ` elif [ "$charclass" == "02" ];'
It's better to use case for that
case $charclass in
00) echo "Amazon";;
01) echo "Sorceress";;
02) echo "Necromancer";;
03) echo "Paladin";;
04) echo "Barbarian";;
05) echo "Druid";;
06) echo "Assassin";;
*) echo "no match";;
esac
Or using an array
chars=(
Amazon
Sorceress
Necromancer
Paladin
Barbarian
Druid
Assassin
)
echo ${chars[charclass]}
Two solutions have been given and both work equally good.
#ivan
case $charclass in
00) echo "Amazon";;
01) echo "Sorceress";;
02) echo "Necromancer";;
03) echo "Paladin";;
04) echo "Barbarian";;
05) echo "Druid";;
06) echo "Assassin";;
*) echo "no match";;
esac
#Sparrow and #hotoku
#!/bin/bash
d2s=barfive.d2s
charclass="$(xxd -u -p -l1 -s 40 "$d2s")"
if [ "$charclass" == "00" ]; then
echo "Amazon";
elif [ "$charclass" == "02" ]; then
echo "Necromancer";
elif [ "$charclass" == "03" ]; then
echo "Paladin";
elif [ "$charclass" == "04" ]; then
echo "Barbarian";
elif [ "$charclass" == "05" ]; then
echo "Druid";
elif [ "$charclass" == "06" ]; then
echo "Assassin";
else [ "$charclass" == "01" ];
echo "Sorceress";
fi
So thanks to all three. ^_^
You should replace all else but last one to elif, like this:
if [ "$charclass" == "00" ]; then
echo "Amazon";
elif [ "$charclass" == "01" ]; then
echo "Sorceress";
elif [ "$charclass" == "02" ]; then
echo "Necromancer";
elif [ "$charclass" == "03" ]; then
echo "Paladin";
elif [ "$charclass" == "04" ]; then
echo "Barbarian";
elif [ "$charclass" == "05" ]; then
echo "Druid";
elif [ "$charclass" == "06" ]; then
echo "Assassin";
else
echo "no match"
fi
The syntax is incorrect, it should be.
if <expression>; then
<commands>
elif <expression>; then
<commands>
else
<commands>
fi
Your code should be something as below.
#!/bin/bash
d2s=barfive.d2s
charclass="$(xxd -u -p -l1 -s 40 "$d2s")"
if [ "$charclass" == "00" ]; then
echo "Amazon";
elif [ "$charclass" == "02" ]; then
echo "Necromancer";
elif [ "$charclass" == "03" ]; then
echo "Paladin";
elif [ "$charclass" == "04" ]; then
echo "Barbarian";
elif [ "$charclass" == "05" ]; then
echo "Druid";
elif [ "$charclass" == "06" ]; then
echo "Assassin";
else [ "$charclass" == "01" ];
echo "Sorceress";
fi
I have a bash script for get date from xferlog like this(this is sample code):
#!/bin/bash
xferlog="/var/log/xferlog"
xfer1="test"
xfer2="test2"
xfer1last=$(tac "$xferlog" | awk -v pattern="$xfer1" '$9 ~ pattern {print; exit}')
xfer2last=$(tac "$xferlog" | awk -v pattern="$xfer2" '$9 ~ pattern {print; exit}')
day1=${xfer1last:8:2}
month1=${xfer1last:4:3}
year1=${xfer1last:20:4}
hour1=${xfer1last:11:2}
minute1=${xfer1last:14:2}
if [ "$month1" == "Jan" ];then
monthlast1=01
fi
if [ "$month1" == "Feb" ];then
monthlast1=02
fi
if [ "$month1" == "Mar" ];then
monthlast1=03
fi
if [ "$month1" == "Apr" ];then
monthlast1=04
fi
if [ "$month1" == "May" ];then
monthlast1=05
fi
if [ "$month1" == "Jun" ];then
monthlast1=06
fi
if [ "$month1" == "Jul" ];then
monthlast1=07
fi
if [ "$month1" == "Aug" ];then
monthlast1=08
fi
if [ "$month1" == "Sep" ];then
monthlast1=09
fi
if [ "$month1" == "Oct" ];then
monthlast1=10
fi
if [ "$month1" == "Nov" ];then
monthlast1=11
fi
if [ "$month1" == "Dec" ];then
monthlast1=12
fi
daylast1=$day1
if [ "$day1" == " 1" ];then
daylast1=01
fi
if [ "$day1" == " 2" ];then
daylast1=02
fi
if [ "$day1" == " 3" ];then
daylast1=03
fi
if [ "$day1" == " 4" ];then
daylast1=04
fi
if [ "$day1" == " 5" ];then
daylast1=05
fi
if [ "$day1" == " 6" ];then
daylast1=06
fi
if [ "$day1" == " 7" ];then
daylast1=07
fi
if [ "$day1" == " 8" ];then
daylast1=08
fi
if [ "$day1" == " 9" ];then
daylast1=09
fi
echo "$daylast1.$monthlast1.$year1 $hour1 $minute1"
Xferlog sample like this:
Thu Sep 2 09:52:00 2004 50 192.168.20.10 896242 /home/test/file1.tgz b _ o r suporte ftp 0 * c
My script working and show me what I want, like this:
02.09.2004 09:52
So I need to change month format( for ex. Jan to 01 ) and write 0 before day if day value is single character.
Any better suggestion for this? Maybe I can do that with smaller code.
Thank you.
If you have access to GNU date:
$ date -d"Thu Sep 2 09:52:00 2004" +"%d.%m.%Y %H:%M"
02.09.2004 09:52
#!/bin/bash
echo "Enter three numbers and this program will give you the middle number : " ; read num1 ; read num2 ; read num3
if [ "$num1" -gt "$num2" ] && [ "$num1" -lt "$num3" ] || [ "$num1" -lt "$num2" ] && [ "$num1" -gt "$num3" ]; then
{
echo "The middle number is $num1"
}
elif [ "$num2" -gt "$num1" ] && [ "$num2" -lt "$num3" ] || [ "$num2" -lt "$num1" ] && [ "$num2" -gt "$num3" ]; then
{
echo "The middle number is $num2"
}
elif [ "$num3" -gt "$num1" ] && [ "$num3" -lt "$num2" ] || [ "$num3 -lt "$num1" ] && [ "$num3" -gt "$num2" ]; then
{ echo "The middle number is $num3" }
fi
The problem I have is with the or condition. I input the numbers 1, 2, and 3, but I get the middle number as 1 all the time.
How about this:
getmid() {
if (( $1 <= $2 )); then
(( $1 >= $3 )) && { echo $1; return; }
(( $2 <= $3 )) && { echo $2; return; }
fi;
if (( $1 >= $2 )); then
(( $1 <= $3 )) && { echo $1; return; }
(( $2 >= $3 )) && { echo $2; return; }
fi;
echo $3;
}
# All permutations of 1, 2 and 3 print 2.
getmid 1 2 3
getmid 2 1 3
getmid 1 3 2
getmid 3 1 2
getmid 2 3 1
getmid 3 2 1
This one should work:
#!/bin/bash
echo "Enter three numbers and this program will give you the middle number : " ; read num1 ; read num2 ; read num3;
if [ "$num1" -gt "$num2" ] && [ "$num1" -lt "$num3" ]; then
{
echo "The middle number is" $num1 ;
}
elif [ "$num1" -lt "$num2" ] && [ "$num1" -gt "$num3" ]; then
{
echo "The middle number is" $num1 ;
}
elif [ "$num2" -gt "$num1" ] && [ "$num2" -lt "$num3" ]; then
{
echo "The middle number is" $num2 ;
}
elif [ "$num2" -lt "$num1" ] && [ "$num2" -gt "$num3" ]; then
{
echo "The middle number is" $num2 ;
}
elif [ "$num3" -gt "$num1" ] && [ "$num3" -lt "$num2" ]; then
{
echo "The middle number is" $num3 ;
}
elif [ "$num3" -lt "$num1" ] && [ "$num3" -gt "$num2" ]; then
{
echo "The middle number is" $num3 ;
}
fi
Here is the example:
$ credits="29" ; echo "C=$credits" ; initial="1064" ; echo "C=$credits" ; limit=`expr $initial / 10` ; echo "C=$credits" ; if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then echo "C=$credtis" ; fi ; echo "C=$credits" ; echo "I=$initial"
C=29
C=29
C=29
C=29
C=
C=29
I=1064
$ c="29" ; echo "C=$c" ; initial="1064" ; echo "C=$c" ; limit=`expr $initial / 10` ; echo "C=$c" ; if [ "$c" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$c" ; then echo "C=$c" ; fi ; echo "C=$c" ; echo "I=$initial"
C=29
C=29
C=29
C=29
C=29
C=29
I=1064
When var name is "c", everything is ok, but when it's "credits", I can't use var content in the if condition.
Don't understand why...
Your problem is an incorrectly formatted if expression in both lines of code. Specifically, the first line includes an incorrect echo "C=$credits" ; between the end of the if and the then:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then
should be:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; then
echo "C=$credtis"
fi
The same error is repeated here:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then
should be
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; then
echo "C=$credtis"
fi