Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

207 bytes added, 10:51, 29 December 2020
Additional Loop Statements
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:
A short-cut to convert text values to binary numbers is to contain within a double set of round brackets: (( ))
Example1Example:
<pre style="color:blue;font-weight:bold;font-family:courier;">
</pre>
Example2Alternative Example:
<pre style="color:blue;font-weight:bold;font-family:courier;">
echo "$num1 x $num2 = $((num1 * num2))"
5 x 10 = 50
</pre>
 
Example using a Loop:
 
<pre style="color:blue;font-weight:bold;font-family:courier;">
value=1
while [ $value -le 5 ]
do
echo "$value"
((value=value+1)) # could also use ((value++))
done
</pre>
13,420
edits

Navigation menu