Shell Note 7

Source: Internet
Author: User
Tags arithmetic

  1. Variable, which provides a way for users to store, retrieve and manipulate data.

  2. Environment variables, positional variables, predefined special variables, user-defined variables

  3. Environment variables

    (1) Shell environment variables, is a class of shell predefined variables, is used to set the system operating environment variables, there is a unified system naming.

    The values of some environment variables are set by the system, and the values of some environment variables are set by the user.

    (2) HOME: Full path name for the owner directory

    CD $HOME equivalent to CD

    (3) Path: Some directory paths are defined and the paths are separated by colons.

    When executing a command or shell script, the shell searches for these directories in the order set in the path variable, and the first matching command or shell script found will be executed.

    (4) Term: terminal type.

    (5) PWD: The absolute path to the current working directory.

    (6) PS1: main prompt. Root user's main prompt #, main prompt for normal users $

    (7) PS2: Auxiliary prompt. The default secondary prompt is >

    (8) Shell: Defines the shell's interpreter path.

    (9) Mail: Defines the path to the system mailbox.

    (10) LOGNAME: The user name of the login user.

    (11) UID: UID of the current user.

  4. Positional variables

    When the shell interprets a command, the location variable is associated with each parameter in the command line, in turn:

    $: Corresponds to the currently executing command name

    $: corresponding to the 1th position parameter

    $: Corresponds to the 2nd position parameter

    ...

    $9: corresponding to the 9th position parameter

    When there are more than 9 arguments on the command line, you need to move the position parameter with the shift command, each shift command is executed, the variable 0 is unchanged, and the 1 start position parameter shifts one bit to the left.

  5. Pre-defined special variables


    Variable Description
    $# Number of actual positional parameters (excluding shell script names)
    $* A string that consists of all positional arguments in the command line
    $! The process number corresponding to the previous background command
    $? Represents the exit status (return value) of the most recent command after execution, in decimal numbers. Executes the command successfully, generally returns 0.
    $$ Current process number PID
  6. User-defined variables

    Users can customize variables as needed. Variable names are case-sensitive.

    A variable name, preceded by a letter or underscore, followed by any number of letters, numbers, and underscores.

  7. Creating variables

    Declare and typeset are used to create variables. With options, you can also set how variables are created.

    For example, Declare-r can create read-only variables. Read-only variables cannot be revoked. Executes the declare command without any options, listing the variables for all settings.

    Create variables by variable name = variable value.


  8. Use {} to differentiate between variables and other characters, for example, ${order}th,${order} is the variable

  9. unset, deleting variables

  10. Assigning values to variables

    (1) Direct assignment

    Name=tom

    (2) using the Read command to assign a value

    Read variable 1 variable 2 ...

    Separate multiple data or variables with spaces

    If the number of variables is equal to the number of data, the corresponding assignment

    If the number of variables is greater than the number of data, then the variable with no input data takes null value

    If the number of variables is less than the number of data, the excess data is assigned to the last variable

    (3) command line parameter assignment

    echo "You are $ $"

    (4) The output value of the command is assigned

    Currdir= ' pwd '

    (5) file read-in Data implementation assignment

    #! /bin/bash

    #filename: ReadFile

    LS *.c > execfile

    While Read line

    Do

    chmod +x $LINE

    Done<execfile

  11. printf and Echo Output

    # printf "sdgafgf%d\n" 3

    Sdgafgf3

    #echo $name 1 [$name 2 ...]

  12. Array

    Declares an array and assigns a value to it

    #student = (element1 element2 ...)

    #i =0

    #echo ${student[$i]}

  13. Copying of arrays

    The array also has two special indexes * and @, which function to extract the entire array elements, but when they are used with double quotes, they are slightly different. The meaning of the symbol @ is to copy the contents of the original array into a new array, resulting in the same new array as the original, but the symbol * is to copy all the elements from the original array into the new array, and the resulting new array has only one element.

    #all = ("${name[*]}")

    #list = ("${name[@]}")

  14. Number of array elements

    ${# name[*]}

  15. The length of an array element

    ${# Name[num]}

  16. The bourned shell does not have built-in arithmetic operations and cannot add, subtract, multiply, or divide directly.

  17. Integer operations using expr or let commands

  18. Floating-point arithmetic, using awk or BC

  19. Expr command

    The expr command, which is an expression-handling command. Operators used: + 、-、 \*,/,%. Operators need to leave blank space before and after the operator, and can only be integer operations.

    # echo ' Expr 3 \* 2 '

    6

    #a = 5; B=3

    #echo ' expr $a + $b '

    8

    #

  20. Let command

    The Let command can be used interchangeably with expr. The Let statement does not need to add the dollar sign before the variable, but a single or space-delimited expression must be enclosed in double quotation marks.

    # a=5; B=3

    # Let A=a+b

    # echo A

    A

    # echo $a

    8

    #

  21. BC Command

    # n= ' echo ' s=2; 10/3 "| BC '

    # echo $n

    3

    # n= ' echo ' scale=2; 10/3 "| BC '

    # echo $n

    3.33

    As shown above, scale cannot be changed to another name, which represents the number of decimal places of the 10/3 result;

  22. awk command

    Format: n= ' awk ' BEGIN {} '

    # n= ' awk ' BEGIN {a=3.3; b=1.1; printf "a*b=%.2f, a/b=%.3f" A*b, A/b} '

    Awk:cmd. Line:1: Fatal:not enough arguments to satisfy format string

    ' a*b=%.2f, a/b=%.3f3.63 '

    ^ ran out for this one

    # n= ' awk ' BEGIN {a=3.3; b=1.1; printf "a*b=%.2f, a/b=%.3f", A*b, A/b} '

    # echo $n

    a*b=3.63, a/b=3.000

    Note that after the "" behind printf, you need to have


Shell Note 7

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.