|
| Bash shell的读取、声名和数组 |
| 2009年5月21日 |
变量的有效范围
1 启动一个shell时,操作系统分配一块内存给shell使用,这个区域的变量可以让子程序访问; 2 利用export功能,可以让变量内容写到上述内存中(环境变量); 3 当加载另一个shell时,子shell可以将父shell的环境变量所在的内存导入自己的环境变量区块中。
变量的键盘读取
#read
#读取 putin变量
[klot@localhost ~]$ read putin This is put in. [klot@localhost ~]$ echo $putin This is put in. [klot@localhost ~]$
# 10秒内输入自己的名字,制作成name变量
[klot@localhost ~]$ read -p "Enter your name:" -t 10 name Enter your name:klot [klot@localhost ~]$ echo $name klot # declare / typeset # 这两个命令用于生命变量的属性 # -a 定义为数组(array) # -i 定义为整数数字(integer) # -x 定义为环境变量,与export一样 # -r 设置变量为只读属性(readonly) # -f # -F # -t # -p # 求和:
[klot@localhost ~]$ declare -i sum=1+2+3 [klot@localhost ~]$ echo $sum 6 [klot@localhost ~]$ unset sum [klot@localhost ~]$ echo $sum
# 直接生命整数变量时,不会进行计算: [klot@localhost ~]$ sum=1+2 [klot@localhost ~]$ declare -i sum [klot@localhost ~]$ echo $sum 1+2 [klot@localhost ~]$
# 为变量设置只读属性: [klot@localhost ~]$ declare -r sum [klot@localhost ~]$ sum=new -bash: sum: readonly variable [klot@localhost ~]$
# var 数组 [klot@localhost ~]$ var[1]=a [klot@localhost ~]$ var[2]=b [klot@localhost ~]$ var[3]=c [klot@localhost ~]$ echo ${var[*]} a b c [klot@localhost ~]$标签: Linux Shell |
posted by Klot @ 07:46:00  |
|
|
|
|
|