Day09_shell Basic grammar and Process Control

Source: Internet
Author: User

I. Shell's basic syntax-variables
    1. Naming rules
      1. Start with a letter
      2. Connect words with an underscore or an underscore
      3. The same type is distinguished by numbers
      4. For file best plus extension
    2. System variables
      • Set and env differences
      • Set: Show All variables
      • ENV: System variable is environment variable
    3. Assigning values to variables
      • unset VARNAME: Deleting variables
    4. Common system Variables
      • PATH
      • Pwd
      • Lang
      • HOME
      • Histsize: Number of history log lines
      • PS1: Storing user information
      • Ifs
      • Export: Defining global variables
    5. Define the bounds of a variable name
      [email protected] ~]# rest_mem=20
      [email protected] ~]# echo ${rest_mem}%
      20%
    6. Data type
Two. Shell's basic syntax-operator
  1. Arithmetic operators
    • +
    • -
    • *
    • /
    • %
  2. Relational operations
    • <
    • >
    • <=
    • >=
    • ==
    • !=
    • &&
    • ||
      • Above with (())
    • Test command related, [] can achieve the same effect
  3. Assignment operators
    • =
    • +=
    • *=
    • /=
    • %=
    • [email protected] ~]# ((x%=3))
      [email protected] ~]# echo $x
  4. all calculators in the shell
    • $[]
    • (())
    • $ (())
    • expr
    • floating-point arithmetic b C Software
      • yum install bc-y
      • echo ' SCALE=2;1/3 ' |bc-l (reserved two bit, 1 divided by 3 operation)
  5. Test action
    1. When the command executes, it returns to a system variable of $?
      A value of 0 indicates that the command executed successfully or failed.
    2. Testing command test [] [[]] (())
      1. Test file status
        • -D Directory
        • -E
        • -H Link File
        • -S file length > 0, non-empty
        • -F Normal File
        • -W Writable
        • -R Readable
        • -X Executable
        • -L Symbolic connection
        • -u file has suid bit settings
        • -B Block File
      2. String test
        • = Two strings equal
        • ! = two strings are not equal
        • -Z Empty string
        • -N Non-empty string
      3. Test values
        • -eq equals
        • -ne Not equal to
        • -GT Greater than
        • -lt less than
        • -ge greater than or equal to
        • -le less than or equal to
      4. Expand Test Symbols
Three. Process Control
    1. The IF statement for Process Control

      #!/bin/bashvar='/etc/init.d'#var='/dev/sda'if-d $var ]    then        "$var is directory"elif-b $var ]    then        "$var is block"elif-f $var ]    then        "$var is regular file"else        'unknow'fi
  • Passing parameters to a script

    #test. Shecho $0echo $1echo $2echo $3Echo ${ One}echo'$$'$ $echo'$*'$*Echo' [email protected] '[Email Protected]echo'$#'$#Echo'$?'$?'''test: Python test.sh 1 2 3 4 5 6 7 8 9Output Result:./test.sh2 One$$ 14312$* 1 2 3 4 5 6 7 8 9[email protected] 1 2 3 4 5 6 7 8 9$#$? 0* $ A file name to run* $$ for process PID* $* for all parameters of the input* [email protected] with $** $# number of input parameters* $? Whether the previous command was executed successfully'''
  • Modify the script so that it can receive the arguments from the caller

    [[Email protected]-R3-Srv~]# cat Test_file.sh#!/bin/bashif[-D $1] then Echo"is directory"elif[-B $1] then Echo"is block"elif[-F $1] then Echo"is regular file"ElseEcho' Unknown 'Fi[[email protected]-R3-Srv~]#./TEST_FILE.SH/ETC/PASSWD/etc/passwd isRegularfile
  • Detects if Nginx is started, and if not, starts it

    #! /bin/bashps aux|grep nginx|-v grepif-1 ]    then        systemctl start nginxfi

Day09_shell Basic grammar and Process Control

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.