Shell Learning Notes

Source: Internet
Author: User

█ Start line The shell program must start with the following line (must be in the first line of the file)

#!/bin/sh

The symbol #! is used to tell the system that the parameter behind it is the program used to execute the file █ executable when editing a good script, you must also make it available for execution if you want to execute the script. To make the script executable: chmod +x filename█ pipe (|) :  the output of one command as input to another command. █ Redirect: Outputs the result of the command to a file instead of the standard output (screen). > Write files and overwrite old files >> add to end of file, keep old file contents special characters █ double quotes and single quotation marks can hold the attributes of the variable, but special characters in single quotation marks are only ordinary characters. #name = "$LOGNAME is hh"//root is Hh#name= ' $LOGNAME are hh '//$LOGNAME is HH usually uppercase characters are system preset variables, self-setting variables can use lowercase characters   █ Cancel variable: █shell special variable with unset variable name $: The file name of the script executed: the first parameter name of the script file is $#: The number of arguments passed to the script $*: Displays all parameters passed to the script in a single string $$: The current process ID number for the script to run? : Displays the exit status of the last command, 0 means there is no error, and the other value indicates an error █ common shell inline commands: (1) Echo: Display variable contents (2) Env: Displays the main preset variable contents (3) Set: Displays the contents of the current system (4) Read: Read variable contents from keyboard (5) Declare: Declaring variable Content-A: Defining an array array-f: defined as a function function-i: defined as an integer integer-r: defined as "Read Only"-X: Defined as a pass-through environment output variable, █ arithmetic operator: \* : Multiplication of two variables. /: Divide two variables. * *: Power the two variables. %: modulo operation, the first variable divided by the second variable to find the remainder. *=: Multiply equals, multiplied by the second variable on the basis of the first variable. /=: In addition to equals, divides the second variable on the basis of the first variable. %=: Modulo assignment, the first variable takes a modulo operation on the second variable, and assigns it to the first variable. When using these operators, you need to be aware of the order of operations. For example, enter the following command to output the result of the 1+2. The echo 1+2  shell did not output the result 3, but instead output the 1+2. █ change the order of operations. A. Use expr to change the order of operations. such as:echo  ' expr 1 + 2 ' to output the results of 1+2, using expr to represent the following expression as a mathematical operation. Note that ' It's not a single quote, it's the symbol above the TAB key. b, with let instructionsMathematical operations. The result of the operation can be assigned to the variable B, and the Operation command is B=let 1 + 2. Then use the Echo $b to output the value of B. c, using $[] to represent mathematical operations. Writes a mathematical operation to the brackets in the $[] symbol, in which the contents of the brackets are first mathematically calculated (the brackets can contain spaces). For example command Echo $[1+2], will output 3 █ relational operator:-eq: numeric equality-ne: Numeric Unequal-ge: Number 1 is greater than equals number 2-lt: Number 1 less than 2-GT: Number 1 is greater than number 2-le: Number 1 is less than equals number 2 example: Input Test 1-lt 2 & & echo "Yes" prints yes█ Boolean operation:-A: (and) both conditions are set. Example: Test-r file-a-x file, which is true if file has both R and X permissions. -O: (or) any one of the two conditions is established. Example: Test-r file-o-x file, which is true when file has R or x permissions.!: the opposite state. Example: Test! -R file, which is true when file does not have R permission. █ string Operator: =: two strings equal! = : Two strings unequal-Z: empty string-N: Non-empty string █ conditional expression for test file status:-E: Whether there is a-D: is the directory-F: Is the file-L: Symbolic connection-S: File non-null-r: Readable-W: writable-x: Executable █ backslash: Use backslashes to make the output of one command a command line argument of another command 。 Command: Find. -mtime-1-type F-print is used to find files that have been modified in the last 24 hours (-mtime-2 represents the last 48 hours). If you want to hit a package with all the files you find, you can use the following script: #!/bin/sh# The Ticks is backticks (') not normal quotes ('): TAR-ZCVF lastmod.tar.gz ' Find. -mtime-1-type f-print ' █ "if" expression: if the condition is true then the following part is executed: if ...; Then ..... elif ...; Then ... else ...    fi in most cases, you can use test commands to test the condition. For example, you can compare strings, determine whether a file exists and whether it is readable, and so on. The condition test is usually represented by "[]". Note that the space here is important. The space to ensure the square brackets. [-F "somefile"]: Determine if it is a file[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions [-n ' $var]: Determine if the $var variable has a value ["$a" = "$b"]: Determine whether a $ A and $b are equal  █case expression: Example: #!/bin/shfty Pe= ' file ' "' Case" $ftype " in" $1:zip Archive "*)     Unzip" $ "" $1:gzip Compressed "*)      Gunzip "$" "$1:bzip2 Compressed" *)     BUNZIP2 "$" *) Error "File" can not is uncompressed with Smartzip " ; Esac█for loop Syntax One:-----for (initial value; limit value; execution Step) The DO program done-----Initial value: The variable's starting value limit in the loop: when the value of the variable is within this limit, the loop execution step is continued: The amount of variation of the variable in each cycle   Syntax two:-----for var in con1 con2 Con3 ...//var is a variable do program done-----The first loop when the content of the $var is con1 the second loop, when the content of the $var is con2 the third loop, $ The content of Var is the Con3█while loop while loop, which enables you to repeatedly execute a set of commands until certain conditions occur. It is usually used when you need to manipulate the value of a variable repeatedly. -----While [conditional] do program done-----Enter the while loop when the condition is true, and exit the loop █until loop when the condition is not established sometimes you need to execute a set of commands until a certain condition is true. The syntax is as follows:-----until command do program done-----This way is the opposite of the while loop, when the command is set to exit the loop, otherwise the loop █select loop syntax is as follows:-----Select var in word1 word2 ... Do program done-----█shell Array $a= (123 3 5) $echo $a          //get first element by default 123$echo ${a[1]}   &NBSP ; Access 34$echo ${a[@]}    /Access entire array, @ or * get the entire array 123 3 5$echo ${#a [@]}    //get the length of the array 4$echo ${# A[3]}    //get string length 1$echo ${a[@]:1:2}//Slice way get part of array contents  34 3$echo ${a[@]:2}  /starting from the second element 3 5$echo ${a [@]::2}  //to the second element 123 34█ create function declaration a function syntax: function_name () {    List of commands}

Shell Learning notes

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.