Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

1,019 bytes removed, 17:22, 4 March 2021
Additional Loop Statements
<span style="font-family:courier;font-weight:bold">answer=10<br>read –p “pick a number between 1 and 10: “ guess<br>while test $guess –eq 10<br>do &nbsp;&nbsp;&nbsp;read –p “Try again: “ guess<br>done<br>echo “You are correct”</span><br><br>
 
'''Mathematical Operations:'''
 
Since you did not have to declare a data-type of a variable, user-defined variables store values as only text.
This creates a problem if you want to perform math operations. In order to convert values stored as text in the shell to binary numbers, you need to use a command in order to accomplish that task.
 
The older method is to use the '''expr''' command.
 
''Example:''
 
<span style="font-weight:bold;font-family:courier;">
num1=5;num2=10<br>
result=$(expr $num1 + $num2)<br>
echo "$num1 + $num2 = $result"<br>
5 + 10 = 15
</span>
 
A short-cut to convert text values to binary numbers is to contain within a double set of round brackets: '''(( ))'''
 
''Example:''
 
<span style="font-weight:bold;font-family:courier;">
num1=5;num2=10<br>
((result $num1 - $num2))<br>
echo "$num1 - $num2 = $result"<br>
5 - 10 = -5
</span>
 
Alternative Example:
 
<span style="font-weight:bold;font-family:courier;">
num1=5;num2=10<br>
echo "$num1 x $num2 = $((num1 * num2))"<br>
5 x 10 = 50
</span>
''Example using a Loop:''
13,420
edits

Navigation menu