Bash中的数组赋值和引用
2007-12-20 TsengYia#126.com
http://selinux.cublog.cn################################################################
系统环境:RHEL5 [ 2.6.18-8.el5xen ]
软件环境:bash-3.1-16.1
################################################################
Bash中的数组赋值和引用
示例1:
[root@localhost ~]# SEASON=("Spring" "Summer" "Autumn" "Winter") //给整个数组赋值
[root@localhost ~]# set | grep SEASON //查看已设置的数组信息(下标从0开始)
SEASON=([0]="New_Spring" [1]="Summer" [2]="Autumn" [3]="Winter")
[root@localhost ~]# echo ${SEASON[*]} //显示整个数组的内容
Spring Summer Autumn Winter
[root@localhost ~]# echo ${SEASON[3]} //显示指定的单个数组元素
Winter
[root@localhost ~]# SEASON[0]="New_Spring" //给单个数组元素赋值
[root@localhost ~]# echo ${SEASON[*]} //显示整个数组
New_Spring Summer Autumn Winter
[root@localhost ~]# unset SEASON[2] //清除指定的单个数组元素
[root@localhost ~]# unset SEASON //清除整个数组
示例2:
[root@localhost ~]# cat /etc/shells | tr "\n" " " > /tmp/tmp.file //将文件中的回车转换成空格
[root@localhost ~]# read -a SHELLS < /tmp/tmp.file //以文件中内容给数组赋值(碰到第一个回车符之前的内容)
[root@localhost ~]# set | grep "SHELLS" //查看数组赋值情况
SHELLS=([0]="/bin/sh" [1]="/bin/bash" [2]="/sbin/nologin" [3]="/bin/tcsh" [4]="/bin/csh" [5]="/bin/ksh")
阅读(2805) | 评论(0) | 转发(0) |