高级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关键字一样, 都用来指定变量为只读. 如果你尝试修改一个只读变量的值, 那么会产生错误信息.
-i 整型
declare -i number 脚本将会把变量"number"按照整型进行处理.
如果把一个变量指定为整型的话, 那么即使没有expr或者let命令, 也允许使用特定的算术运算.
-a 数组
declare -a indices 变量indices将被视为数组.
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容,直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素,直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据
-f 函数
declare -f
如果在脚本中使用declare -f, 而不加任何参数的话, 那么将会列出这个脚本之前定义的所有函数.
declare -f function_name
如果在脚本中使用declare -f function_name这种形式的话, 将只会列出这个函数的名字.
declare -x var3
这句将会声明一个变量, 并作为这个脚本的环境变量被导出.
-x var=$value
declare -x var3=373
declare命令允许在声明变量类型的同时给变量赋值.
先到这里了,O(∩_∩)O~
我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。
成于坚持,败于止步
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
待续。。。。
相关文章推荐
- shell实例(八) ---指定变量类型:使用declare或typeset,随机数
- 编写高质量代码改善C#程序的157个建议——建议35:使用default为泛型类型变量指定初始值
- Linux下Bash的变量使用,echo,read,declare,typeset,array,
- 使用STL的经验总结_指定关联容器的比较类型_12
- Linux下Bash的变量使用,echo,read,declare,typeset,array,
- 建议35:使用default为泛型类型变量指定初始值
- EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法
- STM8使用----STVD(COSMIC)定义变量指定其类型和位定义
- pycahrm使用docstrings来指定变量类型、返回值类型、函数参数类型
- Oracle编程之使用其他表的字段类型作为变量的类型
- Tomcat启动时一闪而过,但是jdk环境变量配置正确,并使用java或者javac命令多可以运行,但是tomcat不能运行解决办法。
- Eclipse使用默认编辑器打开指定类型的文件
- 不使用第三个变量交换两个int类型变量的值
- 使用STLPORT debug模式时遭遇error C2664错误,无法将Dbg_Iter转为指定的类型
- php 使用callable强制指定回调类型
- GA教程:使用自定义变量来扩展高级细分
- C#复习_不使用第三个变量交换两个int类型变量的值
- 【Java基础】在使用@Deprecated注解时发生的对象引用变量的类型的问题
- 使用windows的fsutil命令创建指定大小及类型的测试文件
- 不使用成员函数或者友元函数访问类的私有成员变量