当前位置:网站首页>Shell built-in commands
Shell built-in commands
2022-06-25 13:59:00 【Resounding through heaven】
1 Read
1.1 Introduce
read yes Shell Built in commands , Used to read data from standard input and assign it to variable . If there is no redirection , The default is to read the data entered by the user from the terminal console ; If redirected , Then you can read data from the file .
1.2 grammar
read [-options] [var1 var2 ...]
options Supported parameters :
| Options | explain |
|---|---|
| -a array | Assign the read data to the array array, From the subscript 0 Start . |
| -d delimiter | Use string delimiter Specify the position where the reading ends , Instead of a newline ( The data read does not include delimiter). |
| -e | When getting user input , Code conversion of function keys , The character corresponding to the function key will not be directly explicit . |
| -n num | Read num Characters , Instead of a whole line of characters . |
| -p prompt | Display prompt message , The tip is prompt. |
| -r | Read as is (Raw mode), Do not interpret backslash characters as escape characters . |
| -s | silent mode (Silent mode), The entered characters will not be displayed on the screen . When entering passwords and other confirmation information , It is necessary to . |
| -t seconds | Set timeout , The unit is in seconds . If the user does not complete the input within the specified time , that read Will return a non 0 The exit status of , Indicates a read failure . |
| -u fd | Use file descriptors fd As an input source , Instead of standard input , Similar to redirection . |
1.3 demo
1 Use read Command assigns values to multiple variables
#!/bin/bash
read -p " Please enter a name 、 Age 、 hobby :" name age hobby
echo " full name : ${name}"
echo " Age : ${age}"
echo " hobby : ${hobby}"

2 Read a character
#!/bin/bash
read -n 1 -p " Are you sure you want to delete the user's data ? (y/n):" char
echo
echo " The character you entered is : ${char}"

3 Check the password
5 Seconds to verify whether the passwords are consistent
#!/bin/bash
read -t 5 -sp " Input password (5 Seconds ):" pwd1
echo
read -t 5 -sp " Please confirm the password (5 Seconds ):" pwd2
echo
echo "pwd1:${pwd1}, pwd2:${pwd2}"
if
[ $pwd1 == $pwd2 ]
then
echo " The password is the same "
else
echo " Validation failed , Password inconsistency "
fi

2 Exit
2.1 Introduce
exit Used to exit the current Shell The environment process ends running , And you can return a status code . In general use $? You can get the exit status code
2.2 usage
# The default return status code 0, Generally, it means that the command is executed successfully
exit
# Range of digital recommendations 0~255, Generally, it means that the command execution fails
exit Not 0 Numbers
2.3 demo
#!/bin/bash
echo "hello"
exit
echo "world"

3 declare
declare The command is used to declare shell Variable , Implement associative array variables .
3.1 grammar
Associative arrays are also called “ Key value pair (key-value)” Array , key (key) That is, array subscript in string form , value (value) That is, the element value .
declare -A Associative array variable name =([ character string key1]= value 1 [ character string key2]= value 2 …)
Get specified key Value :
${ Associative array variable name [key]}
Get all values :
${ Associative array variable name [*]} # The way 1
${ Associative array variable name [@]} # The way 2
3.2 demo
#!/bin/bash
declare -A array=([name]=" Zhang San " [age]=14)
echo ${
array[name]}
echo ${
array[age]}
echo ${
array[*]}

边栏推荐
- 数据采集系统网关采集工厂效率
- Rust, le meilleur choix pour un programmeur de démarrer une entreprise?
- Gorm-- search you don't know
- 楼宇自动化专用BACnet网关BL103
- Rust, the best choice for programmers to start a business?
- The priority of catch() and then (..., ERR) of promise
- 对白:推荐系统快速入门路线及各知识点总结
- 网络远程访问的方式使用树莓派
- Cesium learning notes
- Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
猜你喜欢

Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched

使用调试工具调试博图TCP连接所遇到的问题

關於一道教材題的講解

DE2-115 FPGA开发板的VGA显示

Rust, le meilleur choix pour un programmeur de démarrer une entreprise?

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

Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板

The starting point for learning programming.

Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world

Getting started with numpy Library
随机推荐
‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
多台云服务器的 Kubernetes 集群搭建
Application of tactile intelligent sharing-rk3568 in financial self-service terminal
Openstack learning notes -nova component insight
Windows下MySQL的安装和删除
Beego--- notes
As a software testing engineer, how do you think to ensure software quality?
sql导入这样怎么解决
Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud
Method for wrapping multiple promise instances into a new promise instance
使用调试工具调试博图TCP连接所遇到的问题
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
Regular match the phone number and replace the fourth to seventh digits of the phone number with****
【Proteus仿真】51单片机+DS1302+lcd1602显示
Go--- route filter
OpenStack学习笔记(二)
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行
Network remote access using raspberry pie
Realization of neural networks with numpy