shell编程系列4--有类型变量:字符串、只读类型、整数、数组

有类型变量总结:

declare命令和typeset命令两者等价

declare、typeset命令都是用来定义变量类型的

declare命令参数总结

.declare -r 将变量设置为只读类型
declare -r var="hello"
var="world" # 变量默认可以修改
[root@es01 shell]# var2="hello world"
[root@es01 shell]# var2="hello python"
[root@es01 shell]# echo $var2
hello python # 声明为只读变量,就不可修改
[root@es01 shell]# declare -r var2
[root@es01 shell]# var2="hello java"
-bash: var2: readonly variable . declare -i 将变量设为整数
# 默认把变量当做字符处理
[root@es01 shell]# num1=
[root@es01 shell]# num2=$num1+
[root@es01 shell]# echo $num2
+ # 声明为整数
[root@es01 shell]# declare -i num3
[root@es01 shell]# num3=$num1+
[root@es01 shell]# echo $num3 .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[]}
make
[root@es01 shell]# echo ${array[]}
jones
[root@es01 shell]# echo ${array[]}
kobe # 数组长度
[root@es01 shell]# echo ${#array[@]} # 输出数组中元素长度
[root@es01 shell]# echo ${#array[]} [root@es01 shell]# echo ${#array[]} -f 显示此脚本前定义过的所有函数和内容
-F 进显示脚本前定义过的函数名 数组常用的方法(仅供参考,实际生产用的少) array=("jones" "mike" "kobe" "jordan")
输出数组内容:
echo ${array[@]} 输出全部内容
echo ${array[]} 输出下标索引为1的内容 获取数组长度:
echo ${#array} 数组内元素个数
echo ${#array[]} 数组内下标索引为2的元素长度 给数组某个下标赋值:
array[]="lily" 给数组下标索引为1的元素赋值为lily
array[]="hanmeimei" 在数组尾部添加一个新元素 删除元素:
unset array[] 清空元素
unset array 清空整个数组 分片访问:
${array[@]::} 显示数组下标索引从1开始到3的3个元素 内容替换:
${array[@]/an/AN} 将数组中所有元素包含an的子串替换为AN 数组遍历:
for v in ${array[@]}
do
echo $v
done .declare -x 将变量声明为环境变量 [root@es01 shell]# num5=
[root@es01 shell]# echo $num5 [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 declare +r 取消一个变量

shell编程系列4--有类型变量:字符串、只读类型、整数、数组的更多相关文章

  1. shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计

    shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计 shell中的数组的用法: shell数组中的下标是从0开始的 array=("Allen" & ...

  2. shell编程系列19--文本处理三剑客之awk中的字符串函数

    shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...

  3. (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单

    原文 (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 接上一节:(C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开这 ...

  4. shell编程系列26--大型脚本工具开发实战

    shell编程系列26--大型脚本工具开发实战 大型脚本工具开发实战 拆分脚本功能,抽象函数 .function get_all_group 返回进程组列表字符串 .function get_all_ ...

  5. shell编程系列16--文本处理三剑客之awk模式匹配的两种方法

    shell编程系列16--文本处理三剑客之awk模式匹配的两种方法 awk的工作模式 第一种模式匹配:RegExp 第二种模式匹配:关系运算匹配 用法格式对照表 语法格式 含义 RegExp 按正则表 ...

  6. shell编程系列15--文本处理三剑客之awk格式化输出printf

    shell编程系列15--文本处理三剑客之awk格式化输出printf printf的格式说明符 格式符 含义 %s 打印字符串 %d 打印十进制数 %f 打印一个浮点数 %x 打印十六进制数 %o ...

  7. shell编程系列9--文本处理三剑客之sed概述及常见用法总结

    shell编程系列9--文本处理三剑客之sed概述及常见用法总结 sed的工作模式:对文本的行数据一行行处理,如下图 sed(stream editor),是流编辑器,依据特定的匹配模式,对文本逐行匹 ...

  8. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

  9. shell编程系列2--字符串的处理

    shell编程系列2--字符串的处理 字符串的处理 .计算字符串的长度 方法1 ${#string} 方法2 expr length "$string" (如果string中间有空 ...

随机推荐

  1. [software test - 001] Why we need software test?

    /*  This is a conclusion about the software testing job. */ /*  Scope: middle level software tasks,  ...

  2. SpringBoot -生成Entity和Dto互转的双向枚举类 -使用注解@Mapper(componentModel = "spring")

    1.导入pom文件 ,版本号自定 <!--mapStruct依赖--> <dependency> <groupId>org.mapstruct</groupI ...

  3. redis的生产环境中的部署?

    使用的是redis cluster 10台机器,5台机器部署了redis主实例,另外5台机器部署了redis 的从实例,每个主实例挂了一个从实例,5个节点对外提供读写服务,每个节点的读写高峰qps可能 ...

  4. 02 c++的封装性 (构造和析构)

    封装性: 关键字:public private protected 破坏封装:友元函数 friend 实现数据的隐藏:class类 默认是私有,结构体默认是公有. 类和对象:(定义类的注意事项) 在类 ...

  5. Go语言 - 数组 | 多维数组

    Array 数组是同一种数据类型元素的集合. 在Go语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化. 1.数组 在定义阶段,长度和类型就固定了,以后不能更改 2.长度也是数组 ...

  6. 2019CCPC网络赛 HD6707——杜教筛

    题意 求 $f(n,a,b)=\sum_{i=1}^n \sum_{j=1}^i gcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1]\%(10^9+7)$,$1 \le n,a,b \l ...

  7. 微信小程序 图片加载失败处理方法

    微信小程序 官方文档对image 媒体组件加载失败 没有太多的解释,使用中出现了几个小问题,今天抽空记录一下 WXML: <image class="userinfo-avatar&q ...

  8. php 简单使用redis 队列示例

    public function redisAction(){ $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo &qu ...

  9. BAT 批量执行SQL脚本

    需要在BAT的sqlcmd中设置数据库连接信息. https://files.cnblogs.com/files/gguozhenqian/BAT%E6%89%A7%E8%A1%8CSQL%E8%84 ...

  10. mongo最大连接数查看

    进入客户端 mongo 输入查看命令 db.serverStatus().connections