Next day of linux Shell Study Notes

Source: Internet
Author: User

Variable

  1. Get local host name

    # Hostname

  2. Obtain the System Version Number

    # Uname-r

  3. Obtain CPU type
  4. Obtain the cpu operation frequency

    Cat/proc/cpuinfo

  5. Obtain memory usage

    # Free-m

  6. Obtain hard disk usage

    # Df

Variable types and variable operations

  1. Local variable (generally referred)

    The local variable only exists in the user's current shell. When the user exits the current shell or opens a new shell, the set variable will not exist.

    Set Variables

    # Variable name = variable value

    Show Variables

    # Echo $ {variable name}

    If a = 1 is displayed, echo $ {}

  2. Environment variables (rarely used)

    Environment variables are used for all user processes (often referred to as subprocesses ). A logon process is called a parent process. User processes executed in Shell are called sub-processes. Unlike local variables, environment variables can be used for all sub-processes, including editors, scripts, and applications.

    Environment Variables disappear when the user logs out, so it is best to define them directly in the $ HOME/. bash_profile (/etc/profile) file.

  3. Location variable (closely related to scripts and functions)

    The position variable indicates $0, $1... ... $9 (only 9)

    $0

    $1

    $2

    $3

    $4

    $5

    $6

    $7

    $8

    $9

    Script Name

    A

    B

    C

    D

    E

    F

    G

    H

    I

    $0 = Current Script Name

    Use location parameters to scripts

    PASS Parameters to system commands

  4. Special Variables

    $ # Number of parameters passed to the script list

    $ * ($ @) Displays all parameters passed to the script in a single string

    $ ID of the current process running the script

    $? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.



Necessity of reference



In variable operations, the most common mistake when the script executes variable assignment is a reference error.

Example: echo abc *

Echo abc '*'

Echo Hit the star button to exit *

Reference Method

Double quotation marks (") (The following must be excluded)

Double quotation marks can be used to reference any character or string except $, ', \, and character.

Single quotes (') (overbearing, any character can be converted)

Similar to double quotation marks, shell ignores any referenced values.

In other words, if the special meaning is blocked, All characters in the quotation marks are treated as a string.


Back quotes (') (execute the brackets as commands)

Used to set the output of system commands to variables.

Shell uses the content in the back quotes as a system command and executes its content


Backslash (\)

If a character has a special meaning, the backslash prevents shell from misunderstanding its meaning, that is, shielding its special meaning.

The following characters have special meanings: & * + ^ $ '"|?



Script instance

  1. Hello. sh

    #! /Bin/bash

    # This is the first script

    Echo "Hello, World !"

    Run:

    Chmod u + x Hello. sh

    ./Hello. sh

  2. Var. sh

    #! /Bin/bash

    Echo $1

    Echo $2

    Echo $3

    Echo $4

    Echo $5

    Echo $6

    Echo $7

    Echo $8

    Echo $9

    Echo $10


  3. #! /Bin/bash

    Echo-e "hostname \ t 'hostname '"

    Echo-e "OScore \ t 'uname-R '"

    Echo-e "CPUInfo \ t 'grep" model name "/proc/cpuinfo | awk-F: '{print $2 }''"

    Ehco-e "CPUMHz \ t' grep" MHz "/proc/cpuinfo | awk-F: '{print $2 }''"

    Echo-e "MEMTotal \ t' free | awk '$1 =" Mem: "{print $2 }''"

    Echo-e "DiskInfo \ t 'df | grep dev | awk '{print $1 $2 }''"


    Review:

    Script structure and running

    Variable type and usage

    Environment Variable

    Local variable

    Parameter variables

    Special Variables

Reference Method for variable assignment

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.