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

高级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

待续。。。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值