(1)数组的定义

root@tcx4440-03:~# a=(1 2 3 4)

root@tcx4440-03:~# echo ${a[1]}
2

root@tcx4440-03:~# a[0]=1
root@tcx4440-03:~# a[1]=2

root@tcx4440-03:~# echo ${a[0]}
1

(2)数组值得获取

root@tcx4440-03:~# var[0]=1
root@tcx4440-03:~# var[1]=2

root@tcx4440-03:~# echo $var[1]
1[1]
root@tcx4440-03:~# echo ${var[1]}
2

(3)获取数组的长度。用${#数组名[@或*]} 可以得到数组长度

root@tcx4440-03:~# a=(10 20 30 40 50 60)
root@tcx4440-03:~# echo ${#a[*]}
6
root@tcx4440-03:~# echo ${#a[@]}
6

root@tcx4440-03:~# echo ${a[@]}
10 20 30 40 50 60

root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60

(4)数组的读取,用${数组名[下标]} 下标是从0开始  下标是:*或者@ 得到整个数组内容
root@tcx4440-03:~# echo ${a[1]}
20

root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60

(5)数组的赋值

root@tcx4440-03:~# a[3]=100

root@tcx4440-03:~# echo ${a[*]}
10 20 30 100 50 60

(6)数组的删除,直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。

root@tcx4440-03:~# echo ${a[*]}
10 20 30 100 50 60
root@tcx4440-03:~# unset a[1]
root@tcx4440-03:~# echo ${#a[@]}
5

root@tcx4440-03:~# echo ${a[*]}

root@tcx4440-03:~# unset a
root@tcx4440-03:~# echo ${#a[*]}
0

(7)分片使用,直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。

A:tt只是一个变量,不是一个数组,所以一定要加啊()

root@tcx4440-03:~# tt=${a[*]:2:4}

root@tcx4440-03:~# echo $tt 30 40 50 60

root@tcx4440-03:~# echo ${tt[1]}

root@tcx4440-03:~# echo ${tt[2]}

B: c仍然是一个数组

root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60

root@tcx4440-03:~# c=(${a[*]:1:4})

root@tcx4440-03:~# echo ${c[*]}
20 30 40 50
root@tcx4440-03:~# echo $c
20

root@tcx4440-03:~# echo ${c[2]}
40
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60

(8)替换,调用方法是:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据。

root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
root@tcx4440-03:~# echo ${a[*]/60/100}
10 20 30 40 50 100

root@tcx4440-03:~# echo ${a[*]}   //数组a的值是不变的
10 20 30 40 50 60

root@tcx4440-03:~# tt=${a[*]/100/1000}  //没有括号,则是一个变量,不是数组
root@tcx4440-03:~# echo $tt
10 20 30 40 50 60

root@tcx4440-03:~# echo ${tt[1]}

root@tcx4440-03:~# tt=(${a[*]/60/1000})  //有括号,则tt是一个数组

root@tcx4440-03:~# echo ${tt[5]}

1000

root@tcx4440-03:~# echo ${tt[*]}
10 20 30 40 50 1000
root@tcx4440-03:~# echo ${a[*]}  //数组a是没有变化的
10 20 30 40 50 60

shell中一维数组值得获取的更多相关文章

  1. Shell中的数组及其相关操作

    http://blog.csdn.net/jerry_1126/article/details/52027539 Shell中数据类型不多,比如说字符串,数字类型,数组.数组是其中比较重要的一种,其重 ...

  2. shell中的函数、shell中的数组、告警系统需求分析

    7月16日任务 20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析 20.16/20.17 shell中的函数 函数就是一个子shell就是一个代码段 ...

  3. linux shell 中的数组的取值 遍历 替换 删除操作

    引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...

  4. Linux centosVMware shell中的函数、shell中的数组、

    一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...

  5. 九 Shell中的数组

    数组:用一个变量存储一组数据,并能够对这组数据中的某一个数据单独操作. 数组的类型:一维数组.二维数组.多维数组 变量的类型 Shell中默认无类型 变量的值默认均视为文本 用在数字运算中时,自动将其 ...

  6. C语言中一维数组

    (1)输出数组元素 #include<stdio.h> int main() { int index; /*定义循环变量*/ int iArray[6]={0,1,2,3,4,5}; /* ...

  7. shell中的数组

    在shell脚本中,除了通常使用的shell变量外,有时也需要复杂的数据结构去实现一些功能,这里简单说明一下shell数组的使用方法: 初始化方法 _array_name[0]="rando ...

  8. JavaScript 实现彩票中随机数组的获取

    1.效果图: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. Linux Shell中的数组及遍历 转

    转自:http://www.linuxidc.com/Linux/2011-09/42929.htm 在Linux下使用shell的时候,为方便起见,偶尔会用到一下数组.数组的申明方式是: array ...

随机推荐

  1. Debian安装中文输入法

    简单暴力: apt-get install ibus-pinyin 你也可以通过GUI下面到Synaptic Package Manager里面选中ibus-pinyin进行安装 安装完成后重启计算机 ...

  2. iOS中使用RSA对数据进行加密解密

    RSA算法是一种非对称加密算法,常被用于加密数据传输.如果配合上数字摘要算法, 也可以用于文件签名. 本文将讨论如何在iOS中使用RSA传输加密数据. 本文环境 mac os openssl-1.0. ...

  3. 如何将NTFS格式的移动硬盘挂接到Mac OS上进行读写(Read/Write)操作

    现在硬盘便宜,很多同学都有移动硬盘,如果你同时使用Windows与Mac OS的话,移动硬盘最好不要使用NTFS文件系统,否则在Mac OS上,你只能读你的移动硬盘,不能写. 但是实际上的情况是,移动 ...

  4. android 中打 Log 的一些技巧

    在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...

  5. QT QT程序初练

    //界面编程#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) ...

  6. MVC中利用ActionFilterAttribute过滤关键字

    在开发过程中,有时候会对用户输入进行过滤,以便保证平台的安全性.屏蔽的方法有很多种,但是今天我说的这种主要是利用MVC中的ActionFilterAttribute属性来实现.由于MVC天然支持AOP ...

  7. [MetaHook] Load DTX texture to OpenGL

    This function load a LithTech *.dtx texture file and convert to OpenGL pixel format, compressed supp ...

  8. SQL脚本循环修改数据库字段类型

    数据库在设计的时候也许考虑不全面,导致某些字段类型不太准确.比如设计的时候是varchar(1024),但是实际使用的时候却发现太小了,装不下,于是需要修改字段类型为ntext什么的. 我最近就遇到了 ...

  9. libtool: Version mismatch error 解决

    在编译一个软件的时候,在 ./configure 和 make  之后可能会出现如下错误: libtool: Version mismatch error.  This is libtool 2.4. ...

  10. Git.Framework 框架随手记--历史原因

    Git.Framework 是近几年工作的一些工作经验总结,虽不能和某些知名的框架相提并论,但是还是比较实用的.此框架经过三年多的升级和维护,已经具有较强的实用性,在此记录该框架的使用操作方式,贡献给 ...