Modify the Initdefault parameter in the/etc/inittab file can modify the default start graphical interface or text interface, 3 is the default to enter the text interface, 5 is the default to enter the graphical interface.
Init 6
Shutdown–r now reboot
Shutdown–r delay number of minutes delay how long to restart
Shutdown
Halt
Init 0
Shutdown–h now
#是root用户
$ is a regular user
Add the user and change the password
Useradd Liuwu
PASSWD Liuwu
Delete User
Userdel–r Liuwu
Switch users
Su
Usage: su root or Su-root
The difference between the two is that Su root does not reload the user variable that switches the user, and Su-root reloads the user's user variables
Restart, start, stop network commands
Service Network Restart/start/stop
VI command
M move cursor to center of page
L MOVE the cursor to the end of the page
H move the cursor to the beginning of the page
Ctrl+f back Page
Ctrl+b Page Forward
0 move the cursor to the beginning of the line
$ move cursor to end of line
/: From top to bottom find n next n prev
?: Find from bottom to top
U revert to previous action-undo
. Repeat previous action-repeat
Nyy the next n-line content where the cursor is copied
YW copy the words after the cursor, with spaces as the demarcation
P Paste the copied content at the cursor
NDD Delete the contents of the down N line where the cursor is located
CTRL + D: Flip down half-screen content
CTRL + U: Flip up half-screen content
Shift + G: to end of file
R: Replace the current position character
X: Delete the current position character
DD: Delete entire row
Set Number: Show line numbers
Set Nonu: Suppress line numbers
: 1,3y copy first line to third row
: 1,3d Delete First row to third row
: 1,3s/str/str_new/g replaces the string from the first row to the third row (all in one row)
: 1,3s/str/str_new replaces the first character of the string in the first line to the third line
: 1,3 g/str/d Delete the line containing the string in the first row to the third row
% all,:%d all deleted
:%s/str/str_new/g Replaces all
Enter the string/+ you want to find/bestest
Press the N key to re-locate the next
N Find Previous
Q Exit VI
Q! Do not save exit
Wq Save exit
E! Do not save the modified file
W Save
! Coercion, followed by an exclamation mark, indicates coercion.
Uname–a Viewing the system version
DF–LH show how big the hard drive is
Netstat-nlpt|grep 80 Check Port number is not occupied
|wc–l
Ps-ef|grep MySQL finds all processes, finds MySQL in the results, and displays a grep process if no MySQL
Ps-ef|grep Mysql–v Prep This ruled out grep.
- Head: Displays the beginning of the file to the standard output
Usage: head [parameter] ... [File] ...
Main parameters:
-n< rows > rows displayed
- Tail: Viewing the end of a file
Usage: tail [parameters] [file]
Main parameters:-F Dynamic Read
Dynamic View Log tail-f Err.log
View the end of 10 rows of logs tail-10 Err.log
Dynamically view the end of the 5-line log tail-5f Err.log
- WC: Counts the number of rows, bytes, and words in the specified file, and displays the results of the statistics output
Usage: WC [options] [file]
Main parameters:
-C Count bytes.
-L counts the number of rows.
-M counts the number of characters. This flag cannot be used with the-C flag.
-W count words. A word is defined as a string separated by a blank, a jump, or a newline character.
-L Prints the length of the longest line.
-HELP Display Help information
can be used without any parameters
Grep-c "foo" a.txt
Ps-ef|grep httpd |grep-v "grep"
Find/-name Besttest
awk Combat
- Line that matches the contents of the file occurrence
awk '/exception/{print NR} ' catalina.out
- Querying the Tomcat process number
Ps-ef|grep Tomcat |grep-v "grep" |awk ' {print $} '
- Output the Access IP address, access path, and status code in the Apache log
awk ' {print ' ip== "$", "path==" $7, "code==" $9} '/opt/lampp/logs/access_log
- The name of the student who failed the grade is output
awk ' {if ($2>60) {}else{print $} ' Soce.txt
- Write the MySQL process number to the Mysql.pid file.
Ps-ef|grep mysql|grep-v "grep" |awk ' {print $ > ' mysql.pid '} '
- Shell supports custom variables
Variable name does not add dollar sign ($) when defining a variable
Name= ' Besttest '
Note that there can be no spaces between the variable name and the equals sign, which may be different from all of the programming languages you are familiar with, along with the following rules:
The first character must be a letter (a-z,a-z).
You can use an underscore (_) without spaces in the middle.
Punctuation cannot be used.
You can't use the keywords in bash (you can see the reserved keywords using the help command).
Using variables, add a dollar sign ($) or ${name} before using a variable to
Echo ${name}
Variable |
Meaning |
$$ |
The current shell process ID, which is the PID of this script |
$ |
Current file name |
$n |
Take the arguments passed to the script, N is a number, n is a few arguments |
$# |
Number of parameters to take |
[Email protected] |
Take all parameters, enclosed by double quotation marks (""), slightly different from $* |
$* |
Take all parameters |
$? |
Exit status of the last command, 0 means no error, and any other value indicates an error |
+-*/% respectively for add, subtract, multiply, divide, withdraw
Just enclose a specific arithmetic expression in "$ ((" and ")"), for example:
a=$ ((4-2)) A has a value of 2
a=10
b=2
echo $ (($a + $b))
echo $ (($a-$b))
echo $ (($a * $b))
echo $ (($a/$b))
echo $ (($a% $b))
Operator |
Description |
Example |
-eq |
Detects whether two numbers are equal and returns true for equality. |
[$a-eq $b] |
-ne |
Detects whether two numbers are equal and returns true if they are not equal. |
[$a-ne $b] |
-gt |
Detects if the number on the left is greater than the right and, if so, returns True. |
[$a-gt $b] |
-lt |
Detects if the number on the left is less than the right and, if so, returns True. |
[$a-lt $b] |
-ge |
Detects if the number on the left is large equal to the right, and returns true if it is. |
[$a-ge $b] |
-le |
Detects if the left number is less than or equal to the right, and returns true if it is. |
[$a-le $b] |
Operator |
Description |
Example |
-D File |
Detects if the file is a directory, and returns True if it is. |
[-D $file] returns false. |
-F File |
Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. |
[-F $file] returns TRUE. |
-R File |
Detects if the file is readable and returns true if it is. |
[-R $file] returns TRUE. |
-W File |
Detects if the file is writable and returns true if it is. |
[-W $file] returns TRUE. |
-X File |
Detects if the file can be executed and, if so, returns True. |
[-X $file] returns TRUE. |
-S file |
Detects whether the file is empty (the file size is greater than 0) and does not return true for null. |
[-S $file] returns TRUE. |
-E File |
Detects whether the file (including the directory) exists and, if so, returns True. |
[-e $file] returns TRUE. |
Defining arrays
In the shell, the array is represented by parentheses, the elements of the array are separated by a "space" symbol, not separated by commas in other languages, only one-dimensional arrays are supported in the shell, and the subscript of an array can be discontinuous.
For example:
Array = (value1 value2 ...)
Array = (value
Value1
value2)
An array can also be defined by assigning a value to the subscript
List[0]=1
List[8]=9
How arrays are evaluated:
${array_name[index]}
${list[0]}
Use @ or * to get all the elements in the array, such as
${list[*]}
${list[@]}
The method of getting the length of the array is the same as getting the string length, using #, for example:
Len = ${#list [*]}
- The IF statement determines which branch to execute by using relational operators to determine whether an expression is true or false. The Shell has three kinds of if ... else statements:
- If ... fi statement;
- If ... else ... fi statement;
- If ... else ... elif. Fi statement.
- Syntax for the IF ... else statement:
Then Statement (s)
Else
Statement (s)
Fi
Syntax for the IF ... elif...else...fi statement:
if [expression]
Then Statement (s)
elif [Expression]
Then
Statement (s)
Else
Statement (s)
Fi
Use the If to write a simple little script
Determine if a script has execute permissions, if not, give it permission to execute, and if not, create a
#!/bin/bash
File= '/root/my.sh '
If [-X $file]
Then
Echo $file
Else
Touch $file
chmod +x $file
Fi
Use case Esac to write a simple little script
According to the input parameters to judge, input R words execute a script, enter C words to create a script, enter D then delete this script, enter the other to execute this script
#!/bin/bash
Num=$1
Case $num in
X
chmod +x my.sh
;;
D
RM-RF my.sh
;;
C
Touch my.sh
;;
*)
Bash my.sh
;;
Esac
Do a little exercise, use for and if, traverse all the files in the/root directory, print out the executable file
#!/bin/bash
Path= '/root '
For file in $path/*
Do
If [-X $file-A-f $file]
Then
Echo $file
Fi
Done
Do a little exercise, use the while to write a calculation of the cumulative sum of the script, enter a number, calculate its cumulative sum.
#!/bin/bash
Num=$1
I=0
Sum=0
While [$i-le $num]
Do
sum=$ (($sum + $i))
i=$ (($i + 1))
Done
echo "$sum"
Do a little exercise and write a function that shows a file with a directory ending with. sh
#!/bin/bash
function Showsh ()
{
For file in $1/*.sh
Do
Echo $file
Done
}
Showsh $
Find–name ' *.sh '
Linux Common commands