In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Bash script programming in arithmetic operations and file search how to use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Arithmetic operation
Compared to other programming languages, bash cannot be expressed directly in the form of variables plus variables; for example, we declare two variables, num1 and num2, and then do the operation.
Num1=2num2=3echo "$num1+$num2" 2x3
The result we show here is that 2x3 only replaces variables instead of adding two variables; in bash, variables are stored as strings by default; even if the two variables we declare here are integers, they will not operate; the declare command-I can declare that the variables we define are integers and can only do variable substitution.
Declare-I num3=9declare-I num4=1echo "$num3+$unm4" 911
For bash to do arithmetic operations, you must use a special arithmetic format:
1. Use the let command
For example
Let $num1+$num2
No data is displayed here. The let command does not output the result to the screen by default; so we need to do variable assignment here; assign the calculated value of $num1+$num2 to another variable, sum.
Let $sum=$num1+$unm2echo "$sum" 5
two。 The arithmetic expression is directly assigned to a variable with []; here it can be assigned or used directly.
Echo $[$num3+$num4] 10
It can also be assigned.
$sum=$ [$num3+$num4] echo "$sum" 10
3. Using two parentheses is similar to the use of []
$sum=$ (($num3+$num4)) echo "$sum"
4. Use the expr command to perform operations; pass variables and operators as parameters to expr for operations
$sum=$ (expr $num3 + $num4)
Note that spaces are required between the parameters that operate as the parameters of the command.
The operators of bash are +, -, /, * (to the power),% (modulo)
Multiplication symbols have a special meaning in bash, so escape characters need to be used in some scenarios
File lookup
The commands you can use to find eligible files on the file system are: locate,find
Locate
The file search command works on a pre-built index library. Systematically traverse the files under all the file paths of the file system to build this library, users do not go through the target path directly but search the index library directly when looking for files; to save system resources; but we consume a lot of resources when we update the index library manually
The characteristics of his work are: find and read quickly, fuzzy search, non-real-time search.
Manually update the index library updatedb
Locate: find eligible files on the file system locate [OPTION]... PATTERN... -b: to the base name in the matching path-c: count the total number of eligible files-r: basically use regular expressions
Find command
Real-time search tool to complete file search by traversing the file system hierarchy under the specified starting directory; unlike the previous locate, find belongs to real-time search and can specify the starting path of the search target, and the default is the current directory; the matching condition of find is precise search, which can be based on file name, size, type, dependency, permissions, etc. And you can delete files that meet the conditions, and the default is to output to standard output.
Job characteristics: search speed is slightly slow; accurate search; real-time search
Fnind:
Find: find [OPTIONS] [find start path] [find condition] [processing Action]
Search condition
Look up by file name:
-name "pattern"-iname "pattern": ignore case
Pattern supports glob-style wildcards
-regex "pattern": finds files based on regular expressions, matching the path, not the file name
Look up according to the file dependency:
-user USERNAME: find all files whose owner is a specified user;-group GROUPNAME: find all files whose owner is a specified group;-uid UID: find all files whose owner is a specified UID;-gid GID: find all files whose owner is a specified GID;-nouser: find files without an owner;-nogroup: find files without a group
Look for it according to the file type:
-type TYPE: F: normal file; d: directory file; l: symbolic link file; b: block device file; c: character device file; p: pipe file; s: socket file
Look up according to the size of the file:
-size [+ | -] # commonly used unit: KMagazine G
Find based on the timestamp:
In days:-atime [+ | -] #-mtime-ctime in minutes:-amin-mmin-cmin
Find based on permissions
-perm [/ | -] modemode: exact permission matching / mode: any one of the permissions of any type of user (urector gpeno) satisfies one of them; there exists or has a relationship between permission bits;-mode: every one of the permissions of each type of user (umeme gpeno) conforms to and conditions are satisfied at the same time; the existence and relationship between permission bits
Combined testing:
And:-a; default combinational logic; both conditions are met as and; or:-o; meet two conditions, one of which is or; not:! ;-not! Take reverse
Processing actions:
-print: output to standard output; default action;-ls: similar to executing the "ls-l" command on the found file; display the details of the file-delete: delete the found file;-fls / PSTH/TO/SOMEFILE: save the long format information found to the specified file;-ok COMAND {}\;: execute the command indicated by COMAND on each file found, and each operation is confirmed by the user. -exec COMAND {}\;: execute the command represented by CMOAND for each file found
Exercise:
1. Find out all the files under the / tmp directory that are mainly non-root.
Find / tmp/ -!-user root
2. Find the files in the / tmp directory that do not contain the fstab string in the file name.
Find /-name [^ fstab]
3. Find out the files whose main part of the / tmp directory is non-root and whose file name does not contain the fstab string.
Find / tmp -!-user root-a-name [^ fstab]
1. Find all files or directories under the / var directory whose main is root and whose group is mail.
Find / var/-user root-a-group mail
2. Find all files or directories in the / usr directory that do not belong to root, bin or hadoop; use two methods
Find / usr/-not-user root-not-user bin-not-user hadoopfind / usr/-not (- user root-o-user bin-o-user hadoop)
3. Find files or directories under the / etc directory whose contents have been modified in the last week and whose owner is neither a root user nor a hadoop user.
Find-atime-7-a-not (- user root-o-user hadoop)
4. Find files or directories that do not belong to or belong to groups on the current system and have been accessed in the last week
Find / (- nouser-o-nogroup)-a-atime-7
5. Find all files in the / etc directory that are larger than 1m and whose types are ordinary files
Find / etc/-size + 1m-a-type f
6. Find files in the / etc directory that all users do not have write permission to
Find / etc/-not-perm / 2222-type f
7. Find at least one class of files in the / etc directory for which users do not have the right to execute.
Find / etc-not-perm / 111-type f
8. Find all files under the / etc/init.d/ directory where all users have execute permission and other users have write permission
Does find / etc/init.d/-prem-113-type f help you after reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.