Der findes ikke et "else if" udtryk i bash - Det du egentlig skriver er:
#!/bin/bash
echo "Write a number!"
read a
echo "Write another number!"
read b
# Asks IF the two variables is different
if [ $b > $a ]
then
echo "The first number is biggest!"
else
if [ $b < $a ]; then
echo "The second number is biggest!"
fi
# Mangler fi!
Du kan løse det på to måder, med "elif":
#!/bin/bash
echo "Write a number!"
read a
echo "Write another number!"
read b
# Asks IF the two variables is different
if [ $b > $a ]
then
echo "The first number is biggest!"
elif [ $b < $a ]; then
echo "The second number is biggest!"
fi
eller med to "fi":
#!/bin/bash
echo "Write a number!"
read a
echo "Write another number!"
read b
# Asks IF the two variables is different
if [ $b > $a ]
then
echo "The first number is biggest!"
else
if [ $b < $a ]; then
echo "The second number is biggest!"
fi
fi
PS: Du mener formentligt "[ $b < $a ]" i stedet for "[ $b > $a ]"
Det hele kan yderligere simplificeres til:
#!/bin/bash
echo "Write a number!"
read a
echo "Write another number!"
read b
# Asks IF the two variables is different
if [ $b -lt $a ]; then
echo "The first number is biggest!"
else
echo "The second number is biggest!"
fi
Lige nu, opretter dit script en fil med navnet af det sidste nummer - den fejl har mit sidste script ikke.
Indlæg senest redigeret d. 03.03.2013 12:09 af Bruger #11328