products:ict:linux:shell_scripting:arithmetic
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| products:ict:linux:shell_scripting:arithmetic [2023/05/04 17:02] – created wikiadmin | products:ict:linux:shell_scripting:arithmetic [2023/05/04 17:13] (current) – wikiadmin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | |||
| + | ====== Shell Scripting Arithmetic ====== | ||
| Line 13: | Line 15: | ||
| #!/bin/bash | #!/bin/bash | ||
| + | |||
| # Basic arithmetic using let | # Basic arithmetic using let | ||
| + | |||
| let a=5+4 | let a=5+4 | ||
| + | |||
| echo $a # 9 | echo $a # 9 | ||
| + | |||
| let "a = 5 + 4" | let "a = 5 + 4" | ||
| + | |||
| echo $a # 9 | echo $a # 9 | ||
| + | |||
| let a++ | let a++ | ||
| + | |||
| echo $a # 10 | echo $a # 10 | ||
| + | |||
| let "a = 4 * 5" | let "a = 4 * 5" | ||
| + | |||
| echo $a # 20 | echo $a # 20 | ||
| + | |||
| let "a = $1 + 30" | let "a = $1 + 30" | ||
| + | |||
| echo $a # 30 + first command line argument | echo $a # 30 + first command line argument | ||
| Line 48: | Line 61: | ||
| 9 | 9 | ||
| + | |||
| 9 | 9 | ||
| + | |||
| 10 | 10 | ||
| + | |||
| 20 | 20 | ||
| + | |||
| 45 | 45 | ||
| Line 75: | Line 92: | ||
| #!/bin/bash | #!/bin/bash | ||
| + | |||
| # Basic arithmetic using expr | # Basic arithmetic using expr | ||
| + | |||
| expr 5 + 4 | expr 5 + 4 | ||
| + | |||
| expr "5 + 4" | expr "5 + 4" | ||
| + | |||
| expr 5+4 | expr 5+4 | ||
| + | |||
| expr 5 \* $1 | expr 5 \* $1 | ||
| + | |||
| expr 11 % 2 | expr 11 % 2 | ||
| + | |||
| a=$( expr 10 - 3 ) | a=$( expr 10 - 3 ) | ||
| + | |||
| echo $a # 7 | echo $a # 7 | ||
| Line 112: | Line 137: | ||
| ./ | ./ | ||
| + | |||
| 9 | 9 | ||
| + | |||
| 5 + 4 | 5 + 4 | ||
| + | |||
| 5+4 | 5+4 | ||
| + | |||
| 60 | 60 | ||
| + | |||
| 1 | 1 | ||
| + | |||
| 7 | 7 | ||
| Line 124: | Line 155: | ||
| In the section on Variables we saw that we could save the output of a command easily to a variable. It turns out that this mechanism is also able to do basic arithmetic for us if we tweak the syntax a little. We do so by using double brackets like so: | In the section on Variables we saw that we could save the output of a command easily to a variable. It turns out that this mechanism is also able to do basic arithmetic for us if we tweak the syntax a little. We do so by using double brackets like so: | ||
| + | < | ||
| $(( expression )) | $(( expression )) | ||
| + | </ | ||
| Here's an example to illustrate: | Here's an example to illustrate: | ||
| Line 131: | Line 164: | ||
| #!/bin/bash | #!/bin/bash | ||
| + | |||
| # Basic arithmetic using double parentheses | # Basic arithmetic using double parentheses | ||
| + | |||
| + | < | ||
| a=$(( 4 + 5 )) | a=$(( 4 + 5 )) | ||
| + | </ | ||
| + | |||
| echo $a # 9 | echo $a # 9 | ||
| + | |||
| + | < | ||
| a=$((3+5)) | a=$((3+5)) | ||
| + | </ | ||
| + | |||
| echo $a # 8 | echo $a # 8 | ||
| + | |||
| + | < | ||
| b=$(( a + 3 )) | b=$(( a + 3 )) | ||
| + | </ | ||
| + | |||
| echo $b # 11 | echo $b # 11 | ||
| + | |||
| + | < | ||
| b=$(( $a + 4 )) | b=$(( $a + 4 )) | ||
| + | </ | ||
| + | |||
| echo $b # 12 | echo $b # 12 | ||
| + | |||
| + | < | ||
| (( b++ )) | (( b++ )) | ||
| + | </ | ||
| + | |||
| echo $b # 13 | echo $b # 13 | ||
| + | |||
| + | < | ||
| (( b += 3 )) | (( b += 3 )) | ||
| + | </ | ||
| + | |||
| echo $b # 16 | echo $b # 16 | ||
| + | |||
| + | < | ||
| a=$(( 4 * 5 )) | a=$(( 4 * 5 )) | ||
| + | </ | ||
| + | |||
| echo $a # 20 | echo $a # 20 | ||
| Let's break it down: | Let's break it down: | ||
| + | < | ||
| a=$(( 4 + 5 )) | a=$(( 4 + 5 )) | ||
| + | </ | ||
| This is the basic format. As you can see we may space it out nicely for readability without the need for quotes. | This is the basic format. As you can see we may space it out nicely for readability without the need for quotes. | ||
| + | < | ||
| a=$((3+5)) | a=$((3+5)) | ||
| + | </ | ||
| As you can see, it works just the same if we take spacing out. | As you can see, it works just the same if we take spacing out. | ||
| + | < | ||
| b=$(( a + 3 )) | b=$(( a + 3 )) | ||
| + | </ | ||
| We may include variables without the preceding $ sign. | We may include variables without the preceding $ sign. | ||
| + | < | ||
| b=$(( $a + 4 )) | b=$(( $a + 4 )) | ||
| + | </ | ||
| Variables can be included with the $ sign if you prefer. | Variables can be included with the $ sign if you prefer. | ||
| + | < | ||
| (( b++ )) | (( b++ )) | ||
| + | </ | ||
| This is a slightly different form. Here the value of the variable b is incremented by 1 (using the same mechanism illustrated under let). When we do this we don't need the $ sign preceding the brackets. | This is a slightly different form. Here the value of the variable b is incremented by 1 (using the same mechanism illustrated under let). When we do this we don't need the $ sign preceding the brackets. | ||
| + | < | ||
| (( b += 3 )) | (( b += 3 )) | ||
| + | </ | ||
| This is a slightly different form of the previous example. Here the value of the variable b is incremented by 3. It is a shorthand for b = b + 3. | This is a slightly different form of the previous example. Here the value of the variable b is incremented by 3. It is a shorthand for b = b + 3. | ||
| Line 178: | Line 251: | ||
| ./ | ./ | ||
| + | |||
| 9 | 9 | ||
| + | |||
| 8 | 8 | ||
| + | |||
| 11 | 11 | ||
| + | |||
| 12 | 12 | ||
| + | |||
| 13 | 13 | ||
| + | |||
| 16 | 16 | ||
| + | |||
| 20 | 20 | ||
| Line 199: | Line 279: | ||
| #!/bin/bash | #!/bin/bash | ||
| + | |||
| # Show the length of a variable. | # Show the length of a variable. | ||
| + | |||
| a=' | a=' | ||
| + | |||
| echo ${#a} # 11 | echo ${#a} # 11 | ||
| + | |||
| b=4953 | b=4953 | ||
| + | |||
| echo ${#b} # 4 | echo ${#b} # 4 | ||
| ./ | ./ | ||
| + | |||
| 11 | 11 | ||
| + | |||
| 4 | 4 | ||
| Line 219: | Line 306: | ||
| print out the result of the expression. | print out the result of the expression. | ||
| + | < | ||
| $(( expression )) | $(( expression )) | ||
| + | </ | ||
| Return the result of the expression. | Return the result of the expression. | ||
products/ict/linux/shell_scripting/arithmetic.1683201772.txt.gz · Last modified: 2023/05/04 17:02 by wikiadmin