Shell Learning Log

Source: Internet
Author: User
Tags bit set case statement delete key

The variables of 0.shell are different from the environment variables, and the user environment area exists. Variables are assigned in the following way: Variable_name = variable_valuea= "Hello" $a the value of a on the variable, and several Linux commands associated with it need to be known. The first character must be a letter (a-z,a-z). You can use an underscore (_) without spaces in the middle. Punctuation cannot be used. Env is used to display variables in the user's environment area and their values; set is used to display variables and their values in the local data area and user environment area; unset is used to delete the current value of the specified variable, which will be specified as the Null;export command to transfer the variables in the local data area to the user environment area. 1.if use if ...; Then...elif ...; Then...else...fi2. Looping statements in shell script have the following formats: While [Cond1] && {| |} [Cond2] ...; do...donefor var in ...; Do...donefor ((cond1; cond2; cond3)) Do...doneuntil [Cond1] && {| |} [Cond2] ...; Do...doneexample:for s in 1 2 3 4doecho $sdonefor s in ' This is a test! ' Do echo $sdone 3.case case var inpattern 1) ...;; Pattern 2) ...;; *)... ;; Esac4. Use the number of command-line arguments, including $ #表示包括 $, $* represents the entire argument list, excluding $ A, which means that the parameter list for the file name is not included. $$ represents the ID of the current shell process, which is the PID, see the following code: $echo The $$ variable means that the current script's file name $n passed to the script or function parameters. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A. $ #传递给脚本或函数的参数个数. $* all parameters passed to the script or function. [email protected] All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below. $? The exit status of the last command, or the return value of the function. $$ the current shell process ID. For Shell scripts, this is the process ID where the scripts are located. The difference between $* and [email protected] $* and [email p rotected] All represent all parameters passed to a function or script, not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n". But when they are enclosed in double quotation marks (""), "$*" takes all parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the parameters to "$" "$" ... All parameters are output in the form "$n". -lt less than-gt greater than-le equals example: if [$#-lt 3]; Then ... [] for conditional testing, commonly used condition tests are: [-F "$file"] to determine whether $file is a file [$a-lt 3] to determine whether the value of $ A is less than 3, the same-gt and-le respectively means greater than or less than equals [-X "$file"] to determine whether $file exists and can be Row permissions, the same-R test file readability [-n ' $a] to determine if the variable $ A has a value, test the empty string with-z["$a" = "$b"] to determine whether the value of $ A and $b are equal [cond1-a cond2] to determine whether Cond1 and Cond2 are also established,-O means cond 1 and Cond2 have a set shift to the first in the argument list, followed by the next parameter to the new first, note that the parameter list does not include $. 5. Read-only variable readonly var delete variable unset var variable is deleted and cannot be used again; unset command cannot delete a read-only variable. 6.Shell variable substitution, command substitution, escape character a = 10echo-e "$a \ n"-e means to replace the escaped character. Output 10 non-e output ten \ n escape character meaning \ \ Backslash \a alert, Bell \b Backspace (delete key) \f page break (FF), move current position to the beginning of next page \ nthe line break \ n enter \ t horizontal tab (TAB) \v vertical tab Command replace the syntax ' command ' of command substitution Example: date= ' Date ' echo "date is $DATE" variable substitution form description ${var} variable original value ${var:-word} if the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var. ${var:=word} If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word. ${var:?message} If the variable var is empty or has been deleted (unset), then theMessage messages are sent to the standard error output, which can be used to detect whether Var can be assigned to a normal value. If this substitution appears in the shell script, the script will stop running. ${var:+word} If the variable var is defined, then return to word, but do not change the value of var. The 7.shell operator uses expr, which is an expression calculation tool. Example: val= ' expr 2 + 2 ' echo ' total value: $val arithmetic operator list Operator Description Example + addition-subtraction \* multiplication/division% take remainder = Assignment = = Equal. Used to compare two numbers, the same returns TRUE.! = is not equal. Used to compare two numbers, and returns true if they are different. The relational operator-le detects whether the left-hand number is less than or equal to the right and, if so, returns True. -ge detects if the number on the left is large equal to the right, and returns true if it is. -LT detects if the number on the left is less than the right and, if so, returns True. -GT detects if the number on the left is greater than the right and, if so, returns True. -ne detects whether two numbers are equal and returns true if they are not equal. -EQ detects whether two numbers are equal and returns true for equality. Boolean operator! Non-operation, the expression is true returns False, otherwise true. -O or operation, which returns true if an expression is true. ,-A and operations, two expressions are true to return true. The string operator list operator shows an example = detects whether two strings are equal and returns true for equality. [$a = $b] returns FALSE.! = detects whether two strings are equal and returns true if they are not equal. [$a! = $b] Returns TRUE. -Z detects if the string length is 0 and returns true for 0. [-Z $a] returns false. -N detects whether the string length is 0 and does not return true for 0. [-Z $a] returns true. STR detects if the string is empty and does not return true for null. [$a] returns TRUE. The file test operator list operator Describes an example-B file that detects if the files are block device files and, if so, returns True. [-B $file] returns FALSE. The-C file detects whether the files are character device files and, if so, returns True. [-B $file] returns FALSE. The-D file detects whether the files are directories and, if so, returns True. [-D $file] returns false. -F file detects whether the files are normal files (neither directories nor device files), and returns True if yes。 [-F $file] returns TRUE. The-G file detects if the SGID bit is set and returns True if it is. [-G $file] returns false. The-K file detects whether the files have a sticky bit set (Sticky bit), and returns True if it is. [-K $file] returns false. The-P file detects whether the files are named pipes and, if so, returns True. [-P $file] returns false. -U file detects whether the file has a SUID bit set and returns True if it is. [-U $file] returns false. The-R file detects whether the files are readable and, if so, returns True. [-R $file] returns TRUE. The-W file detects whether the files are writable and, if so, returns True. [-W $file] returns TRUE. The-X file detects whether files can be executed and, if so, returns True. [-X $file] returns TRUE. -S file to detect whether the files are empty (the file size is greater than 0) and not NULL to return TRUE. [-S $file] returns TRUE. The-e file detects whether files (including directories) exist and, if so, returns True. [-e $file] returns TRUE. 8.shell string Single-quote string restriction: Any character in single quotation marks is output as-is, the variable in the single-quote string is not valid, single quotation marks cannot appear in single quotation marks (not in single quotation marks after using escape character). Advantages of double quotes: double quotes can have variable double quotes can appear escape character stitching string with double quotation mark string length: string= "ABCD" Echo ${#string} #输出 4 extract substring: string= "Alibaba is a great   Company "Echo ${string:1:4} #输出liba查找子字符: String=" Alibaba are a great company "Echo ' Expr index" $string "is" grab a string from a position Expr substr "This was a test" 3 5 is a 9.shell array in the shell, with parentheses to represent the array, and the elements of the array separated by a "space" symbol. The general form of the definition array is: array_name= (value1 ... valuen) get the length of the array # get the number of array elements length=${#array_name[@]}# or length=${#array_name [*]}# Gets the length of the single element of the array lengthn=${#array_name [n]}10. Redirect to file echo "abc" > A.txtprintf can also output the test command to check if a condition is true, similar to square brackets ([]). 11.case statement echo "Input a:" read acase $a in#1) echo "1");; 1) echo ' you select 1 ';; 2) echo ' 2 ';; *) echo "oth";; Esac12. redirect command instructions > file redirect output to file. Command < file redirects the input to file. Command >> file redirects the output to file in an append manner. n > file redirects the file descriptor n files to filename. n >> files redirect files with file descriptor N as an append to file. N >& m merges the output file m and N. N <& m merges the input file m and N. << tag will start to tag the contents of tags between tag and end tag as input. 13. file contains like other languages, the Shell can also contain external scripts that merge the contents of the external script into the current script. The Shell contains scripts that can be used:. FileName or copy Plain text new window source filename

  

Shell Learning Log

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.