Linux Shell Learning notes the next day _ other

Source: Internet
Author: User
Tags memory usage

Variable

    1. Get the native host name

      #hostname

    2. Get the system version number

      #uname-R

    3. Get CPU Type
    4. Get the CPU working frequency

      Cat/proc/cpuinfo

    5. Get Memory usage

      #free-M

    6. Get use of hard disk

      #df

Variable type and variable operation

  1. Local variables (generally referred to)

    The local variable exists only in the user's current shell, and when the user exits a new shell of the current shell or opener, the variable set will not exist

    Setting variables

    #变量名 = variable Value

    Show variables

    #echo ${Variable Name}

    Suppose a=1 shows the Echo ${a}

  2. Environment variables (very few)

    Environment variables are used for all user processes (often referred to as child processes). The logon process is called the parent process. The user processes executed in the shell are called child processes. Unlike local variables, environment variables are available for all child processes, including editors, scripts, and applications

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

  3. Positional variables (very much related to scripts and functions)

    Position variable indicates $0,$1 ... $ (only 9)

    $

    $

    $

    $

    $

    $

    $

    $

    $

    $

    Script Name

    A

    B

    C

    D

    E

    F

    G

    H

    I

    $0= Current script Name

    To use positional parameters in a script

    Passing parameters to system commands

  4. Special variables

    $# the number of argument lists passed to the script

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

    $$ the current process ID number of the script run

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



The need for reference



In a variable operation, the easiest mistake to make when a script executes a variable assignment is to refer to the error

Example: Echo ABC *

Echo ABC ' * '

echo Hit the star button to exit *

Way of referencing

double quotation marks (") (you need to exclude the following several)

Use double quotes to refer to any character or string except $, ', \, character

single quotation mark (') (more overbearing, anything can be converted to characters)

Similar to double quotes, the shell ignores any reference values.

In other words, if the special meaning is masked, all the characters in the quotation marks will be used as a string.


Inverted quotation mark (') (to be executed as a command in parentheses)

Used to set the output of a system command to a variable.

The shell takes the contents of the inverted quotes as a system command and executes its contents


Back slash (\)

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

The following characters contain a special meaning:& * + ^ $ ' "|?



Script instance

  1. hello.sh

    #!/bin/bash

    #This is the

    echo "hello,world!"

    Perform:

    chmod u+x hello.sh

    ./hello.sh

  2. var.sh

    #!/bin/bash

    echo $

    echo $

    echo $

    echo $

    echo $

    echo $

    echo $

    echo $

    echo $

    echo $


  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 $} '"

    Ehco–e "Cpumhz\t ' grep" MHz/proc/cpuinfo | Awk–f: ' {print $} '

    Echo–e "Memtotal\t ' free | awk ' $1= ' Mem: ' {print $} ' '

    Echo–e "Diskinfo\t ' df | grep Dev |awk ' {print $} '


    Review:

    Structure and operation of scripts

    Types and usage of variables

    Environment variables

    Local variables

    Parameter variable

    Special variables

Reference method when assigning a variable value

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.