Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

797 bytes added, 10:44, 29 December 2020
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. For example:
 
<pre>
num1=5;num2=10
result=$(expr $num1 + $num2)
echo "$num1 + $num2 = $result"
5 + 10 = 15
</pre>
 
A short-cut to convert text values to binary numbers is to contain within a double set of round brackets: (( ))
 
Example1:
 
num1=5;num2=10
((result $num1 - $num2))
echo "$num1 - $num2 = $result"
5 - 10 = -5
 
Example2:
 
num1=5;num2=10
echo "$num1 x $num2 = $((num1 * num2))"
5 x 10 = 50
===Using Startup Files===
13,420
edits

Navigation menu