Bash: Difference between revisions

Jump to navigation Jump to search
3 bytes removed ,  14 March 2022
Mediawiki's links are made with square brackets and that messes up the if statements
imported>Sjwhitak
(Moved messed up link to real link.)
 
imported>Sjwhitak
(Mediawiki's links are made with square brackets and that messes up the if statements)
 
=== If, Elif, Else ===
<pre>
#!/bin/bash
 
echo "Enter a number:"
#!/bin/bash
read number
 
echo "Enter a number:"
# if and elif both have a "then" associated with it
read number
if [ $number -eq 1 ]
then
# if and elif both have a "then" associated with it
echo 1
if [ $number -eq 1 ]
elif [[ $number -eq 2 ]]
then
then
echo 1
echo 2
elif [[ $number -eq 2 ]]
else
then
echo "You can't get anything right you hopeless loser"
echo 2
fi
else
</pre>
echo "You can't get anything right you hopeless loser"
fi
 
This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).
Anonymous user

Navigation menu