您的位置:首页 > 运维架构 > Shell

高级Bash脚本编程指南(12):指定变量的类型: 使用declare或者typeset

2013-05-08 19:59 666 查看
高级Bash脚本编程指南(12):指定变量的类型: 使用declare或者typeset
成于坚持,败于止步

declare或者typeset内建命令(这两个命令是完全一样的)允许指定变量的具体类型. 在某些编程语言中, 这是指定变量类型的一种很弱的形式. declare命令是从Bash 2.0之后才被引入的命令. typeset也可以用在ksh的脚本中.

declare/typeset选项

-r 只读

declare -r var1(declare -r var1与readonly var1是完全一样的)

这和C语言中的const关键字一样, 都用来指定变量为只读. 如果你尝试修改一个只读变量的值, 那么会产生错误信息.

root@ubuntu:~/resource/shell-study/0508-2013# declare -r var1=11
root@ubuntu:~/resource/shell-study/0508-2013# echo $var1
11
root@ubuntu:~/resource/shell-study/0508-2013# let "var1=12"
bash: var1: readonly variable
root@ubuntu:~/resource/shell-study/0508-2013# ^C
root@ubuntu:~/resource/shell-study/0508-2013# readonly var2=12
root@ubuntu:~/resource/shell-study/0508-2013# echo $var2
12
root@ubuntu:~/resource/shell-study/0508-2013# let "var2=13"
bash: var2: readonly variable
root@ubuntu:~/resource/shell-study/0508-2013#

-i 整型

declare -i number 脚本将会把变量"number"按照整型进行处理.

如果把一个变量指定为整型的话, 那么即使没有expr或者let命令, 也允许使用特定的算术运算.

#!/bin/bash

declare -i number
number=3
echo "number = $number"

number="123"
echo "number = $number" 

number="abc"
echo "number = $number" 

number=6/3
echo "number = $number" 

not_int_number=6/3
echo "not_int_number = $not_int_number" 
exit 0
看看结果:

root@ubuntu:~/resource/shell-study/0508-2013# ./test1.sh 
number = 3
number = 123
number = 0
number = 2
not_int_number = 6/3
root@ubuntu:~/resource/shell-study/0508-2013#

-a 数组

declare -a indices 变量indices将被视为数组.

root@ubuntu:~/resource/shell-study/0508-2013# declare -a str
root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5)
root@ubuntu:~/resource/shell-study/0508-2013# echo $str
1
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[0]}
1
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[1]}
2
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[2]}
3
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[3]}
4
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[4]}
5
root@ubuntu:~/resource/shell-study/0508-2013#

用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容,直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素,直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据

root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5)
root@ubuntu:~/resource/shell-study/0508-2013# echo $str
1
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[*]}
1 2 3 4 5
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}
1 2 3 4 5
root@ubuntu:~/resource/shell-study/0508-2013# str[1]=10
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}
1 10 3 4 5
root@ubuntu:~/resource/shell-study/0508-2013# unset str
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}

root@ubuntu:~/resource/shell-study/0508-2013#

-f 函数

declare -f

如果在脚本中使用declare -f, 而不加任何参数的话, 那么将会列出这个脚本之前定义的所有函数.

declare -f function_name

如果在脚本中使用declare -f function_name这种形式的话, 将只会列出这个函数的名字.

root@ubuntu:~/resource/shell-study/0508-2013# declare -f
command_not_found_handle () 
{ 
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- $1;
        return $?;
    else
        if [ -x /usr/share/command-not-found ]; then
            /usr/bin/python /usr/share/command-not-found -- $1;
            return $?;
        else
            return 127;
        fi;
    fi
}
root@ubuntu:~/resource/shell-study/0508-2013# declare -f hello
-x export

declare -x var3

这句将会声明一个变量, 并作为这个脚本的环境变量被导出.

-x var=$value

declare -x var3=373

declare命令允许在声明变量类型的同时给变量赋值.

先到这里了,O(∩_∩)O~

我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐