Arithmetic operations
Compared with other programming languages, it cannot be expressed directly in bash in the form of variables plus variables; for example, we first declare the two variables num1 and num2 and then perform operations.
num1=2 num2=3 echo "$num1+$num2" 2+3
The result we display here is directly 2+3. It is just a variable replacement, rather than adding two variables. In bash, variables are stored as string types by default; even if the two variables we declare here are integers, they will not perform operations; declare command-i
You can declare that the variable we define is an integer bash and it will only replace variables;
declare -i num3=9 declare -i num4=1 echo “$num3+$unm4” 9+1
For bash, to do arithmetic operations, you must use a special arithmetic operation format:
1. Use the let command;
For example
let $num1+$num2
No data is displayed here. The let command will not output the result to the screen by default; so we need to do variable assignment here; assign another variable sum to the value after the operation of $num1+$num2;
let $sum=$num1+$unm2 echo “$sum” 5
2. Arithmetic operation expressions are directly assigned to a variable using []; here you can assign values or directly
echo $[$num3+$num4] 10
Assignment can also be done;
$sum=$[$num3+$num4] echo "$sum" 10
3. Use two brackets and [] to use similar usage
$sum=$(($num3+$num4)) echo "$sum"
4. Use the expr command to perform operations; pass variables and operators as parameters to expr for operation;
$sum=$(expr $num3 + $num4)
Note that since the parameters that are used as parameters of the command need spaces;
The operators of bash include +, -,, /, * (power), % (module)
Multiplication symbols have special meanings in bash, so in some scenarios, escape symbols are required.
File search
The commands that can be used to find files that meet the criteria on the file system are: locate, find
locate
The working principle of the file search command is to rely on the pre-built index library. Systematically pass through all files under the file paths of the file system to build this library. When searching for files, users do not directly search for the target path but directly search for the index library; to save system resources; but when we manually update the index library, we will consume a lot of resources;
His working characteristics are: quick search, fuzzy search, non-real-time search;
Manually update index library updatedb
locate:Find files that meet the criteria on the file system locate [OPTION]... PATTERN... -b:to the base name in the matching path -c:Statistics how many documents meet the criteria -r:Basically use regular expressions
Find command
Real-time search tool, complete file search by traversing the file system hierarchy structure in the specified starting directory; unlike the previous location, find is a real-time search, and can specify the starting path of the search target, the default is the current directory; the matching conditions of find are accurate searches, and can be based on file name, size, type, subordinate relationship, permissions, etc.; and can also delete files that meet the conditions, and the default is output to standard output;
Working characteristics: slightly slow search speed; accurate search; real-time search
fnind:
find: find [OPTIONS] [Find the starting path] [Find criteria] [Processing actions]
Find the criteria;
Search by file name:
-name “pattern” -iname “pattern“ :Ignore case
pattern supports glob-style wildcard characters;
-regex “pattern”:Find files based on regular expressions,The path is matched,Not a file name;
Search by file affiliation:
-user USERNAME:Find all files whose owner is the specified user; -group GROUPNAME:Find all files that belong to the specified group; -uid UID:Find the owner as specifiedUIDAll files of; -gid GID:Find the group as specifiedGIDAll files of; -nouser:Find files without owners; -nogroup:Find files without group;
Find by file type:
-type TYPE: f:Normal files; d:Directory file; l:Symbol Link File; b:Block device file; c:Character device file; p:Pipeline files; s:Socket file;
Find by file size:
-size [+|-]# Common units:K,M,G
Find by timestamp:
In days: -atime [+|-]# -mtime -ctime In minutes: -amin -mmin -cmin
Search by permissions
-perm [/|-]mode mode:Precise permission matching /mode:Any type of user(u,g,o)Any one of the permissions(r,w,x)Just satisfy one of them;There are or relationships between permission bits; -mode:Every type of user(u,g,o)Every permission(r,w,x)Only when the conditions are met are met;The existence and relationship between permission bits;
Combination test:
and:-a;Default combination logic;两个条件都满足为and; or:-o;满足两个条件其中一只为or; No:!;-not!Reverse
Processing actions:
-print:Output to standard output;Default action; -ls:Similar to executing a found file“ls -l”Order;Show file details -delete:Delete the found file; -fls /PSTH/TO/SOMEFILE:Save the file length format information found in the specified file; -ok COMAND {} \; :Execute each file found byCOMAND表示的Order每次操作都由用户确认; -exec COMAND {} \; :Execute each file found byCMOAND代表的Order;
practise:
1. Find all files whose owner is not root in the /tmp directory;
find /tmp/ -! -user root
2. Find the file that does not contain the fstab string in the file name in the /tmp directory
find / -name [^fstab]
3. Find out the file whose owner is not root and whose file name does not contain the fstab string;
find /tmp -! -user root -a -name [^fstab]
1. Find all files or directories whose owner is root and whose group is mail;
find /var/ -user root -a -group mail
2. Find all files or directories that do not belong to root, bin or hadoop in the /usr directory; use two methods
find /usr/ -not -user root -not -user bin -not -user hadoop find /usr/ -not ( -user root -o -user bin -o -user hadoop )
3. Find the file or directory of the /etc directory that has been modified in the past week and is not the root user or hadoop user;
find -atime -7 -a -not ( -user root -o -user hadoop )
4. Find files or directories that have no genus or genus groups on the current system and have been accessed in the past week;
find / ( -nouser -o -nogroup ) -a -atime -7
5. Find all files larger than 1M and type of ordinary files in the /etc directory;
find /etc/ -size +1M -a -type f
6. Find files in the /etc directory where all users do not have permission to write;
find /etc/ -not -perm /222 -type f
7. Find at least one type of files that the /etc directory does not have execution permissions
find /etc -not -perm /111 -type f
8. Find all files in the /etc// directory, all users have execution permissions and other users have write permissions;
find /etc// -prem -113 -type f
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.