2016-7-2 Linux Shell Basics

Source: Internet
Author: User
Tags arithmetic stdin

Static language: Compiled language

Strongly typed (variable) converted into executable format beforehand

C C + + Java C #

A variable of a strongly typed language must be declared beforehand, or even initialized, before it is used.


Dynamic language: Interpreted language

Weak type

Edge Interpretation Side Execution

PHP Shell python

Weakly typed languages, when variable declarations are not even differentiated by type



Programming Ability:

Script compilation


Variable type: Determine the storage format and length of the data in advance


1byte=8b 1 (byte) byte = 8 bit (bit)

Character type

Numeric type

Integral type

Floating point Type



Bash Variable type

Environment variables

Local variables (local variables)

Positional variables

Special variables: $?


Local variables:

Name=cary----Defining variables

echo $NAME----Reference variable

Local Varname=value----variables, scope current code snippet


Environment variable: scope is the current shell process and its child processes

Export Varname=value Defining environment variables----exporting keywords using export


The script starts a child SEHLL process when it executes

Scripts that are started on the command line inherit the current shell environment variable;

Scripts that are automatically executed by the system (not command line)


In the shell language, ' single quotation marks do not replace variables, and what is in the output.

"" Double quotes do variable substitution, there are variables to do variable substitution output


Output redirection

>---overlay append

>>---append does not overwrite


/dev/null--Data black hole



Undo Variable: Unset varname


To view variables in the current shell: set


To view environment variables in the current shell: printenv or env or export


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


Script format: The script must start with a magic number

#! /bin/bash

The other lines begin with # to denote comments


Execution of the script: the execution of the script must have the Execute permission of x, and it needs to indicate the path


Exercise: Write a script

1. Add 5 users, User1,.... User5

2. Each user's password is the same as the user name, and requires that the execution result information of the passwd command not be displayed after the Add password is completed

3. After each user creation is completed, the Add account creation success is displayed

#!/bin/bash

Useradd user1

echo "user1" |passwd--stdin user1 >/dev/null

echo "Add user1 successfully"



Condition Judgment:

If the user exists

Add user, give password and show add success

Otherwise

Display already exists, no add

How to achieve conditional judgment in bash

Condition Test Type:

Integer test

Character test

File test

Expressions for Conditional tests:

[Expression]

Test expression


Integer comparison:

-EQ: Test for equality of two integers: $a-eq $B

-ne: Test for two integers: unequal to true, equal to False

-GT: Test whether a number is greater than another number: greater than, true, otherwise, false

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

-ge: greater than or equal to

-le: Less than or equal to


Logical relationship of the command:

Logic and &&: When the first condition is false, the second condition is not judged and the end result is already

If the first condition is true, the second condition also needs to be judged

Logical OR | | : The second condition does not execute when the first condition is true

If the first condition is false, you also need to perform a second condition

#!/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 user3 &>/dev/null | | echo "User3 exists"

Users= ' wc-l/etc/passwd |cut-d:-fl '

echo "$USERS USERS"


Conditional judgment, control structure:

Single-branch if statement

if judgment condition; Then

Statement1

Statement2

....

Fi


eg

#!/bin/bash

Name=user1

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

echo "$NAME exsits"

Fi


If statements for dual branches

if judgment condition; Then

Statement1

Statement2

.......

Else

Statement3

Statement4

......

Fi



eg

Name=cary.qin

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

echo "$NAME exists"

Else

Useradd $NAME

echo "$NAME" | passwd--stdin $NAME &>/dev/null

echo "Add finished"

Fi


'----The execution result of the anti-quote command, which provides the contents of the name execution



Numerical operations of the shell

A=3

B=6

1. Let arithmetic op-expression

Let c= $A + $B


2. $[arithmetic operation expression]

c=$[$A + $B]



3.$ ((arithmetic expression))

c=$ (($A + $B))


2016-7-2 Linux Shell Basics

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.