One of the basics of shell programming

Source: Internet
Author: User
Tags arithmetic define local

Outline

First, the programming language classification

Second, Shell classification

Iii. Variables Related concepts

Iv. Types of Bash variables

Five, logical operation

Six, arithmetic operations

Seven, bash condition test type and test method

Viii. the difference between the execution result of the command and the return value of the execution state




First, the programming language classification

    • Distance from people: machine language, assembly language, high-level languages

    • Whether pre-conversion is required for execution: compiled language, interpreted language

    • Programming Style Classification: Object oriented, process oriented

    • Whether variables need to be pre-declared: Strong-typed, weakly-typed languages


II. shell classification and bash concepts

    • Gui:gnome, KDE, XFCE

    • Cli:sh, bash, csh, Ksh, tcsh, zsh


Iii. Variables Related concepts

    • Variable definition: A named memory space

    • Assignment procedure definition: The process of saving data to this memory space is called assignment

    • Declaring the role of a variable type: Defining the storage format, storage size, and supported operations of the variable

    • Variable type: Numeric, character, Boolean, date-time


Iv. Types of Bash variables

1. Local variable: Valid only for the current shell process, not for its child shell and other shells

[[email protected] test]# a= ' Jerry ' # define a local variable a

2. Local variables: Only valid for local code (and can only be defined in functions)

[[email protected] test]# local b= ' Tom ' # command line cannot define local variables-bash:local:can only is used in a function

3. Environment variables: Valid for Shell processes and their child shells

[[email protected] test]# export c= ' natash ' # define environment Variables C[[email protected] test]# echo $Cnatash [[email protected] test]# bash # Reopen a sub Shell[[email protected] test]# echo $C # Previously defined environment variables are still valid in the child shell, but re-landing is not effective natash

4. Position variable $[1-10]

# # Indicates the first parameter passed to the script, # # represents the second argument passed to the script

5. Special variables

$: script name itself $#: The number of arguments passed to the script $?: The execution status return value of the previous command, 0 for success $$: The ID number of the current shell process that the script is running $!: The process ID number of the last process running in the background $*: Displays all parameters passed to the script in a single string. Unlike positional variables, this option parameter can exceed 9 [email protected]: Same as $*, but quoted when used and returns each parameter in quotation marks


Five, logical operation

1. Logic and:&&

[[email protected] scripts]# ID user2 &>/dev/null && userdel-r user2 && echo "Delete user2 finish ed. "  Delete User2 finished. # The front is true, the back is executed, the front is false, the back is not executed; Just ...

2. Logic or: | |

[[email protected] scripts]# ID user2 &>/dev/null | | echo "User2 NOT exists."        User2 NOT exists. # The front is true, the back is not executed, the front is false, the back is executed; Otherwise...

3. Logical non-:!

[[email protected] scripts]#! ID user2 &>/dev/null && echo "User2 not exists."        User2 NOT exists. # The front is true, the reverse is false, the front is false, the reverse is true; Just ...


Six, arithmetic operations

1.let Arithmetic operation expressions

[[email protected] scripts]# a=3[[email protected] scripts]# b=5[[email protected] scripts]# let c= $A + $B; Echo $C 8

2.$[arithmetic operation expression]

[[email protected] scripts]# a=3[[email protected] scripts]# b=5[[email protected] scripts]# d=$[$A + $B];echo $D 8

3.$ ((arithmetic expression))

[[email protected] scripts]# a=3[[email protected] scripts]# b=5[[email protected] scripts]# e=$ (($A + $B)); Echo $E 8

4. ' Expr arithmetic expression ', with a space between the operands and operators in the expression, and to use the command reference

[[email protected] scripts]# a=3[[email protected] scripts]# b=5[[email protected] scripts]# f= ' expr $A + $B '; Echo $F 8


Seven, bash condition test type and test method

1. Integer Test   

-GT: Test is greater than, greater than true, the reverse is false-lt: test is less than, less than true, and vice versa is false-ge: test is greater than equals, greater than equals to true, and vice versa is false-le: whether the test is less than equals, less than equals to true, and vice versa is false-eq: test is equal to true, The reverse is false-ne: test whether unequal, unequal to true, and vice versa is False

2. File Testing

-e File: The test file exists, the existence is true, otherwise false-F file: Test files are normal files, the file is true, otherwise false-D file: Test the specified path is a directory, is the directory is true, otherwise false-R file: Test the current user on the specified files is No Read permission, readable true, otherwise false-W: test whether the current user has write permission to the specified file, can be written as true, otherwise false-X: Test whether the current user has execute permission on the specified file; executable is true, otherwise false

3. Character test

= =: Test for equality, equality is true, not equal to false! =: test whether unequal, unequal to true, etc as false-N string: Test whether the specified string is not empty, not empty is true, NULL is false-Z string: Test whether the specified string is empty, NULL is true, and not empty is false

4. Test method

(1). [Expression]

[[Email protected] scripts]# [3-GT 2] && echo "3>2" 3>2

(2). [[Expression]]

[[Email protected] scripts]# [[3-GT 2]] && echo "3>2" 3>2

(3). Test expression

[Email protected] scripts]# Test 3-GT 2 && echo "3>2" 3>2

5. Combination test conditions

(1). Logic and

[[Email protected] scripts]# [3-GT 2-a 3-lt 5] && echo "2<3<5" 2<3<5

(2). Logical OR

[[Email protected] scripts]# [3-GT 2-o 3-gt 5] && echo "2<3<5" 2<3<5

(3). Logical Non-

[[Email protected] scripts]# [! 3-GT 5] && echo "3<5" 3<5


Viii. execution result of command and return value of execution status

[Email protected] scripts]# Ls/home/user4 User8 # This is the execution result of the command [[email protected] scripts]# echo $?0 # This is the execution status return value of the command, 0 for success, 1-255 for unsuccessful


This article is from the "Hello,linux" blog, make sure to keep this source http://soysauce93.blog.51cto.com/7589461/1715355

One of the basics of shell programming

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.