“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

In the last article we looked at some of Bash’s basic features, and in this article we’ll continue to look at other basic features!

Output redirection

In Bash, execution commands are printed to the screen by default, but most of the time we have the computer execute Shell scripts by way of timed tasks. To keep the log of the script execution, we need to write the output of some commands to the log file, which is output redirection.

type symbol role
Standard output redirection Command > file To write the correct output of a command to a specified file or device by overwriting
Command >> file To append the correct output of a command to a specified file or device
Standard error output redirection Error command 2> file To overwrite the error output of a command to a specified file or device
Error command 2>> file To append error output from a command to a specified file or device
Correct output and error output are saved simultaneously Command &> file To save the correct and incorrect output of a command to a file by overwriting
Command &>> file To append the correct and incorrect output of a command to a file
Command >> File 1 2>> File 2 Append correct output to file 1 and incorrect output to file 2
#Command output, but do not want to display, save in the trashCommand & > / dev/nullCopy the code

The sample

  • Correct output of redirection examples
#Writes the output of the date commandlogIn the file[root@VM-0-5-centos ~]# date > log [root@VM-0-5-centos ~]# cat log Sunday, 07 November 2021 20:20:27 CST
#Cover means
[root@VM-0-5-centos ~]# ls > log
[root@VM-0-5-centos ~]# cat log
hello_world.sh
log
test.txt

#Additional way[root@VM-0-5-centos ~]# date >> log [root@VM-0-5-centos ~]# cat log hello_world.sh log test.txt Sunday 07 November 2021 20:20:43 CSTCopy the code
  • Error output redirection example
[root@VM-0-5-centos ~]# dfasf 2> error_log
[root@VM-0-5-centos ~]# cat error_log
Copy the code
  • Correct error output redirection

In practice, we do not know whether the command was successfully executed in advance, so we cannot determine the correct or wrong output, so it is best practice to save the correct and wrong output at the same time.

#The correct output
[root@VM-0-5-centos ~]# date &>> all_log
#Error output[root@VM-0-5-centos ~]# abcd &>> all_log [root@VM-0-5-centos ~]# cat all_log Sun 07 Nov 2021 20:26:32 CST-bash: Abcd: command not foundCopy the code
  • Output to trash can

Some commands will have output when executing the script, but this output does not help us, so we can output these logs to /dev/null.

[root@VM-0-5-centos ~]# ls &> /dev/null
Copy the code

Input redirection

The input parameters of the command are keyboard-dependent, but we can change the input method to use files as input. Input redirection is rarely used, so be aware.

Wc [option] [filename] -c Number of bytes -w Number of words -l Number of linesCopy the code

Example:

[root@VM-0-5-centos ~]# cat test.txt
insert something
i love you
i love you
i miss you
[root@VM-0-5-centos ~]# wc < test.txt
 4 11 50
 
#4 lines, 11 words, 50 bytes
Copy the code

Multi-command execution

In scripting, we can execute multiple commands simultaneously. If multiple commands depend on each other, you can execute them in the following way.

Multiple command executor format role
; Command 1; Order 2 Multiple commands are executed sequentially without any logical relationship between them
&& Command 1&& Command 2 Logically, command 2 is executed only when command 1 is executed successfully. Command 2 is not executed when command 1 is executed incorrectly
|| Command 1 | | 2 command If command 1 is executed successfully, command 2 is not executed. If command 1 is not executed successfully, command 2 is executed
#1. Use";"There is no dependency on the front and back commands[root@VM-0-5-centos ~]# ls; Date all_log error_log file1 file2 hello_world.sh log test.txt Sunday, 07 Nov 2021 20:48:14 CST [root@VM-0-5-centos ~]# dfas; Date-bash: dfas: command not found Sun 07 Nov 2021 20:48:25 CST
#2. Use logic and[root@VM-0-5-centos ~]# ls && date all_log error_log file1 file2 hello_world.sh log test.txt Sunday 07 November 2021 20:49:14 CST#The former command failed and the latter command will not be executed[root@VM-0-5-centos ~]# dfad && date-bash: dfad: command not found
#3. Use logic or
#The first command is executed successfully, but the second command is not executed
[root@VM-0-5-centos ~]# ls || date
all_log  error_log  file1  file2  hello_world.sh  log  test.txt
#If the first command fails to be executed, the second command is executed[root @ VM - 0-5 - centos ~] # dsfa | | date - bash: dsfa: command was not found on November 7, 2021 20:50:12 CST on SundayCopy the code

Wildcards and other special characters

We can use wildcards in Bash.

The wildcard role
? Matches any character
* Matches 0 or any number of characters, that is, can match anything
[] Matches any character in parentheses. For example, [ABC] must match a character, a, b, or C
[-] Matches any character in parentheses. – indicates a range, and [a-z] indicates a lowercase letter
(^) Logical no: matches a character that is not in parentheses. [^0-9] matches a character that is not a number
[root@VM-0-5-centos ~]# touch abc
[root@VM-0-5-centos ~]# touch acd
[root@VM-0-5-centos ~]# touch ade

#? Matches any character
[root@VM-0-5-centos ~]# ls a??
abc  acd  ade

#[] Matches any character in parentheses
[root@VM-0-5-centos ~]# ls a[bc]?
abc  acd
Copy the code

Other special symbols in Bash

symbol role
‘ ‘ Single quotes. All characters in ‘ ‘have no special meaning and are treated as strings
“” Double quotation marks. None of the special symbols has any special meaning, except for “$”, “‘”, and “\”The value of a reference object,Reference commandandEscape characterThe special meaning of.
` ` The quotation marks. Represents calling system commands, which are executed first under Bash, but are recommendedThe $()
The $() Like backquotes, system commands are invoked
# In a shell script, represents a comment
$ The user invokes the value of a variable
\ Escape character, the special character after \ will lose its special meaning and become normal character.
# ' 'Is treated as a string
[root@VM-0-5-centos ~]# echo '$(date)'
$(date)

# ""$(date) in the content is treated as a system command[root@VM-0-5-centos ~]# echo "$(date)" Sunday, 07 November 2021 21:09:52 CST
#The contents of backquotes are treated as commands[root@VM-0-5-centos ~]# echo 'date' Sunday, November 07, 2021 21:10:52 CST
#$can be used to call variables
[root@VM-0-5-centos ~]# name=1
[root@VM-0-5-centos ~]# echo $name
1

#With \ escape, $becomes a normal character and no system command is invoked
[root@VM-0-5-centos ~]# echo  \$name
$name
Copy the code

conclusion

This article introduces some of the basic functions of Bash, and mastering these techniques can greatly improve our productivity.

  • I/O redirection
  • Multi-command execution
  • The wildcard

More and more

Personal blog: lifelmy.github. IO /

Wechat official account: Long Coding road