Some Tips I get WHEN Learning Bash

xiaoxiao2021-03-06  60

1.Space problem: when using "test", which is synonymous with "[", except that when the [command is used, a trailing] is also used just for readability Spaces must be put between the [braces and the condition being. Checked. by "=" in "=" in "=" = "=" = "=" m, ie script was simple and i raigure my script was simple and i figure, i just Remember this by thinking this "[" IS JUST LIKE WRITING "TEST", AS "if Test -f xxx" is Same as "if [-f xxx]", and String Comparist "-eq", SO I shop always add spaces. But if u Use "=" to assign a value to a variable, space shouth't be added, or it'll insper a error like "GR1X: Command Not Found" in Statement "GR1X =" good ""

2. Variable Problem Use quotes around variable as much as possible. Like in> read input> if [$ input = "gr1x"] if ur script user just press Enter, which means $ input contains nothing, just black string, then bash parse IT Like this:> IF [= "GR1X"] so error c c:;; IF UR Script User INPUT "AAA BBB CCC", Which Means Bash Will Parse It Like this:> IF [AAA BBB CCC = "GR1X"] Error Occured: "[: =: Too Many Arguments"

3. Control Structure ----------- for Variable in Values ​​Do Statements Done ----------- If Condition Then Statements Elif Condition Then Statements Else Statements Fi ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----- Case Variable in Pattern [| Pattern] ...) Statements ;; Pattern [| Pattern] ...) STATEMENTS ;; ... *) Statement 1 # uu Wildcard to make it default statement 2 #the Most Explicit Matches First Statement 3 #multiple Statements ............. ;; # this ;; can be omited as it's the last copy esac #interesting, Just Reverse the letter ORDER LIKE IF-FI ---------------- Statement1 && Statement2 && Statement3 && ... #the and list # to Execute A Series of Commans, Exec uting the next command only if all the previous commands have succeeded. statement1 || statement2 || statement3 || ... #The OR List # To execute a series of commands until one succeeds, then not execute any more commands following. # Short Circuit Evaluation: only the minimum number of statements is executed to determine the result, statements that can not affect the result are not executed.4 Use braces {} to enclose multiple statements to make a statement segment.

5. Always Define a Function Before You Can Invoke It, No Forward Declarations in Bash.

6. FUNTION Parameters: $ *, $ @, $ #, $ 1, $ 2 ... WHEN The Function Finishes, They Are Restored to Their Previous Values ​​--- The Positional Parameters to the Script. Use "local" to Declare Local Variable (NOTE: $ 0 ----- The name of the shell script. $ # ----- The number of parameters passed to script. $$ ----- The process id of the shell script, useful When Generate / TMP / TMPFILE _ $$ $ 1, $ 2, ...-------- The Parameters Given to The Script. $ @ ---- A List of All The Parameters, In A Single Variable. $ * ---- a list of all the parameters, in a single variable, separated by The First Character in The Environment Variable IFS. $ IFS --- An Input Field Separator; 7. Command:, The Null Command, CAN be used as an alias for true, Which Means "While" HAS The Same Meaning with "While:".> if [-d fred]; the ":> else> echo directory GR1X DID NOT> FI

8.The dot. Command executes the command in the current shell, useful if u wanna to set enviroumental variable within a script, as when a script executes an external command or script, a new environment (a subshell) is created, the command is Executed in the new environment, and the environment is kilonment apartfrom The EXIT code Code.

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

New Post(0)