Raspberry Pi_Eng_20.2.4 Operations in Script


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)

최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


20.2.4  Operations in Script

 

20.2.4.1    Assignment Operation

 

When we define a new variable or we assign a value to a variable in a script, it is called an assignment. The operator used in this case is "=". This is called assignment mode, and the basic format is as follows.

 

VARIABLE=Value

 

There should be no spaces in "VARIABLE=Value".

ex) PIDDIR=/var/run/samba

 

 

If the value to be assigned to the variable has a space, it must be expressed in the format "xxx xxx" using " ".

ex) name="David John"

 


 

20.2.4.2    Reference Operation

 

   Reference to a variable value

 

To refer to the current value assigned to a variable, use the "$" operator. Follow "$" with variable name, and use the format of "$VARIABLE". This is referred to as referencing mode, and the basic format is as follows.  

 

$VARIABLE

 

The following example shows a simple assignment and a reference. We define "PIDDIR" variable, allocate the value to it, and refer to the value assigned to the variable in the form of "$PIDDIR" in the next sentence.

 

PIDDIR=/var/run/samba

NMBDPID=$PIDDIR/nmbd.pid

 

If "$variable" is used inside a string, the value of the variable is used.

 "$Variable"       -- Value of the variable is used.

 

 

   Reference to a variable name

 

To use the variable name itself, use it with ' ' (single quote).

 $Variable        -- Variable name is used.

 

To use the variable name inside a string, use it with "" (escape).

"$Variable"     -- Variable name is used..


 

20.2.4.3    Arithmetic Operation of Integer Number Type

 

   Arithmetic operator

 

Calculating with number in scripts are called arithmetic operations, and arithmetic operators are as follows:

    +               -- Add

    -                -- Subtract

    *                -- Multiply

                      -- Use "*" in "expr" command. 

    /                -- Divide

    **               -- Exponentiate

    %               -- modular, mod -- Remainder value in integer division

    +=             -- plus-equal     -- Increase by constant value

    -=              -- minus-equa    -- Decrease by constant value

    *=              -- times-equal    -- Multiply constant value

    /=              -- slash-equal    -- Divide by constant value

    %=             -- mod-equal     -- Remainder value in dividing by constant value

 

 

   Assigning the result of numerical calculation

 

Assigning the result of numerical calculation to another variable is processed in the following format. You do nott need to use a space between operand and operator, and you do not have to use "$" to refer to a variable in an arithmetic expression.

    Basic format -- Variable= $((arithmetic expression))

 

n=$(($n+2))

n=$(( $n + 2 ))

n=$((n+2))

n=$(( n + 2 ))

 

 

[Used Example]

 

#! /bin/bash

n=1

 

n=$(( $n+1 ))

echo $n

 

n=$((n+1))

echo $n

 

 

   How to use number variable defined with "declare" operator

 

If you declare a variable corresponding to a number in the script, the variable is initialized to "0". You do not need to use "$" to refer to a variable in an expression. Also, if the expression contains spaces, use " " (double-quote).

 

declare -i num

num=num+10  

num=$num+10  

 

[Used Example]

 

#! /bin/bash

 

declare -i n

 

n=$n+1

echo $n

 

n="n + 1"

echo $n

 

 

   How to use "let" operator

 

"let" operator performs integer arithmetic operations and tests numeric expressions. You do not need to use "$" to refer to a variable in an expression. Also, if the expression contains spaces, use " " (double-quote).

 

let n=$n+10

let n="$n + 10"

let n=n+10

let n="n + 10"

 

[Used Example]

 

#! /bin/bash

n=1

 

let "n= $n + 1"

echo $n

 

let "n=$n+1"

echo $n

 

 

   Arithmetic operation with "expr" command

 

You can perform arithmetic operations with "expr" command in scripts. The "expr" command basically requires a space for operand and operator. Note that you must use it with escape("") when using "*".

 

When using the result of the calculation using the "expr" command, the entire expression should be surrounded by ` ` (backtab).

 

n=`expr $n + 2`

n=`expr $n - 1`

n=`expr $n / 3`

n=`expr $n * 3`           -- Means "*"

 

[Used Example]

 

#! /bin/bash

n=1

n=`expr $n + 1`

echo $n

 

 

20.2.4.4    Arithmetic Operation of Real Number Type

 

Shell does not allow real numberoperations. Use "awk" command to perform real number operations. When using the results of calculations, you must enclose the entire expression using ` ` (backtab).

 

pi@raspberrypi:~ $ m=`awk -v x=2.66 -v y=5.22 'BEGIN{printf "%.2f\n", x*y}'`

 

pi@raspberrypi:~ $ echo $m

13.89

 


 

20.2.4.5    Logical Operation

 

In a normal program, various states are examined to determine the current situation. In this case, a logical operation is required. A logical operation is an operation that turns out to be true or false. The following operations can be performed.

 

   Logical Operation Using Comparison Operator

 

These operators compare the two values to determine the size, and determine True or False according to the result.

 

<operand-1>    <operator>    <operand-2>

 

The size comparison operators are as follows:

    =              -- True if operand-1 and operand-2 are equal, False otherwise.

    -eq            -- True if operand-1 and operand-2 are equal, False otherwise.

 

    !=              -- True if operand-1 and operand-2 are not equal, False otherwise.

    -ne             -- True if operand-1 and operand-2 are not equal, False otherwise.

 

    -gt             -- True if operand-1 >  operand-2, False otherwise

    -ge             -- True if operand-1 >= operand-2, False otherwise

 

    -lt              -- True if operand-1 <  operand-2, False otherwise

    -le              -- True if operand-1 <= operand-2, False otherwise

 

 

   Logical Operation Using Logical Operator

 

This is an operator that performs logical operations on two conditional expressions. This can be used within "test" command or "if" test bracket.

 

    "and" logical operator        

 

It returns "True" if both values of <conditional expression-1> and <conditional expression-2> are "True", or "False".

 

<conditional expression-1>          <operator>       <conditional expression-2>

 

You can use the following operators:

    -a              -- and

    &&            -- and

 

    "or" logical operator          

 

It returns "True" if at least one value of <conditional expression-1> and <conditional expression-2> is "True", or "False"

 

<conditional expression-1>          < operator>      <conditional expression-2>

 

You can use the following operators:

    -o              -- or

    ||                -- or

 

    Negative result

 

This operator is an operator that reverses the operation result of a conditional expression. If the result of the conditional expression is "True", it is changed to "False". If the result of the operation is "False", it is changed to "True".

 

< operator>    <conditional expression>

 

You can use the following operators:

    !                -- negative

 

   Logical Operation Using File Operator

 

This operator is an operator that checks the status of a specified file and then returns the result. It is often used in "test" commands. The check result is returned to "True" or "False".

 

<expression>    <directory/file>

 

The file operators are as follows:

    -a   -- True if the file exists, otherwise False

    -e   -- True if the file exists, otherwise False

 

    -r   -- True if the user can read the file, False otherwise

    -w -- True if the user can write the file, False otherwise

    -x   -- True if the user can execute the file, False otherwise

    -o   -- True if the user is the owner of the file, False otherwise

 

    -z   -- True if the size of the file is 0, False otherwise

 

    -f   -- True if the file is a file, False otherwise

    -d   -- True if the file is a directory, False otherwise