当前位置:网站首页>shell 内置命令
shell 内置命令
2022-06-25 13:32:00 【响彻天堂丶】
1 Read
1.1 介绍
read 是 Shell 内置命令,用于从标准输入中读取数据并赋值给变量。如果没有进行重定向,默认就是从终端控制台读取用户输入的数据;如果进行了重定向,那么可以从文件中读取数据。
1.2 语法
read [-options] [var1 var2 ...]
options支持的参数:
| 选项 | 说明 |
|---|---|
| -a array | 把读取的数据赋值给数组 array,从下标 0 开始。 |
| -d delimiter | 用字符串 delimiter 指定读取结束的位置,而不是一个换行符(读取到的数据不包括 delimiter)。 |
| -e | 在获取用户输入的时候,对功能键进行编码转换,不会直接显式功能键对应的字符。 |
| -n num | 读取 num 个字符,而不是整行字符。 |
| -p prompt | 显示提示信息,提示内容为 prompt。 |
| -r | 原样读取(Raw mode),不把反斜杠字符解释为转义字符。 |
| -s | 静默模式(Silent mode),不会在屏幕上显示输入的字符。 当输入密码和其它确认信息的时候,这是很有必要的。 |
| -t seconds | 设置超时时间,单位为秒。如果用户没有在指定时间内输入完成, 那么 read 将会返回一个非 0 的退出状态,表示读取失败。 |
| -u fd | 使用文件描述符 fd 作为输入源,而不是标准输入,类似于重定向。 |
1.3 demo
1 使用 read 命令给多个变量赋值
#!/bin/bash
read -p "请输入姓名、年龄、爱好:" name age hobby
echo "姓名: ${name}"
echo "年龄: ${age}"
echo "爱好: ${hobby}"

2 读取一个字符
#!/bin/bash
read -n 1 -p "您确定要删除用户的数据吗? (y/n):" char
echo
echo "您输入的字符为: ${char}"

3 校验密码
5秒校验密码是否一致
#!/bin/bash
read -t 5 -sp "输入密码(5秒内):" pwd1
echo
read -t 5 -sp "请确认密码(5秒内):" pwd2
echo
echo "pwd1:${pwd1}, pwd2:${pwd2}"
if
[ $pwd1 == $pwd2 ]
then
echo "密码一致"
else
echo "验证失败,密码不一致"
fi

2 Exit
2.1 介绍
exit 用于退出当前Shell环境进程结束运行, 并且可以返回一个状态码.一般使用 $? 可以获取退出状态码
2.2 用法
# 默认返回状态码0, 一般代表命令执行成功
exit
# 数字建议的范围0~255, 一般代表命令执行失败
exit 非0数字
2.3 demo
#!/bin/bash
echo "hello"
exit
echo "world"

3 declare
declare命令用于声明 shell 变量,实现关联数组变量。
3.1 语法
关联数组也称为“键值对(key-value)”数组,键(key)也即字符串形式的数组下标,值(value)也即元素值。
declare -A 关联数组变量名=([字符串key1]=值1 [字符串key2]=值2 …)
获取指定key的值:
${关联数组变量名[key]}
获取所有值:
${关联数组变量名[*]} # 方式1
${关联数组变量名[@]} # 方式2
3.2 demo
#!/bin/bash
declare -A array=([name]="张三" [age]=14)
echo ${
array[name]}
echo ${
array[age]}
echo ${
array[*]}

边栏推荐
- Settings the PC must be turned on
- Openstack -- creating virtual machines for Nova source code analysis
- K-line diagram 24 classic diagrams (shadow)
- Custom vertical table
- Cesium learning notes
- Some knowledge of the initial C language
- Knowledge of initial C language 2.0
- DE2-115 FPGA开发板的VGA显示
- Hash table, hash conflict
- 多台云服务器的 Kubernetes 集群搭建
猜你喜欢

[pit avoidance refers to "difficult"] antd cascader implements new customized functions

How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?

On the simple realization of Sanzi chess game and the method of judging the victory of n-zi chess

戴尔电脑cpu温度过高怎么办

Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud

Related examples of data storage in memory

OpenStack学习笔记(一)
![[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box](/img/81/f8280f16efa314d736c3a869c32ef0.jpg)
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box

Some knowledge of the initial C language

Test your earning power? What will you do in the future?
随机推荐
API in Nova
Is it safe for Guosen Securities to open an account?
Class usage and inheritance in ES6
Table de hachage, conflit de hachage
leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
Untiy force refresh UI
Qt内存映射
sigmoid函数sigmoid求导
VGA display of de2-115 FPGA development board
Using swiper to realize seamless rotation of multiple slides
作为一名软件测试工程师你认为怎么样才能保证软件质量?
Cold migration and resize of Nova component source code analysis
Scope of ES6 variable
Explanation of a textbook question
Graph contractual learning with augmentations
Windows下MySQL的安装和删除
Syntax 'trap'
英语中的九大词性与九大时态
Network remote access using raspberry pie
Asp. Net webform exporting excel using npoi