2016-2-10 Linux Basic Learning 12

Source: Internet
Author: User

Shell Programming
Compilers, interpreters
Static language: Compiler language (strongly typed variable), converted in advance to executable format, and even initialization required
C, C + +, JAVA, C #
Dynamic languages: Interpreted languages (weakly typed variables), side interpretations for execution, and even non-distinguishing types
PHP, SHELL, Python, Perl

Process oriented: Shell, C
Object-oriented: JAVA, Python, Perl, C + +

With: As long as there is a false, the result must be false
Or: As long as one is true, the result must be true

bash Variable type:
Environment variables
Local variables
Positional variables
Special variables

Local variables:
Varname=value: scope for the entire bash process
Local variables:
Local Varname=value: Scope is the current code snippet
Environment variable: scope is the current shell process and its child processes:
Export Varname=value

Varname=value
Export VARNAME
Export

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7A/D4/wKiom1a7WrSwZcDAAAD19mYt5YQ128.png "style=" width : 600px;height:337px; "title=" Qq20160209165840.png "height=" 337 "hspace=" 0 "border=" 0 "vspace=" 0 "width=" "alt=" Wkiom1a7wrswzcdaaad19myt5yq128.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/D4/wKioL1a7Ww7S8XRlAAFWnnUlxgs102.png "style=" float: none; "title=" Qq20160209171135.png "alt=" Wkiol1a7ww7s8xrlaafwnnulxgs102.png "/>

Positional variables:
$, $, ...
Special variables:
$? : The execution status return value of the previous command
Program execution, there are two types of return values
Program execution Results
Program-to-state return code (0-255)
0: Correct execution
1-255: Error execution 1,2,127 system reservation
Undo Variable:
Unset VARNAME

Variable name requirements
1. Can only contain letters, numbers, underscores, and cannot start with a number
2. Should not be the same as the existing environment variables in the system
3, finally do see the name of the idea

View current shell variables
Set
View environment variables in the current shell
Printenv
Env
Export

/dev/null: Software device bit bucket data black hole

The script starts a child shell process when it executes
Footsteps initiated in the command line inherit the current shell environment variable
The system self-initiated footsteps (non-command line start) need to define the environment variables

Script: Command stack, according to actual needs, combined with command flow control mechanism to realize the source program
Shebang: Magic number
First line of the script:#! Bin/bash
#注释行, do not perform

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/D4/wKiom1a7Wo2Dd_60AABAzKdu4WE272.png "style=" float: none; "title=" Script 1-4.png "alt=" Wkiom1a7wo2dd_60aabazkdu4we272.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/D4/wKioL1a7Wueh_nayAABrg_N__ew863.png "style=" float: none; "title=" Script 1-5.png "alt=" Wkiol1a7wueh_nayaabrg_n__ew863.png "/>

Run the script
Fayi
1, scripting
2, plus permissions (independent execution must be privileged)
3, the current path is added to the environment variable, or is executed under the current path
Law II:
1, scripting
2, interpret the script under the interpreter as a bash parameter

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WnGh18ZyAABLGfcbZEE388.png "style=" float: none; "title=" Script 1.png "alt=" Wkiol1a7wngh18zyaablgfcbzee388.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7A/D4/wKioL1a7WnHDZig3AAF4ALVChFU124.png "style=" float: none; "title=" Script 1-1.png "alt=" Wkiol1a7wnhdzig3aaf4alvchfu124.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/D4/wKioL1a7WnOgCZzJAAF09PhpvIU045.png "style=" float: none; "title=" Script 1-2.png "alt=" Wkiol1a7wnogczzjaaf09phpviu045.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WnSRBDPlAAFEpUthGaM919.png "style=" float: none; "title=" Script 1-3.png "alt=" Wkiol1a7wnsrbdplaafeputhgam919.png "/>


Condition Judgment:
If the user does not exist
Add user, give password and show add success
Otherwise
Do not add

How do I add judgments in bash?
Condition Test Type:
Integer test
Character test
File test

Expressions for Conditional tests:
[Expression]
[[Expression]]
Test expression

Integer comparison:
-EQ: Tests whether two integers are equal, such as $a-eq $B
-ne: Test whether two integers are unequal, not equal to true, otherwise false
-GT: Tests whether a number is greater than another number: greater than true, otherwise false
-LT: Tests whether one number is less than the other: less than true, otherwise false
-ge: greater than or equal to
-le: Less than or equal to

the logical relationship between commands:
Logicwith the: && (as long as one is false, the result must be false)
The first condition is false and the second one does not have to be executed
The first condition is true and a second
Logicor: | | (As long as one is true, the result must be true)
The first condition is true, and the second one does not have to be executed
The first condition is false and a second



Classroom 1, if the number of rows in the/etc/inittab file is greater than 100, a few lines of files are displayed
Wc-l/etc/inittab |

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/D4/wKiom1a7WRGjsgF1AACEaDsWz8A279.png "style=" float: none; "title=" Script 2.png "alt=" Wkiom1a7wrgjsgf1aaceadswz8a279.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/D4/wKioL1a7WWyyjts-AABeiG_dAAQ832.png "style=" float: none; "title=" Script 2-1.png "alt=" Wkiol1a7wwyyjts-aabeig_daaq832.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WWzQttjHAACOWTryVLI431.png "style=" float: none; "title=" Script 2-2.png "alt=" Wkiol1a7wwzqttjhaacowtryvli431.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WWyz-vXXAABJxg9uWA0163.png "style=" float: none; "title=" Script 2-3.png "alt=" Wkiol1a7wwyz-vxxaabjxg9uwa0163.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/D4/wKiom1a7WRLSlWBpAACRx0BhORI064.png "style=" float: none; "title=" Script 2-4.png "alt=" Wkiom1a7wrlslwbpaacrx0bhori064.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7A/D4/wKiom1a7WRPx6yh7AAC9D3kX1-I114.png "style=" float: none; "title=" Script 2-5.png "alt=" Wkiom1a7wrpx6yh7aac9d3kx1-i114.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7A/D4/wKiom1a7WRPRD9MLAABpeSGEu3o463.png "style=" float: none; "title=" Script 2-6.png "alt=" Wkiom1a7wrprd9mlaabpesgeu3o463.png "/>

Classroom 2, if the user exists, it shows that the user already exists, otherwise, add this user
! ID user1 && echo "user1 exists." | | Useradd user1
Classroom 3, if the user does not exist, add, otherwise, show that it already exists
! ID user1 && Useradd user1 | | echo "User1 is"

Classroom 4, 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 is"

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/D4/wKiom1a7WLuCXXj3AACqKqc2eH8990.png "style=" float: none; "title=" Script 3.png "alt=" Wkiom1a7wlucxxj3aacqkqc2eh8990.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/D4/wKiom1a7WLvCdq9ZAABxBHIyBSw595.png "style=" float: none; "title=" Script 3-1.png "alt=" Wkiom1a7wlvcdq9zaabxbhiybsw595.png "/>

5-3 Practice
1. Write a script to complete the following tasks:
(1) Use a variable to save a user name
(2) Delete the user from this variable and delete its home directory
(3) Display "User Delete complete" class information

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7A/D3/wKioL1a7WB_jeg_YAABm14pQPrI272.png "style=" float: none; "Title=" 5-2 exercise 1.png "alt=" Wkiol1a7wb_jeg_yaabm14pqpri272.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/D3/wKioL1a7WB-hXNq4AAA5vCYCqqU402.png "style=" float: none; "title=" 5-2 Practice 1-1.png "alt=" Wkiol1a7wb-hxnq4aaa5vcycqqu402.png "/>

2. Write a script to complete the following requirements:
(1) Add 3 user User1,uuser2,user3, but first determine whether the user exists, does not exist and then add
(2) After the addition, the display added a total of several users, of course, can not include pre-existing and not add
(3) Finally shows how many users are on the current system

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/D3/wKioL1a7WDTSPP4RAADRi6LsHNA179.png "style=" width : 650px;height:161px; "title=" 5-2 exercise 2.png "height=" 161 "hspace=" 0 "border=" 0 "vspace=" 0 "width=" 650 "alt=" Wkiol1a7wdtspp4raadri6lshna179.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WDSjhAH1AABZsnKGjWk586.png "style=" float: none; "title=" 5-2 Practice 2-1.png "alt=" Wkiol1a7wdsjhah1aabzsnkgjwk586.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/D4/wKioL1a7WDTRf4UjAABx3kscsuI080.png "style=" width : 550px;height:141px; "title=" 5-2 exercise 3.png "height=" 141 "hspace=" 0 "border=" 0 "vspace=" 0 "width=" 550 "alt=" Wkiol1a7wdtrf4ujaabx3kscsui080.png "/>

3. Write a script to complete the following requirements:
(1) Given a total user, if its uid is 0, this is displayed as an administrator
(2) Otherwise, it will be displayed as a normal user

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/D4/wKioL1a7WF3hMFyuAABx3kscsuI544.png "style=" float: none; "Title=" 5-2 exercise 3.png "alt=" Wkiol1a7wf3hmfyuaabx3kscsui544.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WF2AJmYjAACLBy8Y-E0629.png "style=" float: none; "title=" 5-2 Practice 3-1.png "alt=" Wkiol1a7wf2ajmyjaaclby8y-e0629.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/D4/wKiom1a7WAOQ-vf-AABLoHXun7E913.png "style=" float: none; "title=" 5-2 Practice 3-2.png "alt=" Wkiom1a7waoq-vf-aablohxun7e913.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/D4/wKioL1a7WF2ALeWlAAA7NeE7MLQ876.png "style=" float: none; "title=" 5-2 Practice 3-3.png "alt=" Wkiol1a7wf2alewlaaa7nee7mlq876.png "/>


conditional judgment, control structure:
Single Branch if statement
If judging condition, then
Statement1
Statement2
...
Fi
If statements for dual branches
If judging condition, then
Statement1
Statement2
...
Else
Statement4
Statement5
...
Fi

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/D3/wKioL1a7V8qC7V5OAACVvY7oOk4755.png "style=" width : 550px;height:200px; "title=" Condition if1.png "height=" "hspace=" 0 "border=" 0 "vspace=" 0 "width=" 550 "alt=" Wkiol1a7v8qc7v5oaacvvy7ook4755.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/D4/wKiom1a7V3CBsJ0qAABHM86oNa0161.png "style=" float: none; "title=" Condition if1-1.png "alt=" Wkiom1a7v3cbsj0qaabhm86ona0161.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/D3/wKioL1a7V8qQjj0zAACXcdwLois793.png "style=" width : 550px;height:175px; "title=" Condition If1-3.png "height=" 175 "hspace=" 0 "border=" 0 "vspace=" 0 "width=" 550 "alt=" Wkiol1a7v8qqjj0zaacxcdwlois793.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/D3/wKioL1a7V8uC0zOjAABmG032Ru8463.png "style=" float: none; "title=" Condition if1-4.png "alt=" Wkiol1a7v8uc0zojaabmg032ru8463.png "/>


This article is from the "11160930" blog, please be sure to keep this source http://11170930.blog.51cto.com/11160930/1741521

2016-2-10 Linux Basic Learning 12

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.