Variable programming of shell statements in Linux

Source: Internet
Author: User
In Linux, shell statement variable programming shell variable 1: Two types of variables: temporary variables and permanent variables temporary variables are defined within the shell program and are only applicable within the program and invisible to other programs. Including user-defined variables and location variables. The permanent variable is... information & shell statement variable programming in Linux shell variable 1: Two types of variables: temporary variables and permanent variables temporary variables are defined within the shell program and are only applicable to the program, invisible to other programs. Including user-defined variables and location variables. A permanent variable is an environment variable and its value does not disappear with the execution of the shell script. 2: The user-defined variable must start with a letter or underline, and consist of letters, numbers, and underscores. When using a variable, you must add the "$" symbol before the variable. 3: When the location variable and the special variable shell explain the execution of user commands, the first part of the command is used as the command name, and the other part is used as the parameter. The parameter determined by the location on the command line is called the location parameter. For example: ls-l file1 file2 file3 $0 the program's file name ls-l $ n the program's Nth parameter value, n = 1-9 4: special variable $ * all parameters of this program $ # number of parameters of this program $ PID of this program $! Run the PID of the previous background command $? Execute the return value of the previous command 5: shell command read command: read data from the keyboard, and pay the variable. For example, the read NAME expr command performs arithmetic operations on integer variables, for example: there must be spaces between expr 3 + 5; otherwise, expr $ var1/$ var2 is expressed in the form of characters. the same as expr $ var1 \ * 10 multiplication requires the complex operation of escape character: expr 'expr 5 + 7' + 3 can be replaced with the command character 6: variable test statement: test condition 1> string test: test str1 = str2 test whether the string is equal to test str1! = Str2 test string is not equal test str1 test string str1 is not empty test-n str1 test string is not empty test-z str1 test string is empty 2> integer test: test int1-eq int2 test integer equal test int1-ge int2 test int1> = int2 test int1-gt int2 test int1> int2 test int1-le int2 test int1 <= int2 test int1-lt int2 test whether int1 <int2 test int1-ne int2 test whether int1 and int2 are not equal 3> File test: whether the file specified by test-d file is the file specified by the test-f file directory or the file specified by the test-x file conventional file can execute the file specified by test-r file Whether to read the file specified by test-w file, whether to write the file specified by test-a file, whether the file size specified by test-s file exists, and whether the test statement is not a zero variable is generally not used separately, it is generally used as a test condition for if statements. For example, the test statement of the if test-d $1 then fi variable can be simplified using []. for example, test-d $1 is equivalent to [-d $1] (note the spaces on both sides of the brackets) 7: The combination of multiple conditions in a flow control statement:-a logic and. if and only when both conditions are true, the result is true-o logic or. if either of the conditions is true, the result is true. For example, elif [-c $ file_name-o-B $ file_name] then (note the space in the Test statement) is an actual example: #/bin/sh if [$ #-ne 2]; then echo "Not enough parameters" exit 0 #0 indicates that the program exits normally. if [$1-eq $2]; then echo "$1 equals $2" # note the difference between double quotation marks and single quotation marks. elif [$1-lt $2]; then echo "$1 littler than $2" elif [$1-gt $2]; then echo "$1 greater than $2" fi .... done statement example: (remove an online user )#! /Bin/sh # the script to kill logined user username = "$1"/bin/ps aux |/bin/grep $ username |/bin/awk '{print $2} '>/tmp. pid killid = 'cat/tmp. pid 'for PID in $ killid do/bin/kill-9 $ PID 2>/dev/null done
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.