热门关键字:  ubuntu  分区  函数  Fedora  linux系统进程

当前位置 :| 主页>Linux教程>Shell开发>

Bash中的数组赋值和引用

来源: 作者: 时间:2007-12-21 Tag: 点击:
Bash中的数组赋值和引用

2007-12-20 kenthy#qingdaonews.com

################################################################
系统环境: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")
最新评论共有 4 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册