zoukankan      html  css  js  c++  java
  • Learning_the_bash_Shell_Third_Edition 12/n

    Typed Variables

    You can set variable attributes with the declare built-in. * Table 6-1 summarizes the available options with declare.

    Option

    Meaning

    -a

    The variables are treated as arrays

    -f

    Use function names only

    -F

    Display function names without definitions

    -i

    The variables are treated as integers

    -r

    Makes the variables read-only

    -x

    Marks the variables for export via the environment

    Typing declare on its own displays the values of all variables in the environment. The -f option limits this display to the function names and definitions currently in the environment. -F limits it further by displaying only the function names.

    cor@debian:~/shell/mar8$ cat 1.sh
    val1=12 val2=5
    result1=val1*val2
    echo "result1 = "$result1

    declare -i val3=12 val4=5
    declare -i result2
    declare -i result3
    result2=val3*val4
    result3=${val3}*${val4}
    echo "result2 = " ${result2}
    echo "result3 = " ${result3}

    #结果

    cor@debian:~/shell/mar8$ . 1.sh

    #the result is just the string “val1*val2”

    result1 = val1*val2

    result2 = 60
    result3 = 60

      

    The -x option to declare operates in the same way as the export built-in that we saw in Chapter 3. It allows the listed variables to be exported outside the current shell environment.

    The -r option creates a read-only variable, one that cannot have its value changed by subsequent assignment statements and cannot be unset.

     Integer Variables and Arithmetic

    The expression $(($OPTIND - 1)) in the last graphics utility example shows another way that the shell can do integer arithmetic. As you might guess, the shell interprets words surrounded by $(( and )) as arithmetic expressions. Variables in arithmetic expressions do not need to be preceded by dollar signs, though it is not wrong to do so.

    Arithmetic expressions are evaluated inside double quotes, like tildes, variables, and command substitutions. We’re finally in a position to state the definitive rule about quoting strings: when in doubt, enclose a string in single quotes, unless it contains tildes or any expression involving a dollar sign, in which case you should use double quotes.

    For example, the date command on modern versions of UNIX accepts arguments that tell it how to format its output. The argument +%j tells it to print the day of the year, i.e., the number of days since December 31st of the previous year. We can use +%j to print a little holiday anticipation message:

    echo "Only $(( (365-$(date +%j)) / 7 )) weeks until the New Year"
    

      The arithmetic operators that are supported. Although some of these are (or contain) special characters, there is no need to backslash-escape them, because they are within the $((...)) syntax.

    Operator

    Meaning

    ++

    Increment by one (prefix and postfix)

    --

    Decrement by one (prefix and postfix)

    +

    Plus

     -

    Minus

     *

    Multiplication

    /

    Division (with truncation)

    %

    Remainder

    **

    Exponentiation

    <<

    Bit-shift left

    >>

    Bit-shift right

    &

    Bitwise and

    |

    Bitwise or

    ~

    Bitwise not

    !

    Logical not

    ^

    Bitwise exclusive or

    ,

    Sequential evaluation

    #

    cor@debian:~/shell/mar8$ i=0
    cor@debian:~/shell/mar8$ echo $i
    0
    cor@debian:~/shell/mar8$ echo $((i++))
    0
    cor@debian:~/shell/mar8$ echo $i
    1
    cor@debian:~/shell/mar8$ echo $((++i))
    2
    cor@debian:~/shell/mar8$ echo i
    i
    cor@debian:~/shell/mar8$ echo $i
    2
    

      Relational operators

    Operator

    Meaning

    <

    Less than

    >

    Greater than

    <=

    Less than or equal to

    >=

    Greater than or equal to

    ==

    Equal to

    !=

    Not equal to

    &&

    Logical and

    ||

    Logical or

    For example, $((3 > 2)) has the value 1; $(( (3 > 2) || (4 <= 1) )) also has the value 1,since at least one of the two subexpressions is true.

    The shell also supports base N numbers, where N can be from 2 to 36. The notation B#N means “N base B”. Of course, if you omit the B#, the base defaults to 10.

     

  • 相关阅读:
    《数据结构与算法Python语言描述》习题第二章第三题(python版)
    mysql中的视图
    mysql中列的增删改
    php隐藏WEBSHELL技巧
    php webshell常见函数
    MySQL join 用法
    BurpSuite 设置Hostname Resolution
    Linux mint 18.1 / Ubuntu 16.04 安装steam
    Linux SCIM/fcitx/ibus 输入法
    mysql 复制表结构 / 从结果中导入数据到新表
  • 原文地址:https://www.cnblogs.com/winditsway/p/14498649.html
Copyright © 2011-2022 走看看