Shell knowledge point supplement (1)-Prompt character setting/read/declare/typeset/variable setting function/alias/universal character and special symbol

Source: Internet
Author: User
Tags uppercase character

1. PS1: (set the prompt character)

This is interesting. You can customize your own prompt;

Every time we press the [enter] key to execute a command, the prompt character will appear again, and we will take the initiative to read the variable value.

The special symbols in the preset bash PS1 variable represent the meaning:

O \ D: represents the date, in the format of weekday month date, for example, "Mon Aug 1"
O \ H: complete host name. For example, the host name is linux.dmtsai.tw.
O \ H: only the first name of the host name. As mentioned above, it is just Linux, and .dmtsai.tw is omitted.
O \ t: display time, in the 24-hour format, for example, HH: mm: SS
O \ t: display time, 12-hour time format!
O \ A: display time, in 24-hour format, HH: mm
O \ U: Account name of the current user;
O \ V: Bash version information;
O \ W: complete working directory name. In the home directory ~ Replace;
O \ W: Use basename to get the working directory name. Therefore, only the last directory name is listed.
O \ #: commands issued.
O \ $: the prompt character. If it is root, the prompt character is #; otherwise, it is $ r ~

OK! Therefore, the preset PS1 content is '\ [\ U @ \ H \ W \] \ $' to understand why our prompt character is: [root @ Linux ~] ! Well, suppose I want to have the following prompt characters:
[Root @ Linux/home/dmtsai 16:50 #12] #
That # represents the 12th commands. So how should we set PS1? You can do this:
[Root @ Linux Home] # PS1 = '[\ U @ \ H \ W \ A #] \ $'
[Root @ Linux/home 17:02 #85] #

# Have you seen it? The prompt character has changed! Interesting! Among them, the #85 is interesting.

2. Read

To read the variables input from the keyboard, use the READ command. This command is most often used in shell script writing for discussion with users. For the script writing method, we will introduce it in later chapters. Let's take a look at the read-related syntax!
[Root @ Linux ~] # Read [-pt] variable
Parameters:
-P: Followed by a prompt character!
-T: the number of seconds that can be followed !』 This is interesting ~ Will not wait for the user all the time!
Example:
Example 1: Let the user input a content on the keyboard and change the content to the atest variable.
[Root @ Linux ~] # Read atest
This is a test
[Root @ Linux ~] # Echo $ atest
This is a test
Example 2: prompt the user to enter his name within 30 seconds and make the input string a named variable.
[Root @ Linux ~] # Read-P "Please keyin your name:"-T 30 named
Please keyin your name: vbird Tsai
[Root @ Linux ~] # Echo $ named
Vbird Tsai
Add the variable name directly without adding any parameters after the read operation. A blank line appears, waiting for your input. If-T is followed by a second, for example, in the above example, if there is no action within 30 seconds, the command will automatically skip ~ If-P is added, the prompt information can be added;

Practical supplement:

Function
Read a row from the standard input.

Syntax
Read [-p] [-R] [-S] [-U [N] [variablename? Prompt]

[Variablename...]

Description
The read command reads a line from the standard input and specifies the value of each field in the input line to the shell variable. The characters in the IFS (internal field separator) variable are used as separators. The variablename parameter specifies the name of the shell variable. The shell variable obtains the value of a field in the input line. The first shell variable specified by the variablename parameter specifies the value of each field, the second shell variable specified by the variablename parameter specifies the value of the second field, and so on until the last field. If the fields in the standard input line are more than the corresponding shell specified by the variablename parameter
When there are many variables, the values of all the remaining fields are assigned to the specified final shell variable. If it is less than the number of shell variables, the remaining shell variables are set as empty strings.

Note: If the variablename parameter is omitted, reply is used as the default variable name.
The shell variable set by the READ command affects the current shell execution environment.

Flag

-P reads the output of processes running by the Korn shell using the | & (pipeline, & mark name.

Note: The file terminator of the-p Mark causes the process to be cleared, so another process is generated.
-R specifies that the READ command processes a \ (backslash) as a part of the input line without using it as a control character.
-S saves the input as a command in the Korn shell history file.
-U [N] reads a single-digit file descriptor number N as the input. You can use the built-in command KSh exec to open the file descriptor. The default value of N is 0, indicating the keyboard. Value 2 indicates a standard error.

Parameters

Variablename? Prompt specifies the name of a variable and the prompt to be used. When the Korn shell is interactive, it writes the prompt to a standard error and runs the input. The prompt contains more than one word and must be enclosed in single or double quotation marks.
Variablename... specifies one or more variable names separated by spaces.

Exit status
This command returns the following export values:

0.
> 0: The file terminator or an error is detected.

Example
The following script prints a file in which the first field in each row is moved to the end of the row.

While read-r XX YY
Do
Print printf "% S % s/n" $ YY $ XX
Done <inputfile reads a row, divides it into fields, and uses "Please enter:" As the prompt. Enter:

Read word1? "Please enter:" The word2 System displays:

Please enter:
You enter:
The value of the hello World variable word1 should be "hello", and the variable word2 should be "World ."

To create a common process, use print-P to write it to the common process, and read the input from the common process with read-P. Enter:

(Read; print "Hello $ reply ")
Print-P "world"
The line value of the read-P line variable should be "Hello world ."

To save the copy of the input line as a command in the history file, enter:

Read-s line <input_file if the input file contains "Echo Hello World,", "Echo Hello World" will be saved as a command in the history file.

3. Declare/typeset

Declare or typeset are the same function, that is, declaring the attributes of a variable. If declare is followed by no parameters, Bash will actively call out all the variable names and contents, just like using set! So what syntax does declare have? Let's see first:
[Root @ Linux ~] # Declare [-aixr] variable
Parameters:
-A: defines variable as an array)
-I: defines the variable that follows as an integer)
-X: similar to export, it is used to change the subsequent variable to an environment variable;
-R: Set a variable to readonly. The variable cannot be changed or unset.
Example:
Example 1: Let the sum variable calculate the sum of 100 + 300 + 50.
[Root @ Linux ~] # Sum = 100 + 300 + 50
[Root @ Linux ~] # Echo $ sum
100 + 300 + 50 <= large! Why didn't I calculate the sum for me? This is the variable attribute of the text type!
[Root @ Linux ~] # Declare-I sum = 100 + 300 + 50
[Root @ Linux ~] # Echo $ sum
450 <== what ??
Example 2: Change sum to an environment variable
[Root @ Linux ~] # Declare-x Sum
Example 3: Change sum to a read-only attribute!
[Root @ Linux ~] # Declare-r Sum
[Root @ Linux ~] # Sum = tesgting
-Bash: sum: readonly variable <= god ~ You cannot change this variable!
Declare is also a useful feature ~ Especially when we need to use the Array Function, it can also help us declare the attribute of the array! However, arrays are also commonly used in shell scripts.

4. Additional variable setting functions

We have just mentioned two methods for getting variables:
[Root @ Linux ~] # Echo $ home
[Root @ Linux ~] # Echo $ {home}
In the usage of $ {Variable}, we can also revise the variable! You only need to add some character signs and then use the comparison string to modify the variable content! In the following example, assume that my variable is named vbird and the content is/home/vbird/testing. X. Sh.
1. Completely present the content of the vbird variable;
[Root @ Linux ~] # Vbird = "/home/vbird/testing. X. Sh"
[Root @ Linux ~] # Echo $ {vbird}
/Home/vbird/testing. X. Sh
2. In the vbird variable, compare from the beginning. If the start is/, delete two/
All data between, that is /*/
[Root @ Linux ~] # Echo $ {vbird ##/*/}
Testing. X. Sh <= deleted/home/vbird/testing/
[Root @ Linux ~] # Echo $ {vbird #/*/}
Vbird/testing. X. Sh <= delete/home/only
# These two small examples are interesting ~ If the variable name is followed by two ##, it indicates ##
# The string following is the "longest" segment. If there is only one # segment, it indicates the "smallest segment!
3. Answer the above question. If it is from the end, delete?
[Root @ Linux ~] # Echo $ {vbird % /*/}
/Home/vbird/testing. X. Sh <= not deleted
[Root @ Linux ~] # Echo $ {vbird % /*}
<= Deleted!
[Root @ Linux ~] # Echo $ {vbird % /*}
/Home/vbird/testing <= Delete only the/testing. X. Sh part
# In this example, note that % compares the meaning of "last character,
# So, the first method is definitely incorrect ~ Because the content of the vbird variable is H rather!
# As for %/*, the "Longest/*" is deleted. Of course, it is all! %/* Is the shortest one!
4. Replace testing in the vbird variable with test
[Root @ Linux ~] # Echo $ {vbird/testing/test}
/Home/vbird/test/testing. X. Sh
[Root @ Linux ~] # Echo $ {vbird // testing/test}
/Home/vbird/test. X. Sh
# If the variable is followed by a slash (/), it indicates that the variable is replaced ~ And only Replace "first 』
# But if it is //, it means all strings are replaced!
Please pay attention to it ~ Anyway, the variables can be followed by #, #, %, %,/, //, and they have different meanings ~ In addition, the content of several different variables can be determined! For example, I need to use two variables, VaR and STR, respectively. Then I want to determine whether the variable content of STR is set to a string, that is, "expr" determines the content of var. So what method can I use to determine? If you can write shell scripts, you can simply use shell scripts. If you can't write them, let's use simple variables!
TIPS: In the following example, the VaR and STR are variables. We want to determine the value of VaR Based on the settings of Str! In general, STR: indicates "When STR is not set or is null"; as for STR, it is only "without this variable 』.

Variable setting method: STR is not set as an empty string. STR is set as a non-empty string.
VaR =$ {str-expr} Var = expr Var = $ Str
VaR =$ {STR:-expr} Var = expr Var = $ Str
VaR =$ {STR + expr} Var = expr
VaR =$ {STR: + expr} Var = expr
VaR =$ {STR = expr} STR = expr; Var = expr STR unchanged Var = $ Str
VaR =$ {STR: = expr} STR = expr Var = expr STR unchanged Var = $ Str
VaR =$ {Str? Expr} expr output to stderr Var = Str
VaR =$ {STR :? Expr} expr output to stderr Var = Str

Based on the above table, let's take a few practical exercises.
Example 1: If the content of the STR variable exists, VaR is set to STR; otherwise, VaR is set to "newvar"
[Root @ Linux ~] # Unset STR; var =$ {str-newvar}
[Root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str"
Var = newvar, STR = <== because STR does not exist, VaR is newvar
[Root @ Linux ~] # STR = "oldvar"; var =$ {str-newvar}
[Root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str"
Var = oldvar, STR = oldvar <= Because STR exists, VaR is equal to STR content.
Example 2: If STR does not exist, both VaR and STR are set to newvar; otherwise, only VaR is newvar.
[Root @ Linux ~] # Unset STR; var =$ {STR = newvar}
[Root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str"
Var = newvar, STR = newvar <= Because STR does not exist, var/STR are both newvar
[Root @ Linux ~] # STR = "oldvar"; var =$ {STR = newvar}
[Root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str"
Var = oldvar, STR = oldvar <= Because STR exists, VaR is equal to STR content.
Example 3: If the STR variable exists, VaR is equal to STR; otherwise, "novar" is output"
[Root @ Linux ~] # Unset STR; var =$ {Str? Novar}
-Bash: Str: novar <= the error message is output because STR does not exist.
[Root @ Linux ~] # STR = "oldvar"; var =$ {Str? Novar}
[Root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str"
Var = oldvar, STR = oldvar <= Because STR exists, VaR is equal to STR content.
# None of the above cases mentioned when STR is set and it is a null string!
# You can test it on your own!
Although I think there is nothing special about variables, if you take a closer look, hey! A bunch of environment variables and system resource variables will affect whether we can run smoothly in bash! For example, path, ulimit, and so on ~

Special variables:

$ * All parameters of this program, such as the command LS-Al Dir, represent-Al dir

$? If the return value of the previous command is 0, the execution is successful!

$! PID used to execute the previous background command

$ PID of the program

$ # Number of parameters of this program
For example, the command LS-Al dir represents two

5. Alias

Add alias:

LOONG:/home/Yee/shell # Alias Vi = "Vim"
LOONG:/home/Yee/shell # lm
Bash: LM: Command not found
LOONG:/home/Yee/shell # Alias LM = "Vim"
LOONG:/home/Yee/shell # lm kkkk.txt

Cancel alias:

LOONG:/home/Yee/shell # una
Unalias uname
LOONG:/home/Yee/shell # unalias LM
LOONG:/home/Yee/shell # lm
Bash: LM: Command not found
LOONG:/home/Yee/shell #

6. Universal characters and special characters:


! In bash, it also supports some million-character (wild card )! With these 10 million characters more, it is easier for us to use Bash to process data! Below we will list some common universal characters:
Symbol
Content

  • *
  • A string of 0 or more characters (or numbers)
  • ?
  • A ten-character string that represents a letter "must have"
  • #
  • Annotation, which is most often used in scripts!
  • \
  • Returns the special character or character to a normal character.
  • |
  • Define two pipeline commands;
  • ;
  • Definition of continuity commands (note! Different from pipeline commands)
  • ~
  • User's home directory
  • $
  • That is, the variable value to be added before the variable
  • &
  • Turn commands into background work
  • !
  • The meaning of "Non" not in the logic operation sense!
  • /
  • Path Separator
  • >,>>
  • Output orientation: replace and accumulate 』
  • '
  • Single quotes, no variable replacement function
  • "
  • Has the function of variable replacement!
  • ''
  • There are two "'" commands that can be executed first!
  • ()
  • In the middle is the start and end of the sub-shell
  • []
  • Character combination in the middle
  • {}
  • A combination of command blocks in the middle!
  • Combination buttons
  • Execution result
  • CTRL + c
  • Terminate the current command
  • CTRL + d
  • Enter the end (EOF), for example, when the email ends;
  • CTRL + m
  • Enter!
  • CTRL + S
  • Pause screen output
  • CTRL + q
  • Restore screen output
  • CTRL + u
  • Delete the entire command column with the prompt characters
  • CTRL + z
  • Pause the current command

[Root @ Linux ~] # Ls test * <= That * indicates that all the subsequent characters are accepted.
[Root @ Linux ~] # Ls test? <== Which? It indicates that the character "must" must be followed by "one"
[Root @ Linux ~] # Ls test ??? <== That ??? Represents the character "must be followed by three!
[Root @ Linux ~] # Cp test [1-5]/tmp
# Copy test1, Test2, test3, test4, and test5 to/tmp if they exist.
[Root @ Linux ~] # Cp test [! 1-5]/tmp
# As long as it is not test1, Test2, test3, test4, test5, other test? ,
# If it exists, copy it to/tmp
[Root @ Linux ~] # Cd/lib/modules/'uname-R'/kernel/Drivers
# The content enclosed by ''will be executed first 』
The examples above are quite interesting! Especially the last two! Note that in [1-5], "only one character is represented", but the range can be 1-5. In this case, if we allow the file to be copied as long as the file name contains at least one uppercase character, we can do this:
Cp * [A-Z] */tmp
Interesting, right ?! That is to say, "[] represents a character, and the definition of this character can be a range (-), a specified project, or both. For example, I want to find all the files containing numbers under/etc/, as shown in the following code:
Ls-lda/etc/* [0-9] *
But what if I only want to find files with names 3 and 5? It will be like this:
Ls-lda/etc/* [35] *
What if it is a certain range or word that you don't want? Use [!] You can! For example, if you do not want an archive that starts with a lower-case character:
Ls-lda/etc /[! A-Z] *
Great! What is that? In a string of commands, the commands in 'command' are executed first, and the executed messages are uploaded back to the external commands for processing! That is to say:
1. the system first executes uname-R to find the output result;
2. Add the result to the directory to execute the CD function!
Great !! In addition, you can use $ () to replace this quot (') function! For example:
CD/lib/modules/$ (uname-R)/kernel
These basic functions require special understanding.

Content Excerpted from: laruence private dish

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.