Source: (Linux Shell) Chapter One--Small trial sledgehammer (i.)
Starting today, let's study the book "Linux Shell Scripting Raiders".
1.1 Brief Introduction
A shell script is typically a text file that starts with #!, as seen in the following:
#!/bin/bash
There are two ways to run a shell script, such as the following:
SH script.sh or./script.sh
But running the script once, we need to give the script permission to run, otherwise the script will not run
chmod a+x script.sh
In the command line, assume that you need to run multiple commands at the same time to use
Cmd1;cmd2
1.2 Interrupt Printing
1.2.1 Practical Walkthrough
Echo is the basic command for interrupting printing.
By default, Echo adds a newline character after each call.
Just need to use text with double-cited, combined with the echo command to be able to print the text on the interrupt, similar to the text without the double-cited can also get the output of nursery rhymes:
The same task can also be completed using the single-quote:
These methods look like acacia, but each has some special uses and side effects, take a look at the following line of commands:
See the command line throwing an error that cannot be prefixed with an exclamation point. So, assuming you want to print an exclamation mark, do not put it in a double argument, or you can precede it with a special transfer character slash to move the exclamation point:
When using echo with a single-pin, bash does not evaluate variables (such as $var) in a single argument, but simply displays them as-is
Another command that can be printed is the same number of printf,printf used in the C language as the printf function. Such as:
See, the output printed by printf is not wrapped with a newline.
1.2.2 Supplemental Content :
Prints color output. Each color has a corresponding color code, for example: reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta =35, cyan = 36, white =37. To print color text, you can enter for example the following command:
1.3 Playing variables and environment variables
1.3.1 Practical Walkthrough
A variable can be assigned in the following way:
Var=value
var is the variable name, and value is the values assigned to the variable, assuming that value does not include any whitespace characters, then it does not need to use an argument for reference, and for reproduction, it must use either a single or double argument. Note that var = value differs from Var=value. Rewriting var=value to var = value uses common errors, but the former is an assignment operation, while the latter is an equality operation. You can print out the contents of a variable by adding a $ prefix to the variable name
Var= "Value"
echo $var or Echo ${var}, you can see that the first command is wrong, and the 23rd command integer prints out the contents of the variable:
We can also refer to the variable values in the two arguments of the printf or echo command:
The Export command is used for environment variable settings. After that, whatever program runs from the current shell script inherits this variable. We are able to export specific variables in a running application or shell script, as needed. By default, there are a number of standard environment variables that are available to the shell for use. Path is one of them.
$ echo $PATH
Usually the value of $path is defined in/etc/environment or/etc/profile or ~/.BASHRC. Suppose you need to include a new path in path to use:
Export path= $PATH:/home/user/bin
1.3.2 Supplemental Content:
gets the length of the string, which can be used in the following ways or :
identify the version number of the current shell :
echo $SHELL or Echo
change bash hint string
When we open a terminal or execute a shell, we will see a prompt string similar to [email protected]:/home/$. The hints and colors in the release numbers of the different gnu/linux are also slightly different. We can use the PS1 environment variable to customize the hint text. The default shell hint text is set on a line in the file ~/.BASHRC. You can use the following command to list the line that sets PS1:
Suppose you want to set a prompt string that can be entered:
Ps1= ">", for example, as seen, the prompt has become ">" angle brackets, but this change only to the current terminal function, assuming a new terminal or computer restart, it will revert to the change of the previous prompt. You must change the value in ~/.BASHRC if you want to set it to take effect.
1.4 Mathematical operations through the shell
1.4.1 Preparation Knowledge
In the bash shell environment, you can run major arithmetic operations with Let, (()), and []. The two tools, expr and BC, are also useful for advanced operations.
1.4.2 Practical Walkthrough
A value can be defined using a normal variable assignment method, which is then stored as a string. However, there are ways we can make it work like a number, as seen in the following:
Of course, you can also do the self-increment or decrement of variables:
Other arithmetic operation methods:
BC is an advanced tool for mathematical operations, and this precision calculator includes a number of options. We can use it to run floating-point arithmetic and apply some advanced functions:
Additional actions for setting BC parameters:
1.5 Topsy file descriptive descriptors and redirects
? 1.5.1 Preparation Knowledge
? We frequently use standard input (stdin), standard output (stdout), and standard error (STDERR) when scripting. Redirecting output to a file through content filtering is one of the fundamental tasks we do. When the command outputs text, the output text may be an error message, or it may be a normal (non-error) output message. By looking at the output text itself, we cannot distinguish between what is normal output text and which is the error text. It is just that we can solve the problem by describing the file descriptor and extracting the text that is associated with the particular descriptive descriptor.
? The file Description descriptor is an integer associated with an open file or data stream. The file description descriptor 0,1 and 2 are reserved by the system.
? ? 0-----------stdin (standard input)
? ? 1-----------stdout (standard output)
? ? 2-----------stderr (standard error)
? 1.5.2 Practical Walkthrough
? ? The output text can be redirected or saved to a file using the following methods:
? echo "This is a sample text 1" > Temp.txt
? ? This method stores the output text in the file temp.txt by intercepting the file, meaning that the contents of the temp.txt are first emptied before the output of the Echo command is written to the file.
? ? Let's look at the following example:
? echo "This is a sample text 2" >> temp.txt
? ? This method will append the text to the target file.
? ?> and >> are not the same. Although both operators can redirect text to a file, the former empties the file and then writes the content, which appends the content to the end of the existing file.
? ?
? Look at what the standard error is and how to redirect it. When the command outputs an error message, the stderr information is printed. Take a look at the following examples:
? ?
Here, + is an illegal parameter, so an error message will be returned. Note: When a command error occurs and is returned, it returns a non-0 exit state, and when the command is completed, it returns the number 0, which can be obtained from the special variable $? (Execute echo $ immediately after execution of the command), and the exit status can be printed
? ? Next, we do not let the error print information in the terminal, but into a file:
? ? Note that the command is LS + 2>out.txt. Error message output to OUT.txt
? ? We are also able to run standard output and standard error messages together into the same command:
? ? be able to see that the error message and the standard output are output to different files. You can also convert stderr to stdout by using commands such as the following, so that both stderr and STDOUT are redirected to the same file:
? ? cmd 2>&1 output.txt or cmd &> output.txt. Sometimes, some unnecessary information may be included in the output. Assuming you don't want the terminal to be flooded with stderr, you can redirect the output of stderr to/dev/null, making sure everything is clean. Suppose we have three files, each of which is a1,a2,a3. However, the normal user does not have "read-write-run" permissions on the file A1. Suppose you need to print the contents of all files with a file name starting with a, you can use the Cat command:
? Can see that the A1 file has no permissions, but because I am logged in with the root user, so still able to read the content, assuming that other users read A1, will error, then we can use the standard error output to capture the error output, and then output the contents of other files:
? ?
? 1.5.3 supplemental content? ? ?
? ? commands that read input from stdin can accept data in a variety of ways. In addition, we are able to use cat and pipe to develop our own descriptive descriptors of the file, such as:
? ? cat File | Cmd
? ? cmd1 | Cmd2
? ? redirect files to command:
? ? cmd < file
(Linux shell) Chapter One--Small trial sledgehammer (i.)