Linux Command Line and shell script Programming
This time, I read the Linux Command Line and shell script programming book for 3rd Times. In general, this book is quite good. It is suitable for beginners. I also learned about shell programming from this book.
However, because I am familiar with the Linux environment and have some shell programming skills, this time it will be faster and I will not write anything that I already know. Just record things that were not noticed before.
Chapter 2 Introduction to Linux Shell
Linux Kernel
GNU software:
Gnome Shell
Gnome Software
Linux release
(1) Core Release
(2) Specific release
Chapter 2 shell
Virtual console in Linux: CTRL + ALT + F1/F2/F3./F8
Analog terminal: xterm, gnome Terminal
1. Task Management command:
&: This command is most often used. It is used at the end of a command and can be executed in the background.
CTRL + Z: You can place a command in the foreground to the background and pause it.
Jobs: view how many commands are currently running in the background
FG: transfers the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use FG % jobnumber to call up the selected command, % jobnumber is the serial number (not PID) of the command being executed in the background found through the jobs command)
BG: Pause a command in the background to continue execution. If there are multiple commands in the background, use BG % jobnumber to call up the selected command, % jobnumber is the serial number (not PID) of the command being executed in the background found through the jobs command)
2. file and directory operations
(1). File List
Ls-L-h-a-s
(2). Create a file/directory
Touch-A access time
-M modify time
-T time stamp
Mkdir-P
(3). copy the file/directory
CP-R (recursive copy file)-v-P (Retain file attributes)
-R (recursive copy directory)
-L (create a hard link file)
-S (create a soft connection)
(4). Rename
MV-V
(5). delete files/Directories
Rm-r-v-F
Rmdir
(6) view the File Content
Cat-N (No)
VI, OD (Binary), more, less, tail, head
NL (add a row number to the output)
Stat (File statistics)
File (file type)
3. Process Management
(1) view Processes
PS-Aux
PS-l
PS-EFH
Top
(2) Process Management
Kill
(3) view Disks
Mount-o loop
Umount
DF-H-l
Du-c-h-S-
(4) data sorting
Sort-N (sort values)
-R (reverse)
(5) Search for Data
Grep-N (show matching row number)
-V (reverse search)
-C (count matching)
-E (specify multiple matches)
4. Environment Variables
(1). Create Global Environment Variables
Export
(2) Remove Global Environment Variables
Unset
(3). Default Environment Variables
Home, hostname, path, ifs (character list used to split fields), optarg, optind
(4). log on to Shell
When you log on to Linux, bash shell starts as the login shell. When you log on to Shel, you will find four different startup files to process the commands.
The order of processing files in bash shell is as follows:
/Etc/profile
~ /. Bash_profile
~ /. Bash_login
~ /. Profile
Execute/etc/profile first, and then check whether there is. bash_profile,. bash_login or. profile in the user's directory.
When executing a new shell, if it is an interactive shell, for example, directly executing bash, Bash will execute the/etc/bashrc file, and then check. bashrc in the user directory.
If it is not interactive (shell script), bash shell executes the content of the bash_env environment variable.
5. Shell mathematical computation
(1) Count = 1; Count = $ [$ count + 1]
(2) Let I = I + 1; (I = I + 1 ))
(3) Var = 1; Var = 'expr $ var + 1'
Chapter 2 permission management in Linux
Common commands:
Useradd is used to add a new user.
Userdel deletes the user, but only deletes the user information in/etc/passwd. It does not delete any files owned by the account in the system.
Passwd change account password
CHSH modify default shell
Groupadd Add User Group
Chmod permission Modification
Chown modify owner
Chgrp modify user group
Chapter 2 structured commands
(1) If-Else
Value Comparison:-EQ,-Ge,-GT,-Le,-lt,-ne
String comparison: = ,! =, <,>,-N,-z
File comparison:-E,-D,-F,-R,-W,-X,-S
Composite condition check: &, |
(2) case
(3) For, while,
(4) Break and continue
Chapter 4 process user input and output
(1) command line parameters
Location Parameter: $0 program name, $1 first parameter, $2 second parameter...
Basename, dirname
$ # Number of command line parameters
$ * All parameters act as an object
$ @ All parameters serve as multiple objects
(2) processing options
Use Case statements
Shift offset parameter
The getopt command accepts Command Options and parameter lists in any form and automatically converts these options and parameters to the appropriate format.
For example, getopt AB: CD-a-B test1-c-d Test2 test3
One option of the SET command is the double-break number. Replace the command line parameter variable with the value in the command line of the SET command.
For example, set -- 'getopt AB: CD-a-B test1-c-d Test2 test3'
The getopts command generates an output for all the options and parameters to be processed found in the command line, and processes the existing shell parameter variables in sequence.
The optarg and optind. optarg environment variables include the values required for the options. The optind value indicates the position in the parameter list when getopts stops processing.
(3) get user input
Echo-N (no line feed)
Read read input
-P prompt
-T timing
-N1 indicates that the READ command exits after only one character is accepted.
-S does not echo
(4) use temporary files
The/tmp directory in Linux handles files that do not need to be permanently saved. It automatically deletes any files in the/tmp directory at startup.
Any user account on the system has the right to read and write files in the/tmp directory.
Mktemp testing. xxxxxx create a local temporary file
-T force mktemp to create a file in/tmp
When mktemp creates a temporary file, it returns the complete path name to the environment variable.
-D. Create a temporary directory.
Input/Output redirection:
1. Redirection Error
Ls-Al badfile 2> test4 # redirects the error to the file test4, so that the program error is directly input to the file test4, and the output is not displayed on the screen.
2. Redirect errors and Data
If you need to redirect both errors and common data, you must use two redirection symbols. The corresponding file descriptor must be placed before the data to be redirected, and then point them to the corresponding output file to save the data
For example:
Ls-Al test test3 badfile 2> test6 1> test7
This is to redirect the error to the test6 file and redirect the standard output stdout to the test7 file.
In fact, you can also redirect stdeer and stdout to the same file using the &> symbol, such:
Ls-Al test Test2 test3 badfile &> test7
In this way, both the error and standard output are directed to the test7 file.
3. Temporary redirection
For example:
Echo "this is an error"> & 2 # redirect this sentence to a standard error
4. Permanent redirection
Exec 1> testout redirects the standard output to testout during Script Execution
Exec 2> testerror redirects standard errors to testerror during Script Execution
However, the problem is that after stdout and stderr are redirected, they cannot be redirected to the original location.
Exec 0 <testfile redirects testfile to standard input during Script Execution
5. output file descriptor. In shell, there are at most 9 open file descriptors, And the other 6 are 3 ~ 8
Exec 3> test13out
Echo "and this"> & 3
Redirect the number 3 to the test13out file, so that the sentence after the echo above enters the test13out file.
6. Restore the redirection file descriptor
Exec 3> & 1
Exec 1> test14out
Echo "dsafadsfasdf"
Exec 1> & 3
In this way, file descriptor 3 is redirected to the standard output, then the standard output is redirected to the file test14out, and then the file descriptor 1 is redirected to 3, so that 1 restores the original stdout. Here file descriptor 3 is equivalent to a temporary variable to save the stdout standard output.
The input file descriptor is as follows:
Exec 6 <& 0
Exec 0 <testfile
....
Exec 0 <& 6
7. Create a file descriptor for reading/writing
You can indeed open the same file descriptor for the input and output. You can use the same file descriptor to read data from a file and write data to the file.
Exec 3 <> testfile
All input and output are directed to the testfile file.
8. disable file descriptors
Disable the file descriptor and redirect it to the special symbol &-
For example, exec 3> &-
Chapter 2 script control
Mktemp testint. xxxxxx
-T option forces mktemp to create a temporary file in the temporary folder of the system
-D mktemp: create a temporary directory
The Tee command can send stdin data to both stdout and the specified file.
Create a function
The return command exits the function in a specific exit state. The return command can use a single integer to define the function exit status. It provides a simple way to set the function exit status through programming.
Note two common errors:
(1) Remember to extract the return value as soon as possible after the function is complete.
(2) Remember that the exit status ranges from 0 ~ 255
Chapter 1 use color in the script