(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. jquery 实现邮箱输入自动提示功能:(二)

    上篇文章写到了一个不错的jquery实现邮箱输入自动提示功能,发现还有一个不错的自动提示插件: 先展示结果如图: html代码: <center> <h1>输入邮箱试试!< ...

  2. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

  3. Redis 一二事 - 在spring中使用jedis 连接调试单机redis以及集群redis

    Redis真是好,其中的键值用起来真心强大啊有木有, 之前的文章讲过搭建了redis集群 那么咋们该如何调用单机版的redis以及集群版的redis来使用缓存服务呢? 先讲讲单机版的,单机版redis ...

  4. 我为什么反对推荐新人编程C/C++语言入门?

    虽然我接触编程以及计算机时间比较早,但是正式打算转入程序员这个行当差不多是大学第四年的事情 从03年接触计算机,07年开始接触计算机编程, 期间接触过的技术包括 缓冲区溢出(看高手写的shellcod ...

  5. struts2验证框架

    如何做一个工号 用户 密码 验证登录页面? 答:1,先画一个login.jsp ,如何画呢?先引入Struts2标签库,利用Struts2标签库画登录页面:如下: 2,先进入useractiion,在 ...

  6. 替换空格-请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

    class Solution { public: void replaceSpace(char *str,int length) { char *tmp; ; int i; ;i<length; ...

  7. hp_jetdirect 9100漏洞检测

    #-*-coding=utf8-*- import socket import sys def main(): if len(sys.argv)<=1: print('Parameters er ...

  8. Linux下Qt的安装与配置

    参考资料:http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html Linux 下编译.安装.配置 QT 下载qt 这里用的是4.7. ...

  9. Linux常用指令---ps(查看进程)

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  10. [CareerCup] 8.6 Jigsaw Puzzle 拼图游戏

    8.6 Implement a jigsaw puzzle. Design the data structures and explain an algorithm to solve the puzz ...