1. Review basic commands
2. Script
3. Variables
4. Alias
5. Conditional judgment
6. Test judgment
1. Review basic commands
shutdown --shutdown/restart
exit --exit the current shell
rmdir --delete empty directories
du --View the storage space occupied by the directory
df --View the space usage of the mounted file system
ln --Create a link
cat --Display all the contents of the file
head --Display the head of the file
tail --Display the tail of the file
less --Split screen/paging Display file content
dirname --Retrieve directory
basename --Retrieve file name
history --History command
1, ln
# ln -s [target] [source -- Create a soft link (symbolic link)
# ln [target] [Source] -- Create a hard link
2, cat
concatinate, concatenate the contents of the files in order and output to the standard output. At present, it only needs to be simply understood as displaying the content of the file.
# cat f1 -- display the content of file f1 on the screen
# cat -n f1 -- display the content of the file, And add the line number
# cat -A f1 -- print out some invisible characters and position markers
3, head
read The head of the file
# head -n 3 /etc/passwd --Read the first three lines of the file
# head -n -3 /etc/passwd --When reading a file, discard the last three lines of the file;
# head -c 3 /etc/passwd --Read the first three lines of the file bytes
# head -c 10m /dev/zero > f1 --Create a 10M file
4, tail
read Get the tail of the file
# tail -n 3 /etc/passwd -- read the last three lines of the file
# tail -c 3 /etc/passwd -- read the last three lines of the file Three bytes
# tail -n +28 /etc/passwd -- Read all lines starting from line 28 to the end of the file
# tail -f /etc/passwd -- Track changes in the content at the end of the file. Commonly used to observe changes in log files
5, history
# set -o history --Enable command history function, recorded by default In the file ~/.bash_history
# set +o history --turn off the command history function
# history --display the last n historical commands
Note : The following three variables are used to control the number of command histories and whether to record timestamps
# vim /etc/bashrc --Generally configured in this file, user-level configuration can be done in ~/. bashrc
HISTSIZE=1000000 --The maximum number of records to store
HISTFILESIZE=2000000 --The maximum size of the command history file
HISTTIMEFORMAT='[%Y-%m- %d %H:%M:%S] ' --Time stamp format of command history
export HISTSIZE HISTFILESIZE HISTTIMEFORMAT --Define them as environment variables
2. Script
1. How to execute the script # vim test.sh
#!/bin/bash
echo 'hello world'
# bash test.sh
# bash -x test.sh --Display information about the script execution process
# sh test.sh
# source test.sh
# . test.sh
--The above are both non-standard execution scripts, and the script file does not need executable permissions; the following is standard execution:
# head -n 1 test.sh
#!/bin /bash
# chmod a+x test.sh
# ./test.sh
You can directly place the script in the PATH path and use it directly as a command Run
Note: The shell must start with #!, pronounced as sha-bang. These two characters are used to identify the file type. #! is used to tell the system that this file The content inside will be interpreted by the specified interpreter, and the string after #! will be interpreted into a program, which is used to interpret the current file. If the sha-bang line is not provided, the current shell will guess one, but the results are not guaranteed to be consistent with expectations; so it is best to specify it explicitly. The running process of ./test.sh:
##1. The kernel reads the script, checks the file type mark, and obtains the interpretation path;
2 , The kernel runs the interpreter;
3. After the interpreter runs, open the script file and explain its execution
Many interpreters that come with Linux: #!/bin/ bash;#!/bin/sh;#!/usr/bin/perl;#!/bin/sed;#!/bin/awk
2. Four arithmetic operators
+ - * /
$(()) $[] expr let
# echo $(( 38%5)) --Take the remainder
# echo $[38/5] --Find the quotient
# echo $((10**2)) --The square of 10
2.1, expr
# expr 14 % 9 --Take the remainder
# expr 34 / 9 --Find the quotient
# expr 30 \* 3 --When using the multiplication sign, its specific meaning must be masked with a backslash
# expr index "sarasara" a --Capture the position where the first character string appears
# expr substr "this is a test" 3 5 -- Grab the string
# expr length "this is a test" -- Calculate the length of the string
2.2. let
let calculation tool is used to execute one or more expressions. There is no need to add $ to represent variables in variable calculation. If the expression contains spaces or other special characters, it must be quoted
# no=20
# let no++ --increase
# let no-- --decrement
# let no+=10 --increment 10
# let no=no+10 --Same as above
# let no-=15 --decrement by 15
# let a=5+4 --change There is no need to add the $ symbol
# echo $a
3. Shell configuration file
/etc/profile --global configuration file,
/etc/bashrc --Global configuration file, generally used to define environment changes
~/.bash_profile --User configuration file
~/.bashrc --User configuration File
~/.bash_logout -- user configuration file, the file will be read when the login shell exits
4, shell type
According to the shell The startup methods are different, and the shells can be roughly divided into the following types:
login shell - Log in through the text interface, such as su -; the order of reading the shell configuration file is as follows:
/etc/profile——~/.bash_profile——~/.bashrc——/etc/bashrc
interactive shell --The default shell in gnome-terminal; read The order of the shell configuration files is as follows:
~/.bashrc ——/etc/bashrc
non-interactive shell --shell used to run scripts
4.1. Set a variable v in ~/.bashrc, requiring the variable to only appear in the interactive shell
# vim ~/.bash_profile -- Identifies whether the shell Login shell
LOGINSHELL=1
# vim ~/.bashrc --The variable is only set when it is not a login shell
if [ "$LOGINSHELL" != 1 ]; then
v="interactive shell only"
fi
#5. Command priority
Alias> Function> Internal commands> External commands
6. Shell special characters
For the shell, Some characters have special meanings in addition to their own meaning. If they are to be included in a string If it contains special characters, the special meaning of the special characters must be removed through quoting. The following are some special characters:
~ --If not quoted, they will be replaced by the shell's home directory
& -- Put the program in the background for execution
$ --dollar symbol, which can be used for parameter expansion
${} --variable processing, which can be used for parameter expansion The content of the variable can be replaced, deleted, extracted, etc.
* --asterisk, wildcard, matches all characters
? --question mark , wildcard, matches any character
() --Command group
{} --Command group
"" --Double quotes, quotation marks; can remove the special meaning of most special characters, except $;`;!;\
'' --Single quotation mark, quotation mark; can remove the special meaning of all special characters, except itself
. --Equivalent to the source command; can be used to set hidden files; when used for directory names, it represents the current directory
/ --Slash, path separator, root Directory
\ --Backslash can be used to remove the special meaning of a single character, which is the so-called "escape". It can also be used to represent special characters, such as newline characters (\ n)
##`` --Backticks, commands are executed first, and have the same meaning as $(); if there is nesting, you cannot use the `` sign
$(()) and $[] --Operator
: --Empty command
; --Command splitting; do not consider the result of the previous command execution
;; --Terminator of case option
# - -Comments
&& --Logical AND; can split the command; but the result of the previous command needs to be considered
|| --Logical OR; commands can be divided; there is no need to consider the execution result of the previous command
----------- -------------------------------------------------- ---
# echo 'hello world;' "hello world;" hello\ world; --Use three kinds of quotes to match the special meaning of removing spaces
3. Variables
1. Local variables
Variables defined by the current user; valid for the current process, other processes or the current process The child process is invalid
# a=123
# echo $a
# unset a --cancel the changed definition
2. Environment variables
The current process is valid and can be called by child processes;
# env - View all environment variables of the current system
# set - View all environments of the current system Variables and temporary variables
# echo $PATH -- Display the contents of the PATH variable
# export hi=hello --Define environment variables; during work, I generally like to change the ready-to-use environment. Several common environment variables in ~/.bash_profile
:
PATH --Affected command search
PS1 --Command Prompt
TMOUT --timeout time, unit is S, when the time is up, the interactive shell will automatically exit; so it is best to set it as a read-only variable# declare -r TMOUT=60
HISTSIZE, HISTFILESIZE, HISTTIMEFORMAT --Command history
3. System variables
are also called built-in variables in bash; the shell itself has Some fixed variables
$# --The number of parameters followed by the script
$* --All parameters following the script (if included in double quotes, they will be output as a string)
$@ --All parameters after the script (included in double quotes or retain the boundaries of each parameter)
$? --The status returned after the execution of the previous command
$$ --The process number of the current process
$! --The last process number running in the background
!$ --The last command or parameter
!! --The history of the last command
$0 --Program name or process of the current program
$1~$n --Positional parameter variable
#!/bin/bash
echo "\$0 = $0 "
echo "\$# = $#"
echo "\$* = $*"
echo "\$1 = $2"
echo "\$7 = $7"
echo "\$11 = ${11}"
# chmod a+x test.sh
# ./test.sh a b c
4. Variable definition
a. By default, there are no requirements for variable types. You can assign any value to the variable, but there cannot be any spaces on either side of the equal sign
b. Variable names are case-sensitive
c. Variable names cannot start with numbers or special symbols
d. Save the result of the command execution to the variable
5. Call the variable
# a=$(hostname)
# A =123456789
# echo $A
# echo ${A:2:3}
6, array
array Definition: Use parentheses to define an array, and the elements in the parentheses are separated by spaces
# array[0]=var1
# array[1]=var2
# array[2]=var3
# array=(var1 var2 var3) --Equal to the above three lines, define the array
# echo ${array[0]} --Get the array The first value
# echo ${array[*]} --Get all the values of the array
# echo ${array[@]} --Same as above
7. Declare defines typed variables
-i --Treat variables as integers
-r --Define read-only variables
-x --Export variables to environment variables
-a --Treat variables as arrays
8, read interactively define variables
-p --Prompt message
-n --Number of characters
-t --Timeout
-s --No display
4. Alias
The alias of the command, as the name suggests, means that the command can be executed through another name. It is usually used to simplify command input, or to add some parameters to the command, or simply to add multiple access names to the command.
# alias cp mv rm --Check whether the three commands cp, mv, and rm are aliases
# unalias rm --Delete the alias rm
# alias cdyum= 'cd /data/yum' -- Simplify input
# alias rm='rm -i' -- Add parameters to the command
-- Usually the alias is in the configuration file ~/.bashrc
#1, verify the priority level of aliases, functions, internal commands, and external commands
# alias pwd='echo it is an alias' --Create A pwd alias
# function pwd() { echo "this is a function"; } --Create a pwd function
# pwd --Run pwd and find that the output is an alias
# unalias pwd --Delete the alias, then run pwd, and find that the output is the function content
# unset pwd --Delete the function, and then run pwd, the output is the current path
So: Alias>Function>Internal command>External command
5. Conditional judgment
# vim test
---------------------
if [condition];then
command.. .
fi
----------------------
if [condition];then
command...
else
command...
fi
---------------- ------
if [Condition 1];then
command1...
elif [Condition 2];then
command2. ..
else
command3...
fi
------------------ ----
if [Condition 1];then
command1...
if [Condition 2];then
command2...
fi
else
if [Condition 3];then
command3...
elif [Condition 4];then
command4...
else
command5...
fi
fi
6. Test judgment
test == [judge]
1. Judgment of whether the file exists
# vim test.sh
#!/bin/bash
if test -e $1;then --whether it exists;also-p;-c;-b;-L
if [ -f $1 ];then --Use [ judge ] to replace
if test -f $1;then --whether it exists and is an ordinary file
if test -d $1;then --whether it exists and is a directory
if test -S $1;then --whether it exists and it is a socket File
echo 'YES'
fi
# chmod a+x test.sh
# ./test.sh [path]
2. Judgment related to file permissions
-r;-w;-x --Whether there are read, write, and execute permissions
-u --Whether there is suid
-g --Whether there is sgid
-k --Whether there is t bit
-s --Whether it is a blank file, -s means non empty;! -s Empty file
3. String judgment
= --If equal, it is true. There must be spaces on both sides of the equal sign; it can also be: ==
!= --True if not equal
-z String--True if the length of the string is zero
-n String--The length of the string True if not zero
if test $num1 = $num2;then
##if [ $num1 = $num2 ];then
if [ -n $num1 ];then
4. Numerical judgment
-eq --If equal, it is true
-ne --True if not equal
-gt --True if greater than
-ge --True if greater than or equal to
-lt --True if less than
-le --True if less than or equal to
if test $[num1] -eq $[num2];then
if [ $[num1] -eq $[num2] ];then
5. Logical judgment
-a and && --logical and
-o and || --logical or
! --Non
Priority: with > or >Non
##if test -e $1 -o -e $2;then -- Determine whether a file exists
if [ -e
$1 -o -e $2 ];then -- Same as above
if test ! -e $1 -o ! -e $2;then --Judge that the two files do not exist at the same time
if [ ! -e
$1 -o ! -e $2 ];then --Same as above
The above is the detailed content of Share some basic knowledge of Linux operation and maintenance. For more information, please follow other related articles on the PHP Chinese website!