Open main menu

CDOT Wiki β

Changes

Tutorial10: Shell Scripting - Part 1

140 bytes added, 09:52, 16 March 2021
INVESTIGATION 3: COMMAND SUBSTITUTION / MATH OPERATIONS
'''Perform the Following Steps:'''
# Confirm that you are located in your '''home''' directory in your Matrix account.<br><br>Let's demonstrate that the Unix/Linux shell stores numbers as ascii text which can cause problems when performing math operations.<br><br># To demonstrate the need for the '''( ( ))''' construct, issue the following Linux command from the shell:<br><span style="color:blue;font-family:courier;font-weight:bold">echo "1 + 2"</span><br><br>What did you notice?<br><br>
# Issue the following Linux commands (using the ''math construct''):<br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((1 + 2))"</span><br><br>What did you notice?<br>The <span style="font-family:courier;font-weight:bold">(( ))</span> construct converted values '''1''' and '''2''' from ''text'' to '''binary numbers'''.<br>The '''$''' in front of the construct '''expands''' the result of the calculation.<br><br>
# Issue the following Linux 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 the '''awk''' or '''bc''' Linux commands (we will '''NOT''' cover that method to work with ''decimal numbers'' in this course).<br><br>You can use the ''math construct'' with variables as well.<br><br>
13,420
edits