Open main menu

CDOT Wiki β

Changes

Tutorial10: Shell Scripting - Part 1

1 byte removed, 10:19, 3 March 2021
INVESTIGATION 3: COMMAND SUBSTITUTION / MATH OPERATIONS
# Issue the following commands demonstrating other types of math calculations:<br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 - 3))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 * 3))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 / 3))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 ** 3))"</span><br><br>'''NOTE:''' You may notice that '''dividing''' '''2''' by '''3''' shows a '''zero''' result. To perform decimal calculations would require<br>the use of other commands such as '''awk''' or '''bc''' (that technique '''NOT''' covered in this course).<br><br>You can use this math construct with variables as well.<br><br>
# Issue the following commands demonstrating using the math construct with variables:<br><span style="color:blue;font-family:courier;font-weight:bold">num1=34</span><br><span style="color:blue;font-family:courier;font-weight:bold">num2=12</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((num1 * num2))"</span><br><br>What did you notice?<br><br>You can create variables and assign them values in the math construction as well.<br><br>
# Issue the following commands demonstrating using the math construct with variables:<br><span style="color:blue;font-family:courier;font-weight:bold">num1=5</span><br><span style="color:blue;font-family:courier;font-weight:bold">num2=3</span><br><span style="color:blue;font-family:courier;font-weight:bold">((result = num1 ** num2))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "The result is: $result"</span><br><br>
# x
13,420
edits