Shell basic notes

Source: Internet
Author: User
Tags uppercase letter vars
Note: No space is allowed between the left and right sides of "=". The semicolon program 1 # is not required at the end of the bash statement #! /Bin/sh # echo somethingecho "Hello World" Echo "Hello !! "CD/root/export topmkdir./ABC Program 2, variables do not need to be of type or definition, $ is added when used, and no need to assign values #! /Bin/sh # Set variable AA = "Hello World" # print aecho "A is: "Echo $ A How to output the variable num = 2 Echo" this is the $ {num} Nd "in the string will become this is the 2nd important prompt: $ # Number of command line parameters for passing in scripts $ * parameter values of all command lines (separated by spaces) $ # command itself (shell file name) $1 The first command line parameter $ N in the nth command line parameter program 3s3 #! /Bin/shecho "Number of vars: "$ # echo" values of vars "$ * echo" value of var1 "$1 echo" value of var2 "$2 Echo" value of var3 "$3 Echo" value of var4 "$4 run and enter. /S3 1 2 3 4 the output is number of vars: 4 values of vars: 1 2 3 4 value of var1: 1 Value of var2: 2 value of var3: 3 value of var4: 4 when a local variable is assigned a value for the first time, the local keyword can be added to declare a local variable, for example: S4 #! /Bin/bashhello = "var1" Echo $ hellofunction func1 () {local Hello = "var2" Echo $ Hello} func1echo $ Hello run./S4 get var1var2var3 !!!! If local is removed, the preceding program outputs the VR1, vr2, and vr2if statement formats: if [expression] Then # code blockfi or if [expression] Then # code blockelse # code blockfi or multiple ifif [expression] Then # code blockelse if [expression] Then # code blockelse # code blockfififi # Each if corresponds to a fi comparison: compare operation Integer Operation string operation same-eq = different-ne! = Greater than-GT> less than-lt <greater than or equal to-ge less than or equal to-Le is empty-Z is not empty-neg: note that there are spaces on the left and right sides of the "[" "]" symbol, "=" both the left and right sides have spaces to compare whether integers A and B are equal. If [$ A = $ B] (you can also use-eq) judge whether integer A is greater than integer BIF [$ A-GT $ B] To compare whether string a and string B are equal if [$ A = $ B] to determine whether string a is null if [-z $ A] judge whether integer variable A is greater than BIF [$ A-GT $ B: -E file already exists-F file is a common file-s file size is not zero-D file is a directory-r file or directory for the current user can read-W file or directory for the current users can write-X files or directories to the portable program of the current user. 5 #! /Bin/shfolder =/home [-R "$ folder"] & Echo "can read $ folder" (the current judgment is true, and the result is after execution) [-F "$ folder"] | echo "this is not file" (the statement is considered as a false execution | the following statement) Because folder is a directory and readable, therefore, the first statement is not output. Because folder is a directory and is not a file, the format of the second statement for loop is output: for VaR in [LIST] Do # code block done where $ VaR is a loop control variable, and [LIST] is a set that VaR needs to traverse. Do/done includes the loop body, it is equivalent to braces in C language. In addition, if do and for are written in the same line, you must add ";" before do. For example: For $ VaR in [LIST]; Do program S6 #! /Bin/bash for day in Sun mon Tue wed Thu Fri Sat Do echo $ day done program S8: if the list is contained in a pair of double quotation marks, it is considered as an element, run only once. #! /Bin/bash for day in "Sun mon Tue wed Thu Fri Sat" Do echo $ day done Note: $ is not added before the variable name in the row where for is located, because this is the process of assigning values, and in the loop body, the variable of the ECHO row must be added with the $ symbol. While loop while [condition] Do # code block done until loop until [condition] Do # code block done while and until differ in that while is executed when it is true, while until is executed when it is false. Case statement: the case structure in Bash is similar to the switch statement function in C language. Multiple option branch control can be performed. Case "$ Var" incondition1); condition2); *) default statments; esac program S7 #! /Bin/bashecho "hit a key, then hit return. "Read keypresscase" $ keypress "in [A-Z]) echo" uppercase letter "; [A-Z]) echo" lowercase letter "; [0-9]) echo "digit"; *) echo "punctuation, whitespace, or other"; esac special: if [-Z $ A] | [-Z $ B] Or Exit 1 error exits. You can omit firead keypress to read a row in IF.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.