Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

If, case statements and file lookup commands for conditional judgment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. script programming

1. How to use the if statement

There are many choices in life, and there are also many choices in the programming world. As with other programming languages, when we want to write a robust script, it is particularly important to choose the appropriate action through conditional judgment. Before we perform some important operations, it is important to determine whether the current environment is suitable for doing so. We can use & & and | | to make simple judgments, but shell has more useful statements. Shell has two common conditional selection statements, if and case. Let's first take a look at how to use if.

The single branch syntax of the if statement:

If condition judgment; then; execution command; fi

[root@localhost ~] # type ifif is a shell keyword [root@localhost ~] # type fifi is a shell keyword [root@localhost ~] # type thenthen is a shell keyword

First of all, you can see that if, fi and then are all keywords in shell.

Double branches:

If condition judgment; then execution code else execution code fi multi-branch: if condition judgment; then execution code elif condition judgment; then execution code elif condition judgment; then execution code. Else execution code fi

The if statement judges one by one, executes the branch command under the condition when the condition is true, and then ends the whole if statement.

Of course, the if statement itself is very simple, but the use of if statements must be proficient in conditional test statements, and when if statements are used in nesting with other statements, you must sort out the logical relationship, otherwise it will be difficult to troubleshoot.

2. The case statement of shell

When the condition we want to judge is a continuous logical relationship, we can easily use if to operate, but if it is a discrete relationship, the if statement will appear bloated and tedious, and the use of case statement will be very efficient. In Linux, especially when writing some service startup scripts, you will encounter a lot of case statements. Mastering case statements is to do operation and maintenance, or at least a basic knowledge of script development.

Unlike if statements, the logical relationship of case statements is clearer.

The case variable refers to inpat1) Branch 1;; pat2) Branch 2;. *) the last branch;; esac

Case supports wildcards, such as *,?, [], |, etc.

Note that you should write esac at the end, inverting case and if is the same, if you don't write it, you will get an error.

II. File search

1. Loacte looks up the file by name

Locate uses the updatedb library to find files, using pattern with wildcards by default, and by default matching full paths including pathnames if wildcards are not used. If the file you just created is not synchronized to the database, the locate command cannot find it.

The default lookup library of locate is / var/lib/mlocate/mlocate.db, and the system sets the default plan to perform database synchronization tasks, which will consume a lot of system resources when the whole file system is complex and changes greatly.

Characteristics of locate:

Find speed block

Fuzzy search

Non-real-time search

Search only directories where the user has read and execute permissions

The whole locate work is actually made up of four parts:

/ usr/bin/updatedb is mainly used to update the database, which is done automatically through crontab.

/ usr/bin/locate (mlocate) is a program used to complete the query function.

/ etc/updatedb.conff is to configure which directories and files to query.

/ var/lib/mlocate/mlocate.db A file that stores file information.

Options for the locate command:

-A:--all is followed by multiple patterns. Only files that match all matching patterns are printed.

-b:--basename only matches files with base names that match pattern, which is the opposite of-- wholename.

-c:--count displays only the number of matched files, not the matched files

-d:--database replaces the default with the specified search database.-indicates that the library is read from standard input.

-e:--existing displays only files that already exist when locate is running

-L:--follow follows the spread of symbolic links when the file exists (default)

-i:--ignore-case ignores case differences when matching patterns

-l:--limit-n LIMIT is limited to the output (or count) of LIMIT projects

-m:--mmap ignores backward compatibility

-P:--nofollow,-H does not follow the spread of symbolic links when checking files

-0:--null output separates items with NUL

-S:--statistics does not search for items and displays statistics about each used database

-q:--quiet does not report error messages about reading the database

-r:--regexp REGEXP searches the basic regular expression REGEXP to replace the glob pattern

The regex pattern is an extended regular expression

-s:--stdio ignores backward compatibility

-w:--wholename matches the full pathname (default)

Second, find real-time lookup tool to complete file search by traversing the specified path

Find features:

The search speed is slow.

Precise search

Real-time search

Only find directories where the user has the appropriate permissions

Syntax: find [options] [find start path] [find condition] [process Action]

Find criteria: options + test conditions

Conditional test: the result is usually Boolean

Combined testing:

And:-an or:-o not:-no or!

De Morgan's law:

! (- pat1-o-pat2) =!-pat1-a!-pat2

(pat1-a pat2) =! Pat1-o! Pat2

Judging by the file name

-name pattern, wildcard matching, exact matching, pattern with quotation marks

-iname is not case sensitive

-inum finds all files of a certain inode number in all partitions

-samefile looks for files with the same inode

[root@localhost ~] # find /-samefile test.shfind:'/ proc/3732/task/3732/fd/7': No such file or directoryfind:'/ proc/3732/task/3732/fdinfo/7': No such file or directoryfind:'/ proc/3732/fd/7': No such file or directoryfind:'/ proc/3732/fdinfo/7': No such file or directory/root/test.sh/opt/t

Note: find only looks for disk files, but cannot find memory files in / proc

-links n Files or directories with n links

-regex matches the entire path rather than the base name, so the regular pattern includes the complete directory path. Find uses emacs regularities by default. If you want to change other regularities, you can specify them with the-regextype option.

According to the affiliation of the document

-user username finds all files for a user

-group groupname finds all the files in a group

-uid searches according to uid

-gid searches according to gid

-nouser looks for files without owners

-nogroup looks for files that do not belong to a group

Note: Linux uses id to identify file dependencies

Find by file type

-type is followed by the type of file. Note that files and directories include symbolic links, so it is best to write symbolic links in front of them when judging.

Find based on file size:

-size [+ | -] # UNIT commonly used units: K, M, G defaults to bytes

Note: only bytes can be used for accurate supersearch. If you look for it in other units, the number between two adjacent integers will be regarded as the large one.

# UNIT: (#-1 UNIT #]

-# UNIT: [0memorie muri 1]

+ # UNIT: (#)

Look up according to time

There are two units according to time, and their calculation methods are not the same.

1. Take days as the unit

-atime [+ | -] #

-mtime

-ctime

2. In minutes

-amin [+ | -] #

-mmin

-cmin

When # is 1, the areas of mmin 1 and mmin-1 will overlap

Find based on permissions:

-perm: [/ | -] mode number

Mode exact match permissions

/ mode any privilege of any type of user can be satisfied.

-mode all users must comply with each permission

Note: / 000 is the same as-000. 0 means you don't care about permissions.

Processing action

-print: output to stdout, printing by default

-ls: displays the long format information of the file

-delete deletes the found file

-fls / PATH saves the found file to the specified file

-ok command {}\; execute the command command for the file you are looking for. {} represents the file found, but there is a prompt for each file you operate on.

-exec command {}\; there is no prompt, where\; represents the end

Note:-ok and-exec process all the files found by find, that is, execute a command for each file. We know that a command is a process. When there are many files found, it will consume more resources and lead to a decline in efficiency. In order to avoid this problem, we can use xargs

Find search criteria | xargs command

Xargs passes all the results found by find to command as a parameter, that is, command is executed only once, which is more efficient. However, due to the pattern that separates all the results with a space and then becomes a variable, it will make an error when there is a space in the found file name.

Exclude directories:

-path PATH-prune combination exclusion, and the relationship

[root@localhost ~] # find / etc-path'/ etc/sane.d'-prune-o-name "* .conf" / etc/resolv.conf/etc/fonts/conf.d/65-0-lohit-marathi.conf/etc/fonts/conf.d/59-liberation-mono.conf/etc/fonts/conf.d/67-lohit-malayalam.conf/etc/fonts/conf.d/59-liberation-sans.conf/etc/fonts/conf.d/65-0-lohit-punjabi .conf / etc/fonts/conf.d/10-scale-bitmap-fonts.conf/etc/fonts/conf.d/60-open-sans.conf/etc/fonts/conf.d/20-unhint-small-vera.conf/etc/fonts/conf.d/65-0-madan.conf/etc/fonts/conf.d/25-no-bitmap-fedora.conf/etc/fonts/conf.d/66-ucs-miscfixed.conf.

Practice

1. Find all the files under the / var directory whose master is root and whose group is mail.

[root@localhost ~] # find / var/-user root-group mail-ls201327104 0 drwxrwxr-x 2 root mail 50 Aug 14 10:37 / var/spool/mail206330921 8-rw- 1 root mail 4219 Aug 14 10:37 / var/spool/mail/root

2. Find all the files in the / var directory that do not belong to root, lp or gdm

[root@localhost ~] # find / var/!\ (- user root-o-user lp-o-user gdm\)-ls135969642 0 drwxr-xr-x 3 colord colord 50 Aug 2 21:22 / var/lib/colord203103112 0 drwxr-xr-x 2 colord colord 6 Nov 20 2015 / var/lib/colord/icc139609985 4-rw-r--r-- 1 colord colord 4096 Aug 2 21:22 / var/lib / colord/mapping.db139609986 8-rw-r--r-- 1 colord colord 7168 Aug 2 21:22 / var/lib/colord/storage.db68217670 0 drwx- 2 tss tss 6 Nov 21 2015 / var/lib/tpm135970004 0 drwxr-xr-x 2 unbound unbound 21 Aug 3 06:10 / var/lib/unbound135970006 4-rw-r--r-- 1 unbound unbound 409 Nov 20 / var/lib/unbound/root.key204613039 0 drwxr-x--x 7 qemu qemu 69 Aug 2 21:18 / var/lib/libvirt/qemu 10634 0 drwxr-xr-x 2 qemu qemu 6 Aug 2 21:18 / var/lib/libvirt/qemu/save70295736 0 drwxr-xr-x 2 qemu qemu 6 Aug 2 21:18 / var/lib/libvirt/qemu/snapshot139609974 0 drwxr-xr-x 2 qemu qemu 6 Aug 2 21:18 / var/lib/libvirt/qemu/dump206290799 0 drwxr-xr-x 3 qemu qemu 19 Aug 2 21:18 / var/lib/libvirt/qemu/channel 10635 0 drwxr-xr-x 2 qemu qemu 6 Aug 2 21:18 / var/lib/libvirt/qemu/channel/target.

3. Find files whose contents have been modified in the last week in the / var directory, and the owner is neither root nor postfix.

[root@localhost] # find / var/-mtime-7-not\ (- user root-o-user postfix\)-ls67136164 4-rw- 1 setroubleshoot setroubleshoot 3286 Aug 10 22:34 / var/lib/setroubleshoot/setroubleshoot_database.xml1485002 0 drwxrwx--T 6 gdm gdm 79 Aug 10 22:33 / var/lib/gdm136239626 4-rw-r--r-- 1 gdm gdm 43 Aug 10 22:33 / var/lib/gdm/.pulse/9987c5b50f134797b202165f43b10443-default-sink136239627 4-rw-r--r-- 1 gdm gdm 42 Aug 10 22:33 / var/lib/gdm/.pulse/9987c5b50f134797b202165f43b10443-default-source 106634-rw- 1 gdm gdm 930 Aug 10 22:33 / var/lib/gdm/.ICEauthority.

4. Find files that have no owners or groups on the current system and have been accessed in the most recent week

[root@localhost ~] # find /-atime-7\ (- nouser-o-nogroup\)-lsfind:'/ proc/2924/task/2924/fd/6': No such file or directoryfind:'/ proc/2924/task/2924/fdinfo/6': No such file or directoryfind:'/ proc/2924/fd/6': No such file or directoryfind:'/ proc/2924/fdinfo/6': No such file or directory139610001 0 drwx- 2 1000 1000 6 Aug 2 21:26 / tmp/.esd-1000 10683 0 drwxr-xr-x 2 1000 1000 6 Aug 2 21:26 / tmp/hsperfdata_nieda 10680 0 drwx- 2 1000 1000 6 Aug 2 21:26 / tmp/tracker-extract-files.1000 10648 4 drwx- 14 1000 1000 4096 Aug 2 21:26 / home/nieda67136350 0 drwxr-xr-x 4 1000 1000 37 Aug 3 06:08 / home/nieda/.mozilla139173477 0 drwxr-xr-x 2 1000 1000 6 Jun 10 2014 / home/nieda/.mozilla/extensions203103518 0 drwxr-xr-x 2 1000 1000.

5. Find all files in the / etc directory that are larger than 1m and whose types are ordinary files

[root@localhost ~] # find / etc/-size-1m-type f | xargs ls-lh-rw-r--r--. 1 root root 0 Sep 10 2011 / etc/akonadi/mysql-local.conf-rw-. 1 root root 0 Jul 27 2015 / etc/cron.deny-rw-. 1 root root 0 Aug 3 06:08 / etc/crypttab-rw-. 1 root lp 0 Nov 20 2015 / etc/cups/classes.conf-rw-r--r--. 1 root lp 0 Nov 20 2015 / etc/cups/client.conf-rw-r--r--. 1 root lp 0 Nov 20 2015 / etc/cups/lpoptions-rw-. 1 root lp 0 Nov 20 2015 / etc/cups/printers.conf-rw-r--r--. 1 root root 0 Aug 12 2015 / etc/environment-rw-r--r--. 1 root root 0 Jun 7 2013 / etc/exports-rw-r--r--. 1 root root 0 Mar 6 2015 / etc/ghostscript/9.07/cidfmap.local-rw-r--r--. 1 root root 0 Mar 6 2015 / etc/ghostscript/9.07/CIDFnmap.local-rw-r--r--. 1 root root 0 Mar 6 2015 / etc/ghostscript/9.07/Fontmap.local-rw-r--r--. 1 root root 0 Jun 7 2013 / etc/motd-rw-. 1 root root 0 Aug 3 06:10 / etc/.pwd.lock-rw-r--r--. 1 root root 0 Nov 20 2015 / etc/qemu-kvm/target-x86_64.conf-rw-r--r--. 1 root root 0 Nov 20 2015 / etc/security/console.apps/xserver-rw-. 1 root root 0 Aug 18 2015 / etc/security/opasswd-rw-r--r--. 1 root root 0 Nov 21 2015 / etc/selinux/targeted/contexts/files/file_contexts.local-rw-r--r--. 1 root root 0 Nov 21 2015 / etc/selinux/targeted/contexts/files/file_contexts.subs-rw-. 1 root root 0 Nov 21 2015 / etc/selinux/targeted/modules/active/netfilter_contexts-rw-r--r--. 1 root root 0 Nov 21 2015 / etc/selinux/targeted/modules/semanage.read.LOCK-rw-r--r--. 1 root root 0 Nov 21 2015 / etc/selinux/targeted/modules/semanage.trans.LOCK-rw-r--r--. 1 root root 0 Jun 10 2014 / etc/sysconfig/run-parts-rw-r--r--. 1 root root 0 Jun 10 2014 / etc/wvdial.conf

6. Find files in the / etc directory that all users do not have write permission to

[root@localhost] # find / etc/-not-perm / 222r-ls134499699 196-Rafael-1 root root 198453 Aug 3 06:09 / etc/pki/ca-trust/extracted/java/cacerts201850195 352-Rafael-1 root root 359773 Aug 3 06:09 / etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt330861-Rafael-1 root Root 266702 Aug 3 06:09 / etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem330862 Ruki Rafael-1 root root 217510 Aug 3 06:09 / etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem330863 208Ruki Ruki-1 root root 211626 Aug 3 06:09 / etc/pki/ca-trust/extracted/pem/objsign-ca -bundle.pem625260 4-r-1 root root 45 Aug 3 06:09 / etc/openldap/certs/password136242234 4-1 root root 785 Aug 16 2016 / etc/gshadow134317908 4-1 root root 795 Aug 13 18:26 / etc/gshadow-134317916 4-1 root Root 1303 Aug 16 2016 / etc/shadow-136242221 4-1 root root 1183 Aug 16 2016 / etc/shadow1058222 4-r Sep Sep 6 2015 / etc/lvm/profile/cache- 1 root root 338 Sep 6 2015 / etc/lvm/profile/cache- Smq.profile1058224 4-Rafael Nov 21 2015 / etc/lvm/profile/command_profile_template.profile1058225 4-root root 2249 Nov 21 2015 / etc/lvm/profile/command_profile_template.profile1058225 4-Ruki-1 root root 828 Nov 21 2015 / etc/lvm/profile/metadata_profile_template.profile1058226 4-Rafael-1 root root 76 Sep 6 2015 / etc/lvm/profile/thin-generic .profile1058227 4-Rafael-1 root root 80 Sep 6 2015 / etc/lvm/profile/thin-performance.profile136009869 4-Rafael-1 root root 460 Nov 20 2015 / etc/dbus-1/system.d/cups.conf1788506 4-Rafael-1 root root 63 Nov 20 2015 / etc/ld.so.conf.d/kernel -3.10.0-327.el7.x86_64.conf139173483 6852-Rafael-1 root root 7014922 Aug 3 06:18 / etc/udev/hwdb.bin135840874 4-Rafael-1 root root 33 Aug 3 06:10 / etc/machine-id1058267 4-Rafael Rafael-1 root root 146 Nov 20 2015 / etc/pam.d/cups139259112 8 -Rmurmurr-1 root root 4188 Jul 7 2015 / etc/sudoers

7. Find at least one class of files in the / etc directory that the user does not have the right to execute.

[root@localhost etc] # find / etc/-not-perm-111111-ls

8. Find files under the / etc/init.d directory where all users have execute permission and other users have write permission

[root@localhost etc] # find / etc/init.d-perm-113-ls134370978 0 lrwxrwxrwx 1 root root 11 Aug 3 06:08 / etc/init.d-> rc.d/init.d [root@localhost etc] # ls / etc/init.d/-ltotal 32Murray 1 root root 13948 Sep 16 2015 functions-rwxr-xr-x. 1 root root 2989 Sep 16 2015 netconsole-rwxr-xrwx. 1 root root 6630 Sep 16 2015 network-rw-r--r--. 1 root root 1160 Nov 20 2015 README [root@localhost etc] # find / etc/init.d/-perm-113-ls203103221 8-rwxr-xrwx 1 root root 6630 Sep 16 2015 / etc/init.d/network

Note: when looking for files in a directory, you must add / at the end of the directory, otherwise you will check the directory itself

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report