1, the position variable $$ returns to the routine of the PID $ 0 representative of the script itself the name of the first parameter contact runtime $ 1 represents a script (common) then $ 2 represents the script runs the second parameter contact runtime $ 3 represents a script third parameters $ * Represents all parameters $ @ is on behalf of all parameters $ # display the number followed by the parameters $? execute a command on success, returns 0, failure of non-zero (common) returns 2, variable calculation commonly used numerical order : (()), the let, expr, BC, $ [] # 100 = A # B = 200 is # $ C = ((A + $ B $)) - arithmetic operation, calculates the integer only, two brackets, not less # c = $ [$ a + $ b] - arithmetic operation, only calculates the integer # let c = a + b - arithmetic, only calculates the integer # expr $ a + $ b - arithmetic, only integer calculation, pay attention to the format, there are spaces on both sides of the equal sign # a = 3.14 # b = 1.23 # echo "$ a + $ b" | bc - arithmetic, decimal integer can be calculated, bc is a calculator 4.37 bc is a calculator # bc Whether it is an integer or decimal BC can be used to calculate, with | piped to 3, and determines the logical sequence ; - perform End front, regardless of success or not, without performing the logic behind && - front successfully performed, was performed behind; failed front, behind not perform a logical aND || - front failed, before the implementation of the back; successful execution front, back or not execution logic ; these symbols are connected && || command - correct determination method: LS # / Test / && echo 'existed' || echo 'Not existed' - error determination method: # LS / testfsdafas / || echo 'existed' && echo 'Not existed' two determination conditions. 1. condition judgment syntax [ syntax] format 1: test <test expression> - not used Example: test 1.txt -f 0 && echo echo. 1 || 2 format: [<test expression>] - common Example: [-f 1. txt] && echo 1 || echo 0 format 3: [[<test expression>]] - not used Example: [[-f 1.txt]] &&1 || 0 echo echo 2. Analyzing the various conditions (1) and the file type of the file exists Analyzing -e whether there are (exist) -f if a regular file -d whether the directory -S socket file -p pipe manage files -c character-character file -b block block file -L soft link # test -f 1.txt && echo 0 || echo. 1 . 1 # [-f 1.txt] && echo echo. 1 || 0 - Note expressions [] at the front and a space . 1 # [[-f 1.txt]] && echo. 1 || echo 0 - Note expressions [] at the front and a space . 1 # [[-f 1.txt]!] && echo echo. 1 || 0 0 inputs a file, it is determined what type # / bin /! the bash Read -p "Please enter a file name:" filename [-e $ filename] || echo "file does not exist" [-f $ filename] && echo "$ {filename} is an ordinary file" [-d $ filename] && echo "$ {filename} is a directory" [$ -S filename] && Echo "$ {filename} is the socket file" [$ -p filename] && Echo "$ {filename} pipeline File" [-C $ filename] && echo "$ {filename} is the character file" [$ filename -b] && echo "$ {filename} is the block file " [$ -L filename] && echo" $ {filename} is a soft link file " ------------------------------- ------------- ! # / bin / the bash [-e $. 1] || echo "file does not exist" [-f $. 1] && echo "$ {} is an ordinary file. 1" [- d $ 1] && echo "$ {1} is a directory" [-S $. 1] && echo "$ {. 1} is the socket file" [-p $. 1] && echo "$ {. 1} is a pipe file" [-C $. 1] && echo "$ {1} is a character file" [$. 1 -b] && echo "$ {1} is a block file" [-L $. 1] && echo "$ {1} is a soft link file" (2) determine the file permissions associated -r which the current user is readable - valid for the average user, root user inaccurate (rw allowed) -w which the current user is writable -x whether its current user execution - common Are there suid -u -g whether sgid whether there is -k t bits (. 6) Analyzing logic -s is blank file - not empty is true, empty is false #! / bin / SH [-e $ 1] || echo "File does not exist" [-x $ 1] && SH $ 1 || echo "no execute permissions" (3) compare two files to determine the file1 -nt file2 compare file1 whether than file2 (new new Within last) file1 file2 -ot comparison whether file1 file2 older than (old Within last) file1 file2 -ef whether the comparison for the same file (4) is determined between integers -eq equal -ne unequal -gt greater than -lt less than -ge greater than or equal -le or less (5) is determined between strings -z string is empty - is determined whether the variable is empty -n is a non-empty string string1 = string2 are equal string1 =! string2 unequal # A = # [the -Z $ A] && echo echo. 1 || 0 . 1 # A. 1 = # [the -Z $ A] && echo echo. 1 || 0 0 -a two conditions are met, it is true -o both meet First, it is to true ! Non (7) determines the difference between the three formats used (arithmetic) in (()) are used in the [] and [[]] in the (arithmetic) Description == -eq equal, equal ! = -Ne not equal , is not equal to > -gt greater than, greater than > = -ge greater equal, greater than or equal <-lt less than, less than <= -le less equal, less than or equal in [] is the logical operator [[]] in using logical operators instructions -a && with -o or || !! non # [-e install.log -a -f install.log] && cat install.log || echo " file does not exist" # [-e 1.sh -a -x 1.sh] && ./1.sh || echo " file does not exist or file not authorized to perform" # [[-e 1.sh && -x 1.sh]] && ./1 .sh || echo "file does not exist or file not authorized to perform." if condition (condition true or false) the then - either a separate line, or later if, to add a semicolon Statement Fi or IF for condition Condition; the then Statement Fi IF (if / is assumed) conditions (true / false) - the condition is true, the code is executed the then (so) codes; Fi (2) double-branched structure if condition (condition true or false) the then Statement - condition is true, execution portion of the else Statement - condition is false, execution section Fi (. 3) a multi-branched structure if condition (condition true or false) the then Statement elif for condition condition the then Statement elif for condition condition the then Statement the else Statement Fi
A document input, determines whether there is, the file type #! / Bin / the bash [-e $. 1] && echo "file exists" || echo "file does not exist" [-f $. 1] && echo "$ {}. 1 is a general file " [-d $. 1] && echo" $ {. 1} is a directory " [-S $. 1] && echo" $ {. 1} is the socket file " [-p $. 1] && echo" $ {. 1} is a pipe file " [ -c $ 1] && echo "$ {1} is a character file" [$. 1 -b] && echo "$ {}. 1 is a block file" [-L $. 1] && echo "$ {}. 1 is a soft link file" - --------------------------- input a file determines if the file type of a single limb: ! # / bin / the bash if [-e $. 1] ; the then "file exists" echo Fi # logical operator to determine the file does not exist IF [-e $. 1!]; the then echo "file does not exist" Fi # determines the file type IF [-f $. 1]; the then echo "$. 1 is an ordinary file" Fi IF [-d $. 1]; the then echo "$. 1 is a directory file" Fi IF [-S $. 1]; the then echo "$. 1 is a socket file" Fi IF [-p $. 1]; the then echo "$. 1 is a pipe file" Fi IF [ -C $. 1]; the then echo "$. 1 is a character file" Fi IF [-b $. 1]; the then echo "$. 1 is a block file" Fi IF [-L $. 1]; the then echo "$. 1 is a link file" Fi ---- ------------------------ two-branch and multi-branch # determine whether a file exists #! / bin / bash whether a file exists # judge if [-e $ 1 ] the then echo "file exists" the else echo "file does not exist." Fi # file type is determined if [-f $ 1]; then echo "$ {1} is an ordinary file" elif [-d $. 1]; the then echo "$ {. 1} is a directory" elif [-S $. 1]; the then echo "$ {. 1} is the socket file" elif [-p $. 1]; the then echo "$ {. 1} is pipe file " elif [-C $. 1]; the then echo" $ {. 1} is a character file " elif [-b $. 1]; the then echo" $ {. 1} is the block file " the else echo" $ {. 1} is a soft link file " fi ------------------------------------------- how to determine a host Are online? ! # / bin / bash the ping -c $ 1 & 2> / dev / null IF (($ == 0)?); the then echo "$ 1 online" the else echo "$ 1 is not online" fi -------- --- #! / bin / bash the ping -c $ 1 & 2> IF [$ -eq 0?]; the then echo "$ 1 online" the else echo "$ 1 is not online" fi