Bash shell scripting Getting Started (ii)
Use of the Read command
Role:
Use read to assign the input value to one or more shell variables:
-p Specifies the prompt to display
-T TIMEOUT
Read reads the values from the standard input, assigns a variable to each word, and all the remaining words are assigned to the last variable
Ex.read-p "Enter a filename:" FILE
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/00/wKioL1eyalqDO5JHAAApqCKyfgg334.png "title=" 10.png "alt=" Wkiol1eyalqdo5jhaaapqckyfgg334.png "/>
Use of If for conditional selection
If action: Select Execute (NOTE: If statement can be nested)
Single Branch
if judgment condition; then
Branch code with true condition
Fi
EX. Single Branch case
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/01/wKiom1eycfnCQ_PFAABVtXKHJUE735.png "title=" 11.png "alt=" Wkiom1eycfncq_pfaabvtxkhjue735.png "/>
Dual Branch
if judgment condition; Then
Branch code with true condition
Else
The condition is a false branch code
Fi
EX. Examples of double branching and nesting if statements
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/00/wKioL1eycqnCFYPNAAChcn-i2_Q623.png "title=" 12.png "alt=" Wkiol1eycqncfypnaachcn-i2_q623.png "/>
Multi-Branch
if judgment condition 1; Then
If-true
Elif judgment Condition 2; Then
If-ture
Elif judgment Condition 3; Then
If-ture
...
Else
All-false
Fi
EX. Multi-branch case
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/01/wKioL1eyfCyhURT4AABbTZ-5Kqo387.png "title=" 13.png "alt=" Wkiol1eyfcyhurt4aabbtz-5kqo387.png "/>
Summary: The use of if is judged on a condition-by-case basis, when the first encounter is a "true" condition, the branch is executed, and then the entire if statement is ended
Use of case for conditional selection
Case Statement Syntax format:
Case expression in
Mode 1)
Condition 1
;;
Mode 2)
Condition 2
;;
Mode N)
Condition N
;;
*)
Remaining conditions
;;
Esac
Each mode in the case can use the Glob matching pattern:
A|b:a or B
*: matches any character of any length
? : Matches any single character
[]: Range Matching
Use cases of ex.case
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/02/wKiom1eyfqjzhCKUAABKq6TQOXY172.png "title=" 14.png "alt=" Wkiom1eyfqjzhckuaabkq6tqoxy172.png "/>
Use of file lookups
File Lookup function: On the file system to find eligible files;
File Lookup Related commands: Locate, find
Non-real-time lookup (Database lookup): Locate
Real-time Find: Find
Use of the Locate command
1.locate Command Features:
(1) Querying system for pre-built file index database
/var/lib/mlocate/mlocate.db
(2) dependent on pre-built indexes
(3) The index is built automatically when the system is idle (periodic tasks), the administrator manually update the database (UPDATEDB)
(4) The index construction process needs to traverse the entire root filesystem, consuming system IO resources very
(5) Working characteristics:
Fast Search Speed
Fuzzy Lookup
Non-real-time lookup
Search for the full path of the file, not just the filename
may only search directories where the user has read and Execute permissions
2.locate Related commands:
Locate KEYWORD
(1) Options:
-I performs a case-sensitive search
N-n lists only the first n matching items
-R support for using regular expressions
(2) Fuzzy search
Locate "PNG"; Search for files containing jpg words
(3) Support regular expressions
Locate-r ' \.png$ ' uses Regex to search for files ending in. jpg
Ex. Use locate to search for files ending in. png, without distinguishing the case of PNG, and displaying only the first 10 rows of the search.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/02/wKioL1eygq3ReA3LAABwojYXXE0283.png "title=" 15.png "alt=" Wkiol1eygq3rea3laabwojyxxe0283.png "/>
EX. Because the content of the locate search depends on the contents of the/var/lib/mlocate/mlocate.db, it is not searchable if the content is not in the data block. Even sometimes some pieces of data can not be searched.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/02/wKiom1eyhBDyaFO7AABDpOS97P4203.png "title=" 16.png "alt=" Wkiom1eyhbdyafo7aabdpos97p4203.png "/>
Small summary: In the production environment, if you use the Locate command to search for the desired content, do not use the UpdateDB command to update the database, because there may be a lot of data to update, which will cause the system will generate a lot of Io, will cause serious system lag.
Use of the Find command
Features of the 1.find command:
(1) Real-time search tool to complete the file search by traversing the specified path; (normal users need to be aware of permissions issues)
(2) Search speed slightly slower
(3) Exact search
(4) Only directories with Read and execute permissions may be searched by the user
Syntax for the 2.find command:
Find [OPTION] ... [Find Path] [Search Criteria] [Handling Action]
Find path: Specify a specific target path; defaults to the current directory
Search criteria: Specifies the criteria for finding criteria, such as file name, size, type, permissions, etc., by default to find all files under the specified path
Handling actions: Perform actions on eligible files, output to screen by default
Search criteria for 3.find commands
(1) Search by file name and Inode:
-name "file name": Support for using Glob
*, ?, [], [^]
-iname "file name": Letter case insensitive
-inumn Search by Inode number
-samefilename files with the same inode number
-links n files with number of links n
-regex "pattern": matches the entire file path string with pattern, not just the file name
EX. Directly matches a file that starts with S and ends with. SH Under the directory
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/05/wKioL1eyuaHQaEX8AAAiQvB9mNg375.png "title=" 17.png "alt=" Wkiol1eyuahqaex8aaaiqvb9mng375.png "/>
EX. Matches the file under the directory starting with S, ending with d.sh, and ignoring case
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/06/wKiom1eyua-i7jYOAAAebbrQcuc359.png "title=" 18.png "alt=" Wkiom1eyua-i7jyoaaaebbrqcuc359.png "/>
EX. Match/testdir/shells file that ends with S, beginning with. sh
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/06/wKioL1eywlry5bMcAAAgygxDX0w480.png "title=" 19.png "alt=" Wkiol1eywlry5bmcaaagygxdx0w480.png "/>
(2) According to the genus, the genus Group looks for:
-user USERNAME: Find files that belong to the specified user (UID)
-group GRPNAME: Finding files that belong to a specified group (GID)
-uiduserid: Finds files that belong to the specified UID number
-gidgroupid: Finding files with a specified GID number for the genus Group
-nouser: Finding files that are not owned by the master
-nogroup: Finding files that are not owned by a group
EX. Find/testdir/shells/The following is the main W, the group is a WAN file
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/06/wKioL1eyxILC_QqoAAAyD3V5FQU450.png "title=" 20.png "alt=" Wkiol1eyxilc_qqoaaayd3v5fqu450.png "/>
(3) Search by file type:
-type Type:
F: Normal file
D: Catalog file
L: Symbolic Link file
S: Socket file
B: Block device files
C: Character device file
P: Pipeline File
EX. Find the directory file below/testdir/shells/
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/06/wKioL1eyxaKym5NSAAAzgPtwzPM713.png "title=" 21.png "alt=" Wkiol1eyxakym5nsaaazgptwzpm713.png "/>
(4) Combination conditions:
With:-A (by default it is already a, for example-user a-group A with-user a-a-group A is a meaning)
Or:-O
Non:-not,!
EX. Find files belonging to W or group W, note that when using-O, you need to add a-ls to the front and back. Otherwise, only the latter (that is, the results of the-ls of-group W) are displayed.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/06/wKioL1eyzBDB1NUXAAAlYm0DAy0693.png "title=" 22.png "alt=" Wkiol1eyzbdb1nuxaaalym0day0693.png "/>
(5) De Morgan Law:
(non-P) or (non-Q) = Non (P and q)
(Non-P) and (non-Q) = Non (p or Q)
EX. Find files that are not root in the master and the genus Group
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/06/wKioL1eyz5SCqj7jAABFfbWYvf0149.png "title=" 23.png "alt=" Wkiol1eyz5scqj7jaabffbwyvf0149.png "/>
(6) Based on the file size to find:
-size [+|-] #UNIT
Common units: K, M, G
#UNIT: (#-1, #)
such as: 6k (5k,6k]
-#UNIT: [0,#-1]
such as: -6k means [0,5k]
+ #UNIT: (#,∞)
Such as: +6k said (6k,∞)
EX. Find files larger than 1k in the/testdir/shells/directory
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/07/wKioL1ey0jfQzLLSAAAw0ttQwN4765.png "title=" 24.png "alt=" Wkiol1ey0jfqzllsaaaw0ttqwn4765.png "/>
EX. Find files larger than 1k in the/testdir/shells/directory, and exclude Bierendesh this directory
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/07/wKioL1ey18bA1vjXAAAjQUdBV_I929.png "title=" 25.png "alt=" Wkiol1ey18ba1vjxaaajqudbv_i929.png "/>
(7) Based on time stamp:
Take "Day" as the unit;
-atime[+|-]# Access Time
#: [#,#+1] unsigned: indicates shorter than (#+1) >x>= #天的时间段被访问过
+#: [#+1,∞] indicates that (#+1) has been accessed beyond the day
-#: [0,#] indicates that the # was visited in the day
-mtime modification Time
-ctime Change Time
In "Minutes" units:
-amin
-mmin
-cmin
EX. Find files that have been modified in the ~/bin directory for two days and follow the prompts to remove
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/07/wKioL1ey26zT6lvjAACmLnIrwJU097.png "title=" 26.png "alt=" Wkiol1ey26zt6lvjaacmlnirwju097.png "/>
EX. Find files modified within two days of the ~/bin directory and delete them directly
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/86/07/wKioL1ey3HLy9sLrAACTRibQ768009.png "title=" 27.png "alt=" Wkiol1ey3hly9slraactribq768009.png "/>
(8) Search by permissions:
-perm [/|-]mode
MODE: Exact permission match
/mode: The permission of any class (U,g,o) object can be a match, or a relationship, + from the CENTOS7 start elimination
-mode: Each class of objects must have the specified permission, and the relationship
0 indicates no concern
EX. Exact search for files with permission 644
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/09/wKiom1ey9myDW9R2AAC0RIMxSEU549.png "title=" 28.png "alt=" Wkiom1ey9mydw9r2aac0rimxseu549.png "/>
EX. Find files with at least Read and execute permissions for u,g,o groups
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/08/wKioL1ey9xLSolsEAAC36hO10O8817.png "title=" 29.png "alt=" Wkiol1ey9xlsolseaac36ho10o8817.png "/>
EX. Find U,g,o one of the files and directories with read and write permissions
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/09/wKiom1ey-K3i2noYAAB9HUKLzxk892.png "title=" 30.png "alt=" Wkiom1ey-k3i2noyaab9huklzxk892.png "/>
Handling actions for 4.find commands:
-print: The default processing action, display to the screen;
-ls: Similar to performing a "ls-l" command on a found file
-delete: Delete the found file;
-flsfile: Long format information for all files found is saved to the specified file
-ok COMMAND {} \; Executes commands specified by the command for each file found;
Users are interactively asked to confirm the command before each file executes
-exec COMMAND {} \; Executes commands specified by command on each file found
{}: Used to reference the found file name itself
Find passes the found file to the command specified later, and finds all eligible files once passed to the following command
Some commands cannot accept too many parameters, at which point the command execution may fail, and the following ways can circumvent this problem
Find | Xargscommand
EX. Processing Action Example
(1) Remove any file that complies with u= read, or g= any, or o=
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/08/wKioL1ey-8eAozckAACqurU6eVU562.png "title=" 31.png "alt=" Wkiol1ey-8eaozckaacquru6evu562.png "/>
EX. Archive the results of the search to a specified file
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/08/wKioL1ey_eGyHV-tAAC126-gAzk555.png "title=" 32.png "alt=" Wkiol1ey_egyhv-taac126-gazk555.png "/>
EX. Search for the specified file and execute the specified command on the qualifying file,-ok will ask, and-exec is executed directly
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/86/09/wKiom1ey_ovCc4R6AABEfKIEWlc490.png "title=" 33.png "alt=" Wkiom1ey_ovcc4r6aabefkiewlc490.png "/>
Small summary: The Find command is quite powerful, but various parameters are also many, need to find the relationship between time, as well as the permission/and the meaning of the symbol. And don't confuse De Morgan law.
Compression, decompression tools and archive tools
Use of the compress/uncompress command
compress [-DFVCVR] [-B maxbits] [file ...]
-D: Uncompressed, equivalent to uncompress
-C: Result output to standard output, do not delete original file
-V: Show details
Uncompress decompression
Zcat file. Z>file can view the compressed document and redirect to the specified file, which is the equivalent of ~ Unzip. to ~
ex.compress Example
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/09/wKiom1ezBKPiwk6LAABLRUSsBoU651.png "title=" 34.png "alt=" Wkiom1ezbkpiwk6laablrussbou651.png "/>
Use of the Gzip/gunzip command
Gzip[option] ... FILE ...
-D: Uncompressed, equivalent to Gunzip
-C: Output the compressed or decompressed results to standard output
-#:1-9, specifying the compression ratio, the larger the value, the greater the compression ratio
Zcat: View text file contents without explicit decompression
EX. Example
GZIP-C Messages >messages.gz
Gzip-c-D messages.gz > messages
Zcat messages.gz > messages
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/08/wKioL1ezBceCsb1cAABJgK56Xd0436.png "title=" 35.png "alt=" Wkiol1ezbcecsb1caabjgk56xd0436.png "/>
EX. A case where the original file was unzipped and maintained by redirection
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/09/wKiom1ezBp7wfIACAABUpmZyZ_8021.png "title=" 36.png "alt=" Wkiom1ezbp7wfiacaabupmzyz_8021.png "/>
Use of the Bzip2/bunzip2/bzcat command
bzip2 [OPTION] ... FILE ...
-k:keep, keep the original file
-D: Unzip
-#:1-9, compression ratio, default = 6
Bzcat: View text file contents without explicit decompression
EX. Example of compressing files with bzip2 and preserving original files
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/08/wKioL1ezB3eDJxKOAAA_BMEW7lA447.png "title=" 37.png "alt=" Wkiol1ezb3edjxkoaaa_bmew7la447.png "/>
Use of the Xz/unxz/xzcat command
Xz[option] ... FILE ...
-k:keep, keep the original file
-D: Unzip
-#:1-9, compression ratio, default = 6
Xzcat: View text file contents without explicit decompression
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/08/wKioL1ezCAHzqlkrAABEEOGrASU848.png "title=" 38.png "alt=" Wkiol1ezcahzqlkraabeeograsu848.png "/>
Use of the Zip/unzip command
Package Compressed Directory
Zip–r specifying compressed file compression directory
Unpacking and decompression
Unzip Sysconfig.zip
The biggest highlight of this command is the ability to compress the directory, while other commands do not support compressing the directory for the time being.
Example:
Cat/var/log/messages | Zip Message-
UNZIP-P Message > Message
EX. Compressing the directory, you need to be aware of the compressed file and directory location when you enter the command
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/09/wKioL1ezDJrgspIRAACHzkPCeZs010.png "title=" 39.png "alt=" Wkiol1ezdjrgspiraachzkpcezs010.png "/>
EX. Unzip the directory to the specified path
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/09/wKiom1ezFtXi3oDGAABBTYOD2y4601.png "title=" 40.png "alt=" Wkiom1ezftxi3odgaabbtyod2y4601.png "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/09/wKiom1ezFuTARoquAAAoTkFfl5s860.png "title=" 41.png "alt=" Wkiom1ezfutaroquaaaotkffl5s860.png "/>
Use of the tar command
tar [OPTION] ... Package, do not compress files. (This command needs to add the suffix name itself, otherwise it may not be recognized)
-CF Packaging files, does not compress
-V can see the process
- T preview the files inside the tar package
-xvf Unpack the tar package and unzip it to the current directory by default
- C Specify the decompression path
-ZCVF packaged and compressed into *.tar.gz
-Z GZ mode compression
-jxz mode compression
-j bz2 mode compression
(1) Create an archive
Tar-c-f/path/to/somefile.tar FILE ...
Tar-zcvf/path/to/somefile.tar FILE ...
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/1B/wKioL1e1IwWwVQxbAABDRwBB_PY204.png "title=" 8.png " alt= "Wkiol1e1iwwwvqxbaabdrwbb_py204.png"/>
(2) View the list of files in the archive file
Tar-t-f/path/to/somefile.tar
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/1B/wKioL1e1IZnD1pEXAABrWqGhXWk504.png "title=" 7.png " alt= "Wkiol1e1iznd1pexaabrwqghxwk504.png"/>
(3) Expand archive
Tar-x-f/path/to/somefile.tar (unzip to current directory)
Tar-x-f/path/to/somefile.tar-c/path/(unzip to the specified directory)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/1B/wKiom1e1IVTQDuTwAAA__C3QuXo980.png "title=" 6.png " alt= "Wkiom1e1ivtqdutwaaa__c3quxo980.png"/>
Use of the Cpio command
Copy from or to a file
The Cpio command is a redirected way of packaging a file for backup, restoring a recovery tool that can decompress files ending with ". Cpio" or ". Tar".
cpio[Options] > file name or device name
cpio[Options] < file name or device name
[option] Option
-o Package a file copy into a file or export the file to the device
-I unpack, unzip the package file or restore the backup on the device to the system
-T Preview, view the contents of the file or output to the file on the device
-V Displays the file name during the packaging process.
-D Unpack the directory, and automatically build the directory when Cpio restore
-C A newer storage method
To back up the ETC directory:
Find./etc-print |cpio-ov >etc.cpio
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/1B/wKioL1e1JQ2h-bPMAABWHzrjM4Q268.png "title=" 9.png " alt= "Wkiol1e1jq2h-bpmaabwhzrjm4q268.png"/>
Content Preview
cpio–tv< Etc.cpio
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/1B/wKioL1e1JVKCCoQPAABbPWxrhw0382.png "title=" 10.png "alt=" Wkiol1e1jvkccoqpaabbpwxrhw0382.png "/>
To unpack a file
Cpio–iv <etc.cpio
cpio–idv< Etc.cpio
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/1B/wKiom1e1JjOhpSSkAABShnzyn5M298.png "title=" 12.png "alt=" Wkiom1e1jjohpsskaabshnzyn5m298.png "/>
Summary: Compression and archiving software, with the most use of gzip and tar, as well as Cpio, will be described in more detail later.
This article is from the "~ Breeze ~" blog, please be sure to keep this source http://weifeng8.blog.51cto.com/1957995/1839840
8.15_linux bash Shell scripting Getting Started (ii) and use of file lookup and compression