Shell-变量高级用法 (3) 有类型变量

我们知道在强类型语言中定义变量的时候必须为这个变量定义类型,比如定义整形可以定义为int型,字符串 string类型,日期类型 Date 等等,在使用之前必须定义类型,这是强类型编程语言的风格。Shell 编程中,由于 Shell 是弱类型语言,在使用变量前不需要为变量声明类型, 但其实 Shell 本身也是支持提前声明有类型变量的,只是说使用方式和一般的强类型编程语言有些不同, 使用declaretypeset 命令来进行声明。

  • declare 命令和 typeset 命令两者等价
  • declaretypeset 命令都是用来定义变量类型的

好了,既然它们是等价的 那我们用 declare 进行演示

declare 常用命令参数表

参数 含义
-r 将变量设为只读
-i 将变量设为整数
-a 将变量定义为数组
-f 显示此脚本定义过的所有函数及内容
-F 仅显示此脚本定义过的函数名
-x 将变量声明为环境变量

declare -r 演示, 只读属性还有一个命令 readonly

[wonbin@localhost shell]$ var1="aaa bbb"
[wonbin@localhost shell]$ declare -r var1
[wonbin@localhost shell]$ echo $var1
aaa bbb
[wonbin@localhost shell]$ var1="xxxx oooo"
-bash: var1: readonly variable
[wonbin@localhost shell]$ var3="asdfasd "
[wonbin@localhost shell]$ readonly var3
[wonbin@localhost shell]$ var3="dasfa "
-bash: var3: readonly variable

declare -i 演示

[wonbin@localhost shell]$ num1=10
[wonbin@localhost shell]$ num2=$num1+20  // 默认当做字符串处理
[wonbin@localhost shell]$ echo $num2
10+20
[wonbin@localhost shell]$ expr $num1 + 10
20
[wonbin@localhost shell]$ declare -i num3
[wonbin@localhost shell]$ echo $num3

[wonbin@localhost shell]$ num3=$num1+90
[wonbin@localhost shell]$ echo $num3
100

declare -f 在shell中将系统定义好的函数,包括函数名及函数体 打印出来

[wonbin@localhost shell]$ declare -f | less
__HOSTNAME () 
{ 
    local zero=0;
    local ret=0;
    local cur_word="$2";
    if [ "$1" == "X" ]; then
        return;
    else
        if [ "$1" == "match" ]; then
            return 0;
        else
            if [ "$1" == "complete" ]; then
                COMPREPLY=($(compgen -A hostname -- $cur_word));
            fi;
        fi;
    fi;
    return 0
}
__SIZE () 
{ 
    return 0
}
__SLAVEURL () 
{ 
    return 0
}
__VOLNAME () 

直接拿来用, 这里用第一个函数做演示

[wonbin@localhost shell]$ __HOSTNAME "complete"
[wonbin@localhost shell]$ echo $COMPREPLY
localhost

declare -F 只打印函数名, 不打印方法体

[wonbin@localhost shell]$ declare -F | less

declare -f __HOSTNAME
declare -f __SIZE
declare -f __SLAVEURL
declare -f __VOLNAME
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __git_aliased_command
declare -f __git_aliases
declare -f __git_commands
declare -f __git_complete
declare -f __git_complete_diff_index_file
declare -f __git_complete_file
declare -f __git_complete_index_file
declare -f __git_complete_remote_or_refspec
declare -f __git_complete_revlist
declare -f __git_complete_revlist_file
declare -f __git_complete_strategy
declare -f __git_compute_all_commands
declare -f __git_compute_merge_strategies
declare -f __git_compute_porcelain_commands
declare -f __git_config_get_set_variables
declare -f __git_count_arguments
declare -f __git_diff_index_files

declare -a array_name 声明数组, 然后 array_name=(value1 ... valuen), 这里不声明也可以

数组的一些操作:

  • 删除元素:

unset my_arr[2] 清除元素 unset my_arr 清空整个数组

[wonbin@localhost shell]$ my_arr=(3 "cccd")
[wonbin@localhost shell]$ echo ${my_arr[*]}
3 cccd
[wonbin@localhost shell]$ unset my_arr[1]
[wonbin@localhost shell]$ echo ${my_arr[@]}
3
[wonbin@localhost shell]$ unset my_arr
[wonbin@localhost shell]$ echo ${my_arr[@]}

[wonbin@localhost shell]$
  • 分片访问: ${array[@]:1:3} 数组下标从1 到3 的三个元素,
    [wonbin@localhost shell]$  m_array=("arsenal" "livepool" "manchester city" "chelsea" "hot spur")
    livepool manchester city chelsea
  • 内容替换: ${my_array[@]/an/AN}将数组中所有元素内包含 an 的子串替换为 AN
    [wonbin@localhost shell]$ my_array=("an1" "an2" "an3" "an4")
    [wonbin@localhost shell]$ echo ${my_array[@]/an/AN}
    AN1 AN2 AN3 AN4
  • 数组遍历:
    for v in ${my_array[@]}
    do
    echo $v
    done

    declare -x 声明为环境变量,可以直接在脚本中使用

取消声明的变量:

  • declare +r
  • declare +i
  • declare +a
  • declare +x
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!