Shell Programming Basics must know

Source: Internet
Author: User

Shell programming


################################################


Static language

Dynamic language


################################################


Programming languages: Machine language, assembly language, advanced languages


################################################


Object-Oriented programming language: SEHLL C

Object-Oriented programming language: JAVA Python perl C + +


################################################


Variables: Memory space, naming

Memory: Addressable storage unit


################################################


Variable type: Defines the storage format of the variable


Character type

Numeric type: integer, floating-point


################################################


Logical Operation:& with | | or! Non-XOR or


################################################


Bash Variable type

Environment variables

Local variables (local variables)

Positional variables

Special variables



Variable name rule

Cannot duplicate a variable that exists

See the name of the idea

################################################


Varname=value the entire bash process is working.

Local Varname=value valid for current code

Environment variables:

Current shell processes and child processes are valid


################################################


Undo Variable: Unset VARNAME


################################################


$? : Return value for program execution completion


o Correct

1-255 error return value 1, 2 127 reserved


[[Email protected] ~]# $?

-bash:127:command not found

[[email protected] ~]# echo $?

127

[Email protected] ~]# echo It is

It is


################################################


Output redirection

>

>>

2>

2>>

&> Errors and correct results


Give the return value to/dev/null


[Email protected] ~]# CD/CDD &>/dev/null

[[email protected] ~]# echo $?

1

[Email protected] ~]# CD/&>/dev/null

[[email protected]/]# echo $?

0



/dec/null

[[email protected] ~]# echo $?

0


commands for viewing environment variables

Printenv

Env

Export



################################################


What a script does: a command stack to accomplish a specific task


Scripting: Creating user1 user2 user3 user4 user5 users


##########

1. Create the. sh file

[Email protected]/]# vim/tmp/useradd.sh


##########

2, writing format

[Email protected]/]#

#!/bin/bash

#

Useradd user1

Useradd User2

Useradd User3

Useradd User4

Useradd User5


##########

3. Execute permissions for script

[Email protected] ~]# chmod +x/tmp/useradd.sh


##########

4, Execute script

[Email protected] ~]#/tmp/useradd.sh


################################################


Conditional judgment


How to achieve conditional judgment in bash

Condition test Type

Integer

Text

File


################################################


Conditional test Expression

[Expression]

[[Expression]]

Test expression


Integer comparison: Compare plus brackets

-eq: = equal [$a-eq $b]

-ne:\= Range

-gt:> Greater than

-lt:< less than

-gt:>=

-lt:<=


################################################


The logical key between commands

Logic and:&& the first one is false, the second does not have to be executed

|| The first one is true and the second one is to execute


Create User7, no presence created USER7, presence hint already exists


[Email protected] ~]# Vi./id.sh


#!/bin/bash

#

! ID user7 &>/dev/null && useradd User7 | | Echo ' User7 is exist '

Or: | | The first one is true


################################################


This article is from the "Jameszhan Linux Advanced ops" blog, so be sure to keep this source http://jameszhan.blog.51cto.com/10980469/1880110

Shell Programming Basics must know

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.