Linux Learning Note 3.0

Source: Internet
Author: User
Tags aliases arithmetic define local stdin unpack uncompress line editor

Command
Service Network Restart Restart Network
Systemctl Network Restart
Systemctl Restart SSH
Name= "VALUE" variable assignment
$ (1.../@/*/#/0/?/$) position variable
Test EXPRESSION Testing Command
Export setting global variables
ReadOnly Name set read-only variable
ENV, printenv, export, declare-x view global variables
Shift left argument
Let arithmetic operations
Read receive input
Locate the pre-built file index database on the query system
Find completes a file lookup by traversing a specified path
Xargs splitting over large parameters
Compression/Decompression:
Compress/uncompress
Gzip/gunzip
Bzip2/bunzip2
Xz/unxz
Zip/unzip
Tar package file
Cpio compressing backups by redirecting packages
Sed line Editor


Shell Script Programming Basics

Programming Basics
Program: Instruction + data
Program Programming Style:
Program: command-centric, data-serving instruction
Object type: Data-centric, instruction serves data
Shell program: Provides programming capabilities to interpret execution
How the program is executed
computer running binary Instructions
Programming Languages:
Low Level: Compilation
Senior:
Compiling: High-level language--compiler (programmer)--Target code (java,c#)
Explanation: Advanced Language--interpreter (computer)--machine code (SHELL,PERL,PYTHON)
BASIC Programming Concepts
How to handle the programming logic:
Sequential execution, loop execution, select execution

Shell Programming:

Over the program, explaining the execution
The basic structure of the programming language:
A combination of various system commands
Data storage: variables, arrays
Expression: a+b
Statement: If

Shell script:

A text file that contains some commands or declarations and conforms to a certain format
Format requirements:
First line shebang mechanism
#! /bin/bash
#!/usr/bin/python
#!/usr/bin/perl
Language used by the declaration
Shell Scripting uses:
Automating common commands
Execute System Administration Commands
Create a simple application
Working with text or files


To create a shell script:

First step: Use a text editor to create a text file
The first line must include the shell declaration sequence: #!
Add a comment, starting with #
Step two: Run the script
Give execute permission to specify the absolute or relative path of the script on the command line
Run the interpreter directly and run the script as a parameter of the interpreter program
Scripting specification:
Script code Opening Conventions
1, the first line is generally called the language used
2, program name, avoid changing the file name is unable to find the correct file
3. Version number
4, the time after the change
5. Relevant information of the author
6, the role of the procedure, and matters needing attention
7, the last is a brief description of each version of the update
Script debugging
Detect syntax errors in scripts (only syntax errors are detected)
Bash-n
Debug execution
Bash-x


Variable

Variable: named memory space
Data storage Method:
Character
numeric value (integer, float type)
Variable type effect:
1. Data storage format
2, the operation of the participation
3, the data range indicated
Report:
Strongly typed variables:
The variable is not cast, he is always the data type, and the implicit type conversion is not allowed. General set
A literal variable is a type that must be specified, a join operation must conform to a type requirement, and a call to declare a variable produces an error
Miss. such as: java,c#
Weakly typed variables:
The runtime of the language implicitly does the data type conversion. No type is required and the default is character type; participating operations
Implicit type conversions are automatically performed at the time of calculation, and variables can be called directly without prior definition
。 such as: Bash (does not support floating point), PHP
Variable naming laws:
1, can not use the reserved words in the program (IF,FOR, etc.)
2. Use only numbers, letters, and underscores, and cannot start with a number
3. See the meaning of the name
4, the Uniform naming rule: Hump name method (ABCDFG big Hump; ABCDFG Small Hump)


Types of variables in bash

According to criteria such as the effective scope of the variable is divided into the following variable type:
Local variables:
The current shell process is in effect and is not valid for any other shell processes
Variable assignment: name= ' value '
You can use reference value:
(1) can be a direct string: name= "root"
(2) Variable reference: name= "$USER"
"": Strong reference, where the variable reference is replaced with the value of the variable
': A weak reference where the variable reference is not replaced with a variable value, while preserving the original string
(3) Command reference: Name= command name=$
Show all variables that have been defined: set
Attached: "" can preserve variable content format
Delete variable: unset name

Environment (GLOBAL) variables:
The effective scope is the current shell process and its child processes, and global variables can be passed down (parent-to-child), but not
Return
Variable declaration, assignment:
Export Name=valueexport
Local variables can be converted into environment variables
Declare-x Name=value
Variable reference: $name, ${name}
Show all environment variables:
Env
Printenv
Export
Declare-x
To delete a variable:
unset name
Bash built-in environment variables:
PATH, Shell, USER, UID, HOME, PWD, shlvl (view current SHELL level), LANG (
Languages), MAIL, HOSTNAME, Histsize, _ (last string of last command)
Report:

() the difference from {}
Common denominator: Performing a combination of multiple commands
Different points: () Open a disposable sub-shell, no impact on the current shell environment, for one-time tasks (in parentheses
shellpid= current SHELLPID, variable inherits current variable)
{} does not open a child shell and has an impact on the current environment, where more than 10 of the positional variables need to be represented by {10}
{} need to add space before and after, and commands need to follow;

Local variables:
The effective scope is a snippet of code in the current shell process, usually referred to as a function

Positional variables:
$1,$2,... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.
Read-only variables:
Can only be declared, cannot be modified and deleted
Declaring read-only variables:
ReadOnly name, declare-r name
To view read-only variables:
Readonly-p

Exit status
Process uses exit status to report success or failure
0 stands for Success, 1-255 for failure
$? variable saves the most recent command exit status
Exit Status Code
Bash Custom exit status code
Exit[n]: Custom exit status Code
Attention:
1, the script once encountered the exit command, the script will immediately terminate, the termination status code depends on the exit command behind
The number
2, if the script does not specify the exit status code, the entire script exit status code depends on the execution of the script
Status code for the last command
3, command error does not affect continue execution, syntax error termination execution


Shift

Function: Moves the input parameter overall to the left (default one), the latter replaces the previous one
Shift more for looping


Arithmetic operations

Arithmetic operations in BASH: Help-let
+,-, * /,% (modulo, remainder), ** (exponentiation)
To implement arithmetic operations:
1. Let-var= arithmetic expression
2. var=$[Arithmetic expression]
3, var=$ (arithmetic expression)
4, var=$ (expr arg1 arg2 arg3 ...) (ARG parameter)
5, Declare-i var= value
6. Echo ' Arithmetic expression ' |BC
Multiplication symbols need to be escaped in some scenarios, such as*
Bash has built-in random number generator: $RANDOM (0-32767)
Random numbers between echo$[$RANDOM%50]:0-49
Enhanced Assignment:
x+= (x=x+1),-=, * =,/=,%=
Let x+=3:x self-assigned value after adding 3
Self-increment, self-reduction:
Let Var+=1
Let var++
Let Var-=1
Let var--

Logical operations
0 false
1 true
And, or
0&0=0;0|0=0
0&1=0;0|1=1
1&0=0;1|0=1
1&1=0;1|1=1

Short circuit with, short circuit or
0&&0=0
0&&1=0
1&&0=0
1&&1=1
CMD1 && CMD2
If CMD1 is false, CMD2 does not execute; Conversely, CMD2 executes
0| | 0=0
0| | 1=1
1| | 0=1
1| | 1=1
cmd1 | | Cmd2
If Cmd1 is true, CMD2 does not execute; Conversely, CMD2 executes

Xor different or
0^0=0
0^1=1
1^0=1
1^1=0
The same is false, the different is true
Function: Take counter

Conditional execution Operators
Depending on the exit status, the command can be run conditionally
&&: represents conditional and then
|| : Represents a conditional or ELSE


Condition test

To determine whether a requirement is satisfied, it needs to be implemented by a test mechanism:
A dedicated test expression needs to be assisted by a test command to complete the test process
Evaluate Boolean declarations for use in conditional execution:
True, return 0
False, return 1
Test command:
Test EXPRESSION
[EXPRESSION] (for normal matches)
[[EXPRESSION]] (for use with extended regular matching)
Note: You must have a white space character before and after expression
Test command
Examples of long formats:
Test "$A" = = "$B" &&echo "Strings is equal"
Test "$A"-eq "$B" &&echo "integers is equal"
Examples of shorthand formats:
["$A" = = "$B"]&&echo "Strings is equal"
["$A"-eq "$B"]&&echo "integers is equal"
Bash's numerical test
-V (VAR) variable var is set
Numerical test:
-GT is greater than
-ge is greater than or equal to
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to
Bash's string test
String test:
= = is equal to
>ASCII code is greater than ASCII code
< is less than
! = is not equal to
=~ whether the left string can be matched by the pattern () on the right
Note: This expression is typically used in [[]], extended regular expressions
-Z "string": whether the string is empty, empty is true, not empty is false
-N "string": whether the string is not empty, not empty is true, empty is false
Note: The operands used for string comparisons should all use quotation marks

Bash file test
Existence test:
-a FILE: Same-E
-e File: Test for existence of files, existence is true, otherwise false
Presence and category testing:
-B File: exists and is a device file
-C file: exists and is a character device file
-D file: Exists and is a catalog file
-F file: exists and is a normal file
-H or-l file: Existing and Symbolic link files
-P file: exists and is a named pipe file
-S file: exists and is a socket file
File permission test:
-R FILE: exists and is readable
-W FILE: exists and is writable
-X FILE: exists and is executable
File Special Permissions Test:
-U FILE: Exists and has suid permissions
-G FILE: Exists and has Sgid permissions
-K FILE: Exists and has sticky permissions
File size test:
-S FILE: exists and is not empty
Whether the file is open:
-T FD:FD file descriptor is already open on a terminal
-N File: Whether the file has been modified since it was last read
-O File: Whether the current active user is a file owner
-G file: whether the current active user is a group of files
Binocular test:
File1-e file2:file1 is a hard link to FILE2
File1-nt File2:file1 is new to FILE2 (MITME)
File1-ot File2:file1 is older than FILE2

Combination Test conditions for bash
Way One:
CMD1&&CMD2 and
cmd1| | CMD2 or
! CMD non-
Way two:
EXPRESSION1 (expression)-a EXPRESSION2 and
Expression1-o EXPRESSION2 or
! EXPRESSION Non-
Note: You must use the test command to


Use the read command to receive input

Assign the input value to one or more shell variables with the Read command
-P Specify the prompt to display, enter No line break
-s silent input, commonly used for passwords
-n Specifies the character length of the input
-d Specifies the input terminator
-T Specify timeout time
Read reads the values from the standard input, assigns a variable to each word, and all the remaining words are assigned to
Last variable
Read Batch assignment:
Read x y z <file
Read x y z <<< "a b C"


Prevent scaling

Reverse Slash:
Causes subsequent characters to be interpreted as intended
Add quotation marks to prevent expansion:
Single quotes Prevent all extensions
Double quotation marks also prevent all extensions, except for the following cases:
$-variable Extension
Anti-quote-command extension
-Prohibit single character expansion
! -Historical Command replacement


Bash's configuration file

Security scope of entry into force:
Global configuration:
/etc/profile
/etc/profile.d/ * . Sh
/etc/bashrc
(Profile: Configuration file, environment variables and startup program)
(Bashrc:bash Run command, Bash's runtime configuration file, put aliases and functions)
Personal configuration:
~/.bash_profile
~/.bashrc
Two ways to Shell login
Interactive login:
1, enter the account password directly through the terminal login
2. Use "Su-username" to switch users
Execution order:/etc/profile-->/etc/profile.d/. sh-->~/.bash_profile-->~/BASHRC-->/ETC/BASHRC
Non-interactive logon:
1, Su UserName
2. Open terminal under graphical interface
3. Execute script
4. Any other bash instance
Order of execution: ~/bashrc-->/etc/bashrc-->/etc/profile.d/"
. Sh

Breakdown by Function:
Profile class:
Provides configuration for the shell with interactive logon
Global:/etc/profile,/etc/profile.d/ * . Sh
Personal: ~/bash_profile
Function: (1) User defined environment variable (2) Run command or script
BASHRC class:
Global:/ETC/BASHRC
Personal: ~/.BASHRC
Function: (1) Define command aliases and functions (2) Define local variables

Editing the configuration file takes effect
After you modify the profile and BASHRC file, it takes effect
Two methods:
1. Restart the shell process
2,. or source

Bash quits task
Save in ~/.bash_logout file (user)
Run when you exit the login shell
For:
1. Create an automatic backup
2. Clear Temporary files

$-variable (represents a combination of some functions)
H:hashall, when this option is turned on, the shell hashes the path of the command, avoiding each
To query. With set +h, the H option is turned off
I:interactive-comments, including this option, shows that the current shell is an interactive
Shell The so-called interactive shell, in the script, I option is off
M:monitor, turn on monitoring mode, you can control the process stop, continue
, backstage or front desk execution, etc.
B:braceexpand, curly brace extension
The H;history,h option opens, you can expand the commands in the History list, you can go through! To complete the use
, such as: "!!" Returns the most recent history command, "!n" returns the nth historical command

Report:
1,. (source) and the difference between running scripts directly
The source run script is run in the current shell, changing the current shell's variables
Running the script directly is running in the shell subprocess, changing the child process variables, having no effect on the current shell
2, the script does not recognize the alias, the alias does not take effect

How bash expands the command line (execution order)
1. Divide the command line into a single command word
2. Expand aliases
3. Expand {} content
4, expand ~ Symbol
5. Replace $ () and "" with commands
6. Then divide the command line into command words
7. Expand File Wildcard characters
8. Handling redirects
9. Run the command


File Lookup

Find eligible files on the file system
Command:
Non-real-time lookup (Database lookup): Locate
Real-time Find: Find
Locate search is fast but cannot be updated in real time
Find is powerful, but search is slow and affects server performance

Locate
Querying the pre-built file index database on the system
/var/lib/mlocate/molcate.db
Rely on pre-built indexes
The index is built automatically when the system is idle (recurring tasks), and the administrator manually updates the data
Library (UPDATADB)
The index build process needs to traverse the entire root file system, consuming resources very
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
Locate command
Locate KEYWORD
Some options:
-I case-insensitive search
N-N only the first n matching options are listed
-R using regular expressions

Find
Real-time Find tool to complete file lookups by traversing a specified path
Working characteristics:
Slow search speed
Exact search
Real-time Search
may only search directories where the user has read and Execute permissions
Find command:
Find [OPTION] ... [Find Path] [Search Criteria] [Handling Action]
Find path: Specify a specific target path (default = current directory)
Search criteria: The specified lookup target, can be file name, size, type, permissions and other criteria (default
To find all files under the specified path)
Handling actions: Perform actions on eligible files (default output to screen)

Find criteria
Refers to the search hierarchy:
-maxdepth level maximum Search directory depth, the specified directory is the first
-mindepth level minimum Search directory depth
Search by file name and Inode:
-name "file name": Supports the use of Glob (wildcard: * ,? , [],[^])
-iname "file name": Letter case insensitive
-inum N: Search by inode Number (inode node number)
-samefile Name: file with the same inode number
-links N: File with a number of links n
-regex "pattern": matches the entire file path string with pattern, not just the file name

According to the genus, the genus Group looks for:
-user USERNAME: Find files that belong to the specified user
-group grpnsme: Finding files belonging to groups of specified groups
-uid UserID: Find a file with the specified UID number as owner
-gid GroupID: 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

Find 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
-empty: Empty file or directory
-prune: Exclude (prune: cut off, remove)

Combination conditions:
With:-A
Or:-O
Non:-not;!
De Morgan's Law:
(not a) or (not B) = Non (A and B)
(Non-a) and (not B) = Non (A or B)
Example:
! A-a! B =! (A-o B)
! A-o! B =! (A-a B)

Depending on the file size, look for:
-size [+|-] #UNIT
Common units: K,m,g,c (Byte)
#UNIT: (#-1,#]
-#UNIT: [0,#-1]
+ #UNIT: (#, Infinity)

Based on time stamp:
In "Days" as the unit:
-atime [+|-]#
#:[#,#+1)
+#:[#+1, Infinity]
-#:[0,#)
-mtime
-ctime
In "Minutes" units:
-amin
-mmin
-cmin

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,/table
Display and set (old version +)
-mode: Each class of objects must have the specified permission, and the relationship
0 indicates no concern

Handling actions
-print: Default processing action, display to screen
-ls: Similar to performing "Ls-l" on a found file
-delete: Delete the found file
-fls file: Long format information for all files found is saved to the specified files
-ok command{}\;: executes commands specified by command for each file found, for each
Before the file executes the command, the user will be asked to confirm interactively
-exec command{}\;: Executes command-specific commands for each file found
{}: Used to reference the found file name itself
OK and EXEC options must be followed by \;
Find passes the found file to the command specified later, finds all eligible files once
Pass to the following command


Parameter Substitution Xargs

Xargs is used to generate parameters for a command, Xargs can read into stdin data, and
The carriage return separates the stdin data into arguments
Xargs-n Resolve parameter too long cannot execute problem
The find and Xargs combinations use the format:
Find|xargs COMMAND


Compression, decompression, and archiving tools

Compress/uncompress:z
-D: Uncompressed, equivalent to uncompress
-C: Result output to standard output, do not delete original file
-V: Show details

Gzip/gunzip:gz
-D: Uncompressed, equivalent to Gunzip
-C
-#:1-9, specifying the compression ratio, the larger the value, the greater the compression ratio (default is 6)
Zcat: View text content under the premise of No pressure

Bzip2/bunzip2
-K: Keep the original file
-D
-# (default = 9)
Bzcat

Xz/unxz
-K
-D
-# (default = 6)
Xzcat

Zip/unzip
Packaging compression
Zip-r/testdir/sysconfig/etc/sysconfig/
Report:
1, linux file name suffix is not strict, but the compression tool on the file suffix strict requirements
2, XZ newness, not commonly used, or gzip and bzip


Tar Tools

Tar (Tape ARchive, abbreviation for tape archive)
(1) Create an archive
Tar-cpvf/path/to/somefile.tar FILE ...
(2) Append files to archive (does not support appending to compressed files)
Tar-r-f/path/to/somefile.tar FILE ...
(3) View the list of files in the archive file
Tar-t-f/path/to/somefile.tar
(4) Expand archive
Tar-x-f/path/to/somefile.tar
Tar-x-f/path/to/somefile.tar-c/path
(5) Combined with compression tool implementation: Archive and Compress
-j:bzip2,-z:gzip,-j:xz

Tar package file
-T: Specify input file
-x: Specifies a list of files to exclude
Split (tar file to multiple small files):
Split-b size-d Tar-file-name Prefix-name
Merge:
Cat


Cpio

Function: Copy file from or to archive
The Cpio command is a redirected way to package a file for backup, restore the recovery tool it can decompress
Files ending with ". Cpio" or ". Tar"
cpio[option]> file name or device name
cpio[option]< file name or device name
-O: Package A copy of the file 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 the contents of the file to the device
-V: Displays the name of the file during the packaging process
-D: Unpack the build directory, and automatically build the directory when Cpio restore
-C: A newer storage method


Text Processing Three Musketeers

Sed, line editor, one-time processing of a single line of content, with loops
When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then read the following line and execute the next loop
Function:
Mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program, etc.
Usage:
sed [option] ... ' Script ' inputfile ...

Linux Learning Note 3.0

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.