One command lets you understand the common parameters of the read command in the shell

One command lets you understand the common parameters of the read command in the shell

We know that there are two ways to receive incoming parameters in Shell. One is to pass parameters through the script, and the other is to receive the incoming parameters through read. A simple example of passing parameters through a script is as follows:

# Pass through the script, where $0 refers to the script name, $1 is the first parameter, and $2 is the second parameter [root@host ~]# ./script.sh 1 2
Total = 3
[root@host ~]# vim script.sh
#!/bin/bash
function add() {
  total=$(expr $1 + $2)
  echo -e "Total = $total"
}
add $1 $2

Let's look at receiving the incoming parameters through read. First, look at the basic format of read:

read [-rs] [-a ARRAY] [-d delim] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [var1 var2 var3......]

[root@host ~]# ./script.sh 
Enter Password:
The password your input is: Test@1234\
[root@host ~]# vim script.sh
#!/bin/bash
read -n10 -t30 -r -s -d $ -p "Enter Password:" password
echo -e "\nThe password your input is:$password"
  • -p prompt statement, followed by input prompt information, here is 'Enter Password: '
  • -n parameter number, sometimes you need to limit the password length, or other input length restrictions, such as [Y/N], only enter one input, -n1
  • -s Shield echo, the input content is not displayed on the screen, generally used for password input
  • -t Waiting time, here set to 30 seconds, if no input is made within 30 seconds or the input is incomplete, the system will terminate.
  • -d Input limit, here is $, input to $, natural termination of input
  • -r blocks the translation function of special characters \, and treats them as ordinary characters after adding them

From the above example, most of the common functions above are basically covered, especially the -p, -n, -t, -s and other parameters, which can be used to learn the read command well.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Examples of receiving pipeline parameters in PowerShell functions
  • Shell script uses for loop to traverse parameters
  • Introduction to common parameters and judgment commands in shell
  • How to use arrays as function parameters in shell (detailed explanation)
  • Comparison of 3 methods of passing parameters in Shell scripts
  • How to use two dashes in shell script to receive external parameters

<<:  How to create WeChat games with CocosCreator

>>:  Solution to Chinese garbled characters when operating MySQL database in CMD

Recommend

How to configure eureka in docker

eureka: 1. Build a JDK image Start the eureka con...

Teach you how to use vscode to build a react-native development environment

question The code has no prompt: Many non-front-e...

How to use libudev in Linux to get USB device VID and PID

In this article, we will use the libudev library ...

JavaScript to achieve calendar effect

This article shares the specific code for JavaScr...

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

Example steps for using AntV X6 with Vue.js

Table of contents 0x0 Introduction 0x1 Installati...

React+TypeScript project construction case explanation

React project building can be very simple, but if...

MySQL time type selection

Table of contents DATETIME TIMESTAMP How to choos...

MySQL 8.0.13 installation and configuration tutorial under CentOS7.3

1. Basic Environment 1. Operating system: CentOS ...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

vue+element-ui implements the head navigation bar component

This article shares the specific code of vue+elem...

Linux uses if to determine whether a directory exists.

How to use if in Linux to determine whether a dir...