Linux Programming--Basics of Shell Programming (2)

a common command

1. find command

Basic usage, a simple example, starts from the root file and searches for a file named test

find / -name test -print

Using this command to search will take a long time, especially if it is mounted on other file systems, such as the file system on Windows (SAMBA server), because the files on Windows will be searched accordingly. At this point, you can use the -mount option to tell the find command not to search other file systems

find / -mount -name test -print

The find command also has some options for comparison and judgment. You can check the manual when using it.

2. grep command

Search for a string in a file, common options

Options meaning
-c Print the number of matching lines instead of outputting the matching lines
-i ignore case
-l List only filenames containing matching lines
-v i.e. search for lines that do not match
-E Enable extended mode

Two regular expressions

Common special characters in regular expressions

character meaning
^ point to the beginning of a line
$ point to the end of a line
. any single character
[] Contains a range of characters, any of which can be matched
? Matching is optional, but matches at most once
* Must match 0 or more times
+ must match one or more times
{n,m} The number of matches is between n and m, inclusive

If you want to use the above special characters as ordinary characters, you need to add \escaping.
Some special matching patterns can also be used in square brackets, such as

match pattern meaning
[:alnum:] Alphanumeric characters
[:alpha:] letter
[:ascii:] ASCII characters
[:blank:] space or tab
[:cncrl:] ASCII control characters
[:digit:] number
[:graph:] Non-control, non-space characters
[:lower:] Lower case letters
[:print:] printable characters
[:point:] Punctuation characters
[:space:] whitespace characters, including vertical tabs
[:upper:] uppercase letter
[:xdigit:] hexadecimal number

for example

查找文件readme.txt中以字母e结尾的行
grep e$ readme.txt

查找文件readme.txt中以字母a结尾的单词
grep a[[:blank:]] readme.txt

查找文件readme.txt中以字母TH开头的3个字母的单词
grep TH.[[:space:]] readme.txt

In a shell script, output a command to a variable

#!/bin/sh

whoisthere = $(who)
echo whoisthere

Arithmetic expansion
Put the arithmetic expression you want to evaluate in $((…)) in two parentheses
such asx=$(($x+1))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854800&siteId=291194637