Linux shell scripts

Source: Internet
Author: User
Tags aliases clear screen echo display

I. What is a script (scripts)

A command file that installs a logical relationship record, in which the file name can be used to initiate the execution of a command within a script, and the shell script is an explanatory language that requires an interpreter shell for the actions recorded in the file.

BASH = GNU Bourne-again Shell,bash is a project developed and promoted by the GNU organization. Bash script is similar to batch processing, simple is to put a lot of instructions together, and provide the loop, condition, judgment and other important functions, syntax is simple and practical, to write programs, greatly simplifies the operation of the administrator, and can complete the functions that the graphics tool cannot achieve.


Two. How to create a shell script

1.vim test.sh

In general, the end of the script is ". Sh", which is not specified by the system, but is an industry specification


2.#! /bin/bash

Script header, this is the interpreter used by the script, also is the default script to run when the child shell opened


3. Script content is composed of logical relationships executed with commands and commands


4. Make the file executable (using chmod +x scripts)


Three. Scripting specifications

1. Scripts should be added to the script description information

Author:an Name

Email: [Email protected] Contact Us

create_date:2017-08-21 Edit Date

vesion:1.0 version

Description:test Scripts Description


Edit Script Description configuration file

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9F/0C/wKioL1ma39HAl3W2AAASoukYD_Y828.png "style=" float : none; "title=" screenshot from 2017-08-21 09-58-13.png "alt=" Wkiol1ma39hal3w2aaasoukyd_y828.png "/>

At the bottom of the VIMRC file, add the following:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9F/0C/wKioL1ma39OSKRIaAAD5wjiBiHI739.png "style=" float : none; "title=" screenshot from 2017-08-21 10-06-54.png "alt=" Wkiol1ma39oskriaaad5wjibihi739.png "/>


After editing complete, open vim and press

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9F/0C/wKioL1ma39OB1ejBAABLVP9OJ_0564.png "style=" float : none; "title=" screenshot from 2017-08-21 10-07-16.png "alt=" Wkiol1ma39ob1ejbaablvp9oj_0564.png "/>



2. Try not to use Chinese in the script, even if using pinyin proxy, Chinese and English system decoding is not the same


3. The script appears in the () | [ ] | < > Wait for a pair of symbols to be typed at once, and the characters in the content with these symbols have spaces


4. The statement in the script to write at once in rich content, you can write the statement structure in the write content


5. The actions used in the statement are indented to write, making the script easy to read


Four. Shell variables


1. The role of variables

A shell variable is used to specify a value for the name later used in the script, and is limited to the shell command line or the script from which the variable is declared.


2. Naming rules for variables

The first letter cannot be a number, and the variable can contain only letters, numbers, and "_ (underscore)"

Example: TEST, Test_redhat, Test_redhat, Testredhat

Can be named according to your preference


3. How variables are set

(1) Environmental level

In the current environment, the current environment is closed and the variables are invalidated.

If you want to take effect permanently, you need to declare export a=1

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9F/0D/wKioL1ma5-XAQKMJAABXecU04dY618.png "title=" Screenshot from 2017-08-21 22-01-52.png "alt=" Wkiol1ma5-xaqkmjaabxecu04dy618.png "/>

(2) User-level

For configured users, other users cannot use the

To be permanently active, you need to modify the configuration file

Vim/root/.bash_profile, adding export a=1

Source/root/.bash_profile Reload configuration file after add is complete

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9F/0D/wKioL1ma6QPDQngBAACJoUrAQlo098.png "title=" Screenshot from 2017-08-21 22-06-30.png "alt=" Wkiol1ma6qpdqngbaacjouraqlo098.png "/>

(3) System level

Settings for aliases

Alias Xie= ' Vim '

Vim ~/.BASHRC

Alias Xie= ' Vim '

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/00/5E/wKiom1ma6Y2A2jdUAAAS9Y5m_do644.png "style=" float : none; "title=" screenshot from 2017-08-21 22-08-50.png "alt=" Wkiom1ma6y2a2jduaaas9y5m_do644.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9F/0D/wKioL1ma6YaBX4G6AAATafHe1Vw557.png "style=" float : none; "title=" screenshot from 2017-08-21 22-08-59.png "alt=" Wkiol1ma6yabx4g6aaatafhe1vw557.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9F/0D/wKioL1ma6YXjnoZdAABjIG5l6Hk766.png "style=" float : none; "title=" screenshot from 2017-08-21 22-08-35.png "alt=" Wkiol1ma6yxjnozdaabjig5l6hk766.png "/>

Deletion of aliases

Delete alias xie= ' vim ' in config./etc/bashrc file

Remove Xie alias Unalias Xie


How variables are defined

User=root

Use positional parameters to read command-line arguments or option inputs passed to the script. Various special variables store the passed option number
The individual parameters of bash parsing or the entire original command line.
Total number of position parameters specified: $#
Position parameters themselves: $, $, $, $ ....
All positional parameters: [email protected], $*

Writing a script test.sh

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/00/5E/wKiom1ma65Twjta3AABfpFwOUeI441.png "style=" float : none; "title=" screenshot from 2017-08-21 22-17-16.png "alt=" Wkiom1ma65twjta3aabfpfwouei441.png "/>

Test

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9F/0D/wKioL1ma642TYJN-AABIIrUgUyg448.png "style=" float : none; "title=" screenshot from 2017-08-21 22-17-30.png "alt=" Wkiol1ma642tyjn-aabiiruguyg448.png "/>



Five. String comparison operators: =,! =


Numeric comparison operators:-eq (equal),-ne (Not Equal),-lt (less than),-le (less or equal),-GT (greater than),-ge (greater or equal)


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9F/0E/wKioL1ma7bqi8pGGAAETxlM0MG8391.png "title=" Screenshot from 2017-08-21 22-26-54.png "alt=" Wkiol1ma7bqi8pggaaetxlm0mg8391.png "/>


-A simultaneous-O or

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/00/5E/wKiom1ma7wGDYYOTAADhxY9p3z8479.png "title=" Screenshot from 2017-08-21 22-32-04.png "alt=" Wkiom1ma7wgdyyotaadhxy9p3z8479.png "/>



Six. File status operator: Test-{b|c|e|f|d|r|w|x|s| L} file/directory

-B Block Device

-C character

-E exists

-F exist and regular

-D Directory

-R Regular

-L Link

-N Variable Value

-X X. Permissions

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/00/5E/wKiom1ma8IHyIGgBAABB65o4xC0920.png "style=" float : none; "title=" screenshot from 2017-08-21 22-36-21.png "alt=" Wkiom1ma8ihyiggbaabb65o4xc0920.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9F/0E/wKioL1ma8HvDu9z-AABHvrEoRoE413.png "style=" float : none; "title=" screenshot from 2017-08-21 22-36-51.png "alt=" Wkiol1ma8hvdu9z-aabhvreoroe413.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/00/5E/wKiom1ma8ILhZcerAABJGrClgnk665.png "style=" float : none; "title=" screenshot from 2017-08-21 22-37-54.png "alt=" Wkiom1ma8ilhzceraabjgrclgnk665.png "/>



Seven. Echo display in shell script with color display, echo display with color

You need to use the parameter-E

The format is as follows:

Echo-e "\033[word background color; text color m string \033[0m"

For example:

Echo-e "\033[41;36m please Input a ipaddress \033[0m"

Where 41 of the position represents the background, 36 of the position is the color of the word

Note:

1, the word background color and the text color is in English ""

2. There is a m behind the text color

3, before and after the string can have no space, if any, the output is also a space

Here is the corresponding word and background color, you can try to find out the different color collocation

Cases

Echo-e "\033[31m Red word \033[0m"

Echo-e "\033[34m Yellow word \033[0m"

Echo-e "\033[41;33m Red bottom yellow word \033[0m"

Echo-e "\033[41;37m Red bottom White \033[0m"

Word Color: 30-–37

Echo-e "\033[30m Black character \033[0m"

Echo-e "\033[31m Red word \033[0m"

Echo-e "\033[32m Green word \033[0m"

Echo-e "\033[33m Yellow word \033[0m"

Echo-e "\033[34m Blue word \033[0m"

Echo-e "\033[35m Purple word \033[0m"

Echo-e "\033[36m sky blue Word \033[0m"

Echo-e "\033[37m White character \033[0m"

Word background color range: 40-–47

Echo-e "\033[40;37m Black Bottom White \033[0m"

Echo-e "\033[41;37m Red bottom White \033[0m"

Echo-e "\033[42;37m Green bottom White \033[0m"

Echo-e "\033[43;37m Yellow \033[0m"

Echo-e "\033[44;37m Blue bottom White \033[0m"

Echo-e "\033[45;37m Purple \033[0m"

Echo-e "\033[46;37m sky blue Bottom White \033[0m"

Echo-e "\033[47;30m black character \033[0m on white background"

Last Face Control option description

\33[0m Close All Properties

\33[1m Setting High brightness

\33[4m Underline

\33[5m Flashing

\33[7M Reverse Display

\33[8m blanking

\33[30m-\33[37m Setting the foreground color

\33[40m-\33[47m Setting the background color

\33[na the cursor to move n rows

\33[NB cursor down n rows

\33[NC cursor right shifts n rows

\33[nd cursor left n rows

\33[Y;XH Setting the cursor position

\33[2J Clear Screen

\33[k clears the contents from the cursor to the end of the line

\33[s Save Cursor position

\33[u Restore cursor Position

\33[?25l Hide Cursor

\33[?25h Display cursor

Cases:

Turn the font into red and keep flashing
#echo-E "\033[31m \033[05m please Input a ipaddress \033[0m"

Word background color range: 40-49

40: Black
41: Crimson
42: Green
43: Yellow
44: Blue
45: Purple
46: Dark Green
47: White

Word Color: 30-39

30: Black
31: Red
32: Green
33: Yellow
34: Blue
35: Purple
36: Dark Green
37: White









Linux shell scripts

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.