博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell编程系列4--有类型变量:字符串、只读类型、整数、数组
阅读量:6973 次
发布时间:2019-06-27

本文共 2026 字,大约阅读时间需要 6 分钟。

shell编程系列4--有类型变量:字符串、只读类型、整数、数组有类型变量总结:declare命令和typeset命令两者等价declare、typeset命令都是用来定义变量类型的declare命令参数总结1.declare -r 将变量设置为只读类型declare -r var="hello"var="world" # 变量默认可以修改[root@es01 shell]# var2="hello world"[root@es01 shell]# var2="hello python"[root@es01 shell]# echo $var2hello python# 声明为只读变量,就不可修改[root@es01 shell]# declare -r var2[root@es01 shell]# var2="hello java"-bash: var2: readonly variable2. declare -i 将变量设为整数# 默认把变量当做字符处理[root@es01 shell]# num1=10[root@es01 shell]# num2=$num1+20[root@es01 shell]# echo $num210+20# 声明为整数[root@es01 shell]# declare -i num3[root@es01 shell]# num3=$num1+90[root@es01 shell]# echo $num31003.declare -a 将变量定义为数组# 定义数组[root@es01 shell]# declare -a array[root@es01 shell]# array=("jones" "make" "kobe" "jordan")# 列出数组所有元素[root@es01 shell]# echo ${array[@]}jones make kobe jordan[root@es01 shell]# echo ${array[1]}make[root@es01 shell]# echo ${array[0]}jones[root@es01 shell]# echo ${array[2]}kobe# 数组长度[root@es01 shell]# echo ${#array[@]}4# 输出数组中元素长度[root@es01 shell]# echo ${#array[0]}5[root@es01 shell]# echo ${#array[1]}4-f 显示此脚本前定义过的所有函数和内容-F 进显示脚本前定义过的函数名数组常用的方法(仅供参考,实际生产用的少)array=("jones" "mike" "kobe" "jordan")输出数组内容:    echo ${array[@]}    输出全部内容    echo ${array[1]}    输出下标索引为1的内容获取数组长度:    echo ${#array}        数组内元素个数    echo ${#array[2]}    数组内下标索引为2的元素长度给数组某个下标赋值:    array[0]="lily"        给数组下标索引为1的元素赋值为lily    array[20]="hanmeimei"    在数组尾部添加一个新元素删除元素:    unset array[2]        清空元素    unset array        清空整个数组分片访问:    ${array[@]:1:4}        显示数组下标索引从1开始到3的3个元素内容替换:    ${array[@]/an/AN}    将数组中所有元素包含an的子串替换为AN数组遍历:    for v in ${array[@]}    do        echo $v    done4.declare -x 将变量声明为环境变量[root@es01 shell]# num5=30[root@es01 shell]# echo $num530[root@es01 shell]# vim test1.sh[root@es01 shell]# cat test1.sh #!/bin/bash#echo $num5# 在脚本中直接使用shell环境中定义的变量是无法引用的[root@es01 shell]# sh test1.sh # 当使用declare -x 变量后,就可以直接在脚本中引用了[root@es01 shell]# declare -x num5[root@es01 shell]# sh test1.sh 30declare +r 取消一个变量

 

转载于:https://www.cnblogs.com/reblue520/p/10919004.html

你可能感兴趣的文章
windows系统安装MongoDB
查看>>
[转]Peer-to-Peer Communication Across Network Address Translators
查看>>
C++临时变量的生命周期
查看>>
Remove Element
查看>>
高淇Struts2.0教程之视频笔记(7)
查看>>
自适应SimpsonSimpson积分
查看>>
初学WebGL引擎-BabylonJS:第2篇-基础模型体验
查看>>
Python的垃圾回收机制以及引用计数
查看>>
C语言经典实例1: 类型长度与类型转换
查看>>
用DateTime.ToString(string format)输出不同格式的日期
查看>>
[转July]KMP算法(mark)
查看>>
mysql获取表列信息、主键信息
查看>>
用POI的HSSF来控制EXCEL的研究
查看>>
jvm09
查看>>
require标识符分析
查看>>
随手记
查看>>
基础JSP学习
查看>>
C# try catch语句&获取随机数的方法
查看>>
第一个shell编程,输出hello world!
查看>>
LinearLayout的layout_gravity失效问题
查看>>