Variables and shell programming

Source: Internet
Author: User
Tags stdin

Programming Ability:

Script programming

Programming languages: Machine language, assembly language, advanced languages

Advanced Language:

Static language: Compiled language

No additional binaries are required, and a compiler is required to directly convert to binary run.

Strongly typed (variable): A variable must be declared beforehand and even initialized before it can be used.

Key words:

Convert into executable format in advance

C, C + +, Java, C #

Dynamic language: Interpreted language on the fly

Weakly typed languages: variables are declared at any time, not differentiated by type

Edge Interpretation Side Execution

ASP ASP. PHP Shell Python perl

Process oriented: Shell, C,perl,

Area object: java,python,perl,c++

Variable type: Directly determines the stored format of the variable, used to determine the storage format and length of the data

Character

Numerical

Period

Fake type

Logical operations: With (multiply), or, non-, XOR, or

1: True

0: Fake

1&0=0

0&1=0

0&0=0

1&1=1

Or: Only one by one is true, and false is False

Non -:! true = False! False = True

XOR: The operand of the same person is false, the difference is true, the same person is zero, no person is 1

With: As long as there is a false, the result is false

Or: As long as there is a true, the result is true

Plastic

Floating-point 11.23 1.123*10^1 | 0.1123*10^2

Variables: Memory space, named memory space,

Variable name:

1. Can only contain letters, numbers, underscores, and cannot begin with a number.

2, should not be the same as the existing environment variables in the system.

3, it is best to see the name of righteousness

Memory: Addressable storage unit

Process:

Bash Variable type:

Reference variable: ${varname}, default {} can be omitted, for example:

Animal=pig

echo "There is some ${animal}s."

1. Environment variables:

Scope is the current shell process and its child processes

Exprot varname=value "Export"

A child shell process is started when the script is used at execution time

command-line-initiated scripts inherit the shell environment variables at that time;

Scripts that are automatically executed by the system (not command-line startup) require a self-defined environment variable

2. Local variables (local variables)

BASH: Variables for a process

Varname=value, local variables, scopes are useful for the entire bash process;

Local varname=value, locals, scope is the current code snippet

3. Position variable

$1,$1,$2 ...

4. Special variables (bash built-in, also known as system variables)

In order to save special data, for example: $?: The execution status return value of the previous command;

Program execution Results

Program Status return codes (0-255)

0: Correct execution

1-255: Error execution, 1,2,127 system reserved:

Undo Variable:

Unset VARNAME

To view variables in the current shell:

Set (including environment variables and local variables)

To view environment variables in the current shell:

Printenv

Env

Export

A variable corresponds to multiple values:

Animal=pig:dog:goat or: animal= $ANIMAL:p IG

Output redirection:

>

>>

2>

2>>

&> simultaneous redirection

/dev/null

ID Student &>/dev/null

Script: Command stack, according to the actual needs, combined with the command flow control mechanism to implement the source program

#!/bin/bash

#注释行, do not perform

How do I make conditional judgments in bash?

Condition Test Type:

1. Integer test

2. Character test

3. File test

Expressions for Conditional tests:

[Expression]

[[Expression]]

Test expression

Integer comparison:

Requires two operands

-EQ: Tests whether two integers are equal, such as: $A-eq $B

[$A-eq $B]

-ne: Tests whether two integers are unequal, unequal to true, and equal to False

-GT: Tests whether a number is greater than another number, is greater than true, or false

-LT: Test whether a number is less than another number, less than true, otherwise false

-ge: Greater Than or

-le: Less than or equal to

The logical relationship between commands:

Logic with:&& ID user1 &>/dev/null && echo "Hello user1"

When the first condition is false, the second condition is not judged and the final result is already there.

When the first condition is true, the second condition must be judged

Logical OR: | |

!id user6 && useradd User6 Front for false do not execute, for true execution

ID User6 | | Useradd User6 If the front is true and not executed, for false execution

If the user exists, it shows that the user already exists, otherwise the user is added:

ID user1 && echo "use1 exists" | | Useradd user1

If the user does not exist, add it or show that it already exists

! ID user1 && Useradd user1 | | echo "User1 exists"

If the user does not exist, add and give the password, otherwise, show that it already exists;

! ID user1 && useradd user1 && echo "user1" | passwd--stdin User1 | | echo "User1 exists"

Cases:

#!/bin/bash

!id user1 &>/dev/null && useradd user1 && echo "user1" | passwd--stdin user1 &>/dev/null | | echo "" User1 exists. "

!id user2 &>/dev/null && useradd user2 && echo "User2" | passwd--stdin user2 &>/dev/null | | echo "" User2 exists. "

!id user3 &>/dev/null && useradd user3 && echo "User3" | passwd--stdin user1 &>/dev/null | | echo "" User3 exists. "

users= ' Wc-l/etc/passwd | Cut-d '-f1

echo "$USERS USERS"


#!/bin/bash

Name=user1

Userid= ' Id-u $name '

[$USERID-eq 0] && echo "This is root" | | echo "This was not root"

Conditional judgment, control structure

Single Branch if statement:

if judgment condition; then

Statement1

Statement2

...

Fi

Cases:

#!/bin/bash

Name=user1

If ID $NAME &>/dev/null;then

Ehco "$NAME exists."

Fi


Two-branch if statement:

if judgment condition; then

Statement1

Statement2

...

Else

Statement1

Statement2

...

Fi

Cases:

#!/bin/bash

Name=user1

If ID $NAME &>/dev/null;then

Ehco "$NAME exists."

Else

echo "$NAME not Exist"

Fi


#!/bin/bash

Name=root

If [' id-u $NAME '-eq 0]; Then

echo "This is Admin"

Else

echo "This was not Admin"

Fi


Variables and 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.