Bash study summary

xiaoxiao2021-03-06  111

Variable reference: $ {abc} variable output: export myvar = ABC variable clearance: unset myvar

Get the file name: basename /Home/dir/abc.c gets ABC.C Get path name: dirname /Home/dir/abc.c Get / Home / Dir

Command Replacement: `LS` Alternate Command Replacement: $ (LS)

String: # in the left,% in the right $ {myvar # abc} from myvar left to find the first ABC, keep the ABC's left to find the last ABC from Myvar, keep ABC The next $ {myvar% abc} is looking for the first ABC from MyVar, retains $ {myvar %% ABC} in front of ABC, finds the last ABC from Myvar, retains $ {MyVar in front of ABC: Start: Length}

Conditional Statement IF [-f "$ file"] THEN Echo $ file is fileelif [-d "$ file"] THEN Echo $ File Is Directoryfi

For File In / ETC / A * Do IF [-f $ file] THEN Echo $ file is file elif [-d $ file] THEN Echo $ File Is Directory Fidone

While [Condition] Do StatementsDone

MYVAR = 0until [$ myvar-eq 10] do echo $ myvar myvar = $ ($ myvar 1)) DONE

Case "$ {x ## *.}" IN GZ) Gzunpack $ {SROOT} / $ {x} ;; bz2) BZ2Unpack $ {SROOT} / $ {x} ;; *) Echo "Archive Format Not Recognized." EXIT ; ESAC

Arithmetic: $ ((222 * 223))))

Command line variable: $ 0 command itself $ 1, $ 2, ... Each variable $ # Variable number $ @ Various variables separated by spaces

Operator: File comparison operator-E FileName exists, true [-e / var / log / syslog] -d filename If FileName is a directory, true [-d / tmp / mydir] -f filename if FileName is a regular file, the true [-f / usr / bin / grep] -l filename If the filename is a symbolic link, it is true [-l / usr / bin / grep] -r filename if FileName is read, Really [-r / var / log / syslog] -w filename If FileName can be written, it is true [-w /var/mytmp.txt] -x filename if filename can be executed, it is true [-l / usr / bin / grep] filename1 -NT filename2 If FileName1 is new than filename2, true [/ TMP / Install / etc / services -nt / etc / services] filename1 -ot filename2 If FileName1 is older than filename2, it is true [/ boot / bzimage -OT ARCH / I386 / BOOT / BZIMAGE] String Compare Operator (please pay attention to the use of quotation marks, this is a good way to prevent space for disrupt code) -z String If the String length is zero, it is true [-z "$ MyVar "] -n string If the String length is not zero, it is true [-n" $ myvar "] string1 = String2 If string1 is the same as String2, it is true [" $ myvar "=" one two three "] string1! = string2 If String1 is different from String2, it is true ["$ myvar"! = "One two through"]

Arithmetic comparison operator Num1 -EQ Num2 equal to [3-EQ $ mynum] Num1 -ne Num2 is not equal to [3 -ne $ mynum] Num1 -LT Num2 less than [3 -LT $ mynum] Num1 -le Num2 less than or equal to [3 -LE $ mynum] Num1 -gt Num2 is greater than [3 -GT $ mynum] Num1 -ge Num2 is greater than or equal to [3 -GE $ mynum]

Function: tarview () {local myvar = "Hello" # Local variable, otherwise the global variable echo -n "Displaying Contents of $ 1" IF [$ {1 ## *.} = Tar] the echo "(uncompressed tar) "TAR TVF $ 1 ELIF [$ {1 ## *.} = Gz] THO" TAR TZVF $ 1 ELIF [$ {1 ## *.} = Bz2] THEN ECHO "(BZIP2-Compressed TAR) "CAT $ 1 | BZIP2 -D | TAR TVF-Fi}

Entry for other Bash files Source FileName

转载请注明原文地址:https://www.9cbs.com/read-125173.html

New Post(0)