bash shell 中数组使用举例
发布时间:2020-12-15 22:40:33 所属栏目:安全 来源:网络整理
导读:bash shell 中数组使用举例 一 背景 让我们先来看一个 shell 脚本的执行过程及结果: [[email?protected] ~]$ sh array.sh N2 N3 N4The elements of this array 2-4 are: N2 N3 N4N1 is in array. N2 is in array. N3 is in array. N4 is in array. The orig
bash shell 中数组使用举例
一 背景让我们先来看一个 shell 脚本的执行过程及结果: [[email?protected] ~]$ sh array.sh N2 N3 N4 The elements of this array 2-4 are: N2 N3 N4 N1 is in array. N2 is in array. N3 is in array. N4 is in array. The original array is as follows: N1 N2 N3 N4 The length of this array is 4. The array[2] is N3. Append an element at the end of this array. This array: N1 N2 N3 N4 N5 Modify an element in an array. This array: N1 N2 N6 N4 N5 二 实现实现脚本如下: #!/bin/bash array=(‘N1‘ ‘N2‘ ‘N3‘ ‘N4‘) case $1 in ${array[0]}) echo "${array[0]}" ;; ${array[@]:1:3}) echo "The elements of this array 2-4 are: ${array[@]:1:3}" ;; *) echo "ERROR" ;; esac for num in ${array[@]} ;do echo "${num} is in array. " done echo "The original array is as follows: ${array[@]}" echo "The length of this array is ${#array[*]}. " echo "The array[2] is ${array[2]}. " array[${#array[@]}]=N5 echo "Append an element at the end of this array. This array: ${array[@]}" array[2]=N6 echo "Modify an element in an array. This array: ${array[*]}" 三 总结3.1 这个例子实现了数组的各种用法,我们可以通过执行结果进行直观理解。需要注意的是子数组的获取,元素的修改,追加。3.2 shell 数组的使用与其他编程语言有所不同,可以类比理解。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- bash – `sed -i -e`和`sed -ie`有什么区别?
- angularJs通过过滤器实现获取数据字典
- scala – Spark GraphX聚合求和
- Angular中嵌套路由中的多个布局(2)
- bash – 具有输入和管理员权限的Applescript Execute Shell
- Laravel学习笔记之bootstrap源码解析
- twitter-bootstrap – Bootstrap 3.0的Iconpicker – 使用自
- 如何做“diff -r”的UNIX在Windows Cmd提示?
- twitter-bootstrap – 如何在引导模式Angular 4中正确显示谷
- 作为MQ Message的监听器的WCF / WebService?