shell_01
File expression
if [ -f file ] If the file exists if [ -d ... ] If directory exists if [ -s file ] If the file exists and is not empty if [ -r file ] If the file exists and is readable if [ -w file ] If the file exists and is writable if [ -x file ] If the file exists and is executable
Integer variable expression
if [ int1 -eq int2 ] If int1 be equal to int2 if [ int1 -ne int2 ] If not equal to if [ int1 -ge int2 ] If>= if [ int1 -gt int2 ] If> if [ int1 -le int2 ] If<= if [ int1 -lt int2 ] If<
String variable expression
if [ $a = $b ] If string1 be equal to string2 The string allows the assignment sign to be used as an equal sign if [ $string1 != $string2 ] If string1 Not equal to string2 if [ -n $string ] If string Non empty(Non-zero), return 0(true) if [ -z $string ] If string Empty if [ $sting ] If string Non empty, return 0 (and-n similar)
s ^ h ^ e ^ l ^ in ^ i ^ f ^ from - z ^ to - d ^
[ -a FILE ] If FILE If it exists, it is true. [ -b FILE ] If FILE True if a special file exists and is a block. [ -c FILE ] If FILE If it exists and is a word special file, it is true. [ -d FILE ] If FILE True if it exists and is a directory. [ -e FILE ] If FILE If it exists, it is true. [ -f FILE ] If FILE True if it exists and is an ordinary file. [ -g FILE ] If FILE Exists and has been set SGID Is true. [ -h FILE ] If FILE True if there is a symbolic connection. [ -k FILE ] If FILE True if the adhesive bit exists and has been set. [ -p FILE ] If FILE Exists and is a name pipeline(F If O)Is true. [ -r FILE ] If FILE If it exists and is readable, it is true. [ -s FILE ] If FILE True if it exists and the size is not 0. [ -t FD ] If file descriptor FD True if open and pointing to a terminal. [ -u FILE ] If FILE Exists and is set SUID (set user ID)Is true. [ -w FILE ] If FILE If FILE If it exists and is writable, it is true. [ -x FILE ] If FILE True if it exists and is executable. [ -O FILE ] If FILE Existing and valid user ID Is true. [ -G FILE ] If FILE True if it exists and belongs to a valid user group. [ -L FILE ] If FILE True if there is a symbolic connection. [ -N FILE ] If FILE existence and has been mod If ied since it was last read Is true. [ -S FILE ] If FILE True if a socket exists. [ FILE1 -nt FILE2 ] If FILE1 has been changed more recently than FILE2,or If FILE1 exists and FILE2 does not Is true. [ FILE1 -ot FILE2 ] If FILE1 than FILE2 To be old, perhaps FILE2 Exist and FILE1 True if it does not exist. [ FILE1 -ef FILE2 ] If FILE1 and FILE2 Pointing to the same device and node number is true. [ -o OPTIONNAME ] If shell Options“ OPTIONNAME" True when on. [ -z STRING ] "STRING" If the length of is zero, it is true.
Location parameters
$0 The running name of this program $n The second part of this program n Parameter values, n=1..9 $* All parameters of this program,This option can have more than 9 parameters. $# Number of parameters in this program $$ Of this program PID(The current process in which the script is running ID number) $! To execute the previous background instruction PID(The process of the last process running in the background ID number) $? The return value of the last instruction executed (Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error) $- display shell Current options used, and set The command functions are the same $@ Follow $*Similar, but can be used as an array
cut is a selection command, which is to analyze a piece of data and take out what we want. Generally speaking, the selected information is usually analyzed according to the "line", not the whole information.
(1) Its syntax format is:
cut [-bn] [file] or cut [-c] [file] or cut [-df] [file]
instructions
The cut command cuts bytes, characters, and fields from each line of the file and writes them to standard output.
If you do not specify the File parameter, the cut command reads the standard input. One of the - b, - c, or - f flags must be specified.
main parameter
-b: Split in bytes. These byte positions ignore multi byte character boundaries unless the - n flag is also specified.
-c: Split in characters.
-d: Custom separator, tab by default.
-f: Used with - d to specify which area to display.
-n: UN split multi byte characters. Use only with the - b flag. If the last byte of the character falls within the range indicated by the List parameter marked with - b
Within the range, the character will be written out; Otherwise, the character will be excluded.
(2) What is the general basis of cut? In other words, how can I tell cut what I want to locate?
The cut command mainly accepts three positioning methods:
First, bytes, with option - b
Second, characters, with the option - c
Third, fields, with the option - f
[]: [condition] is a judge. If the condition is true, it returns 0 and if it is false, it returns 1.
Echo: echo STR print string str (default line feed)
-EQ: INT1 - EQ INT2 is an integer equality judge. When integers on both sides are equal, it returns true 0, otherwise it returns false 1.
$? : $ The symbol is used to refer to the value of the variable,? Is a special variable that stores the return value of the immediate precursor command execution, $? The general combination is used to check the return value of the execution result of the previous command.
Exit: exit 0 ~ 255, which is used to return its own exit status code. If the value exceeds 255, the modulo 256 operation will be automatically performed.
shell_02
In bash, $() and ` ` (backquotes) are used as command substitution
Command replacement is similar to variable replacement. They are used to reorganize the command line. First complete the command line in quotation marks, then replace the results, and then reorganize the new command line.
The shell scans the command line and finds the $(cmd) structure. It executes the cmd in $(cmd) once to get its standard output, and then puts this output into the original command
$(()) can perform integer operation, binary conversion and redefine variable values
sed -e can execute multiple commands in one line
sed -e 's/wang/w/g;s/xu/x/g' user.txt,
By the way,'s/wang/w/g;s/xu/x/g ', s stands for search, G is as many matches as possible, and how many are replaced, so it means to replace user Replace wang with W and Xu with x in text
Character wildcard
. match any single character
[] matches any single character within the specified range
[^] matches any single character outside the specified range
[: alnum:] letters and numbers
[: alpha:] stands for any English upper and lower case characters, i.e. A-Z, a-z
[: lower:] lowercase [: upper:] uppercase
[: blank:] white space characters (spaces and tabs)
[: Space:] horizontal and vertical white space characters (wider range than [: blank:])
[: cntrl:] non printable control characters (backspace, delete, alarm bell...)
[: digit:] decimal digit [: xdigit:] hexadecimal digit
[: graph:] printable non white space characters
[: print:] printable characters
[: punct:] punctuation
1. Character set [: and:]
2. Sort symbols [. And.]
3. Equivalent character set [= and =]
shell_03
tr command
tr character can replace, compress and delete characters from standard input. It can program one group of characters to another group of characters, which is very powerful.
tr [option] [char_set 1] [char_set 2] tr Command will“ set 1"Replace the character set in with“ set 2".
tr -c -d -s
-c replace the character set with the complement of the character set in string 1. The character set is required to be ASCII.
-d delete all input characters in string 1.
-s delete all repeated character sequences and keep only the first one; The recurring string is about to be compressed into a string.
shell_04
Specify: sign separator to display the first column
cut -d":" -f1 filename cut -d: -f1 filename
Getopts command format: getopts OPTSTRING VARNAME
OPTSTRING: tell getopts what options and parameters there will be (use ":" after options to indicate that parameters need to be added after options)
VARNAME: save the options obtained by getopts
getopts ahf: var
Tell getopts to find the - A, - h, - f options, where the f option needs to be followed by a parameter, and the obtained options are saved to the variable var
While getopts: HVF: o: Varname # tells getopts that this script has four options - h, - v, - f, - o, - f and - o need to be followed by parameters
OPTIND: getopts uses OPTIND as the index to process the next parameter to be processed and record the current state.
OPTARG: in the above loop, a and b have a colon after each parameter. The colon indicates that there is a parameter value after the input parameter. When getopts finds the colon, it will process the parameter value entered by the user, and this parameter value is saved in OPTARG.
root@GO-EMAIL-1 ~]# sh 1.sh -ab foo
sh 1.sh -ab foo
-ab first element, OPTIND=1
foo 2nd element, OPTIND=2
The third element is empty OPTIND=3
So when processing - a, the next one is b, and the first element is still being processed, so print
a=1
When processing b, the next parameter is foo, but foo is not a parameter. SKIP is lost, and the next parameter is empty, optind=3
So print
b=3
important
. you can put another script into the execution script
Add in the execution script . Imported script name
After shifting n, the first n parameters will be destroyed (offset)
On the meaning of cat < < EOF statement
Cat < < EOF, end with EOF input character as standard input:
Cat > filename, create a file, output the standard input to the filename file, and end with ctrl+d as the input:
Cat > filename < < EOF, end with EOF as input, and the function of ctrl+d is the same:
If the redirected operator is < < -, the Tab at the beginning of the line where the delimiter (EOF) is located will be removed.
The basename command is only the file name or directory name used to remove the path and file suffix
1.basename file (directory) / / delete all contents before the last "/" symbol (including "/") of the path
2.basename file or directory suffix (suffix: optional parameter, specifying the file suffix string to be removed)
Date can be used to display or set the date and time of the system.
-d<character string>: Displays the date and time to which the string refers. Double quotation marks must be added before and after the string; -s<character string>: Set the date and time based on the string. Double quotation marks must be added before and after the string; -u: display GMT; --help: Online help; --version: Displays version information.
Date format string list
If you need to display the date in the specified format, you can use the string starting with "+" to specify its format
%H hour(With 00-23 To show). %I hour(With 01-12 To show). %K hour(With 0-23 To show). %l hour(With 0-12 To show). %M minute(With 00-59 To show). %P AM or PM. %r time(Including time, minutes and seconds, and 12 hours for hours AM/PM To show). %s Total seconds. The starting time is 1970-01-01 00:00:00 UTC. %S second(Expressed in local idioms). %T time(Including time, minutes and seconds, and hours are expressed in 24-hour system). %X time(Expressed in local idioms). %Z Downtown. %a Abbreviation for week. %A Full name of the week. %b Abbreviation of English name of month. %B Full English name of the month. %c Date and time. Input only date The command will display the same result. %d date(With 01-31 To show). %D date(Including mm / DD / yy). %j The day of the year. %m month(With 01-12 To show). %U Number of weeks in the year. %w The number of days of the week. 0 represents Sunday, 1 represents Monday, and so on. %x date(Expressed in local idioms). %y particular year(With 00-99 To show). %Y particular year(Expressed in four digits). %n When displayed, insert a new row. %t When displayed, insert tab. MM month(necessary) DD date(necessary) hh hour(necessary) mm minute(necessary) ss second(selectivity)