Shell Programming Combat

Source: Internet
Author: User
Tags chmod

Why you should learn shell programming

Shell scripting language is an important tool to implement LINUX/UNIX system management and automation operations, and the core of Linux system and basic application software mostly involve the content of shell script.

A qualified Linux system administrator must be proficient in writing the Shell scripting language to improve the productivity of operations personnel and reduce unnecessary duplication of effort.

The basics you need to learn the shell

Good command of Vim editor, familiar with SSH terminal and other configuration

The most common development of shell scripts under Linux is the Vim editor, which can make shell scripting more effective with VIM!

Have a certain Linux command base, master 80 commands above

Unlike other scripting languages, such as the Python,shell lacks a library of external functions that can be used directly, the commands of the Linux system can serve as a library of shell functions!

Familiar with common Linux service deployment, optimization, analysis and troubleshooting

The direct purpose of learning the shell is to automate the management of the system and services at work, so if you are unfamiliar with service operations, using the shell is meaningless.

What is a shell?

The shell exists at the outermost layer of the operating system, is responsible for direct dialogue with the user, interprets the user's input to the operating system, processes the output of the operating system, and then feeds back to the user.

The shell is a command interpreter whose role is to interpret and execute commands and programs entered by the user, and the Shell interpreter executes one command per input, and there is an interactive way.

From the boot ready to enter the user name and password, to the successful login, all operations are interpreted and executed by the shell.

What is a shell script

This program is called a shell script when a command or program statement is executed not at the command line, but through a program file.

A simple shell script

Clear the Messages log file under/var/log

Put all the commands into a file, stacked up is a script file! Note The executable permissions of the file, and the user identity

/var//dev/null >"Logs cleaned up. done"
Types of Shell scripting languages
The Shell scripting language is a weakly typed language (it is not necessary to define the type of the variable) there are two big shells in Linux, one Bourne Shell and the other C shell
All we need to know is that the standard Bourne Again Shell (bash) replaces
View the shell of the CentOS 7 system

Other scripting languages

Phpperlpython

Language selection

Shell scripting language Advantage lies in the processing system bottom-level business, based on Linux command processing is also the fastest, often language development automation installation, monitoring alarm, software startup scripts.
First shell script with Hello World

Script Start

A canonical script file will indicate in the first line which program executes the contents of the script and must be on the first line!!! If you do not add the default will also be given to bash interpretation, good programming habits, preferably with the opening interpreter language identifier
#!/bin/bash
#!/usr/bin/env python
How to execute a shell script
Bash script name sh    script Name This method is used when the script does not have permission to execute, or when the file header does not specify an interpreter

Path/scriptname

This method requires the script to have executable permissions, chmod +x scriptname

SOURCE ScriptName
. ScriptName
Loading and executing commands and statements in a script file
[[Email protected]_python/tmp One:Ten: ,] #sh test.shchaoge nb[[email Protected]_python/tmp One:Ten: -] #cat test.sh#!/bin/Bashecho'Chaoge NB'[[Email Protected]_python/tmp One:Ten: +]#. Test.shchaoge Nb[[email Protected]_python/tmp One:Ten: -] #source Test.shchaoge NB
#添加可执行权限给test. Sh
chmod +x test.sh
./test.sh
Variable
Environment variables generally refer to the export of built-in command variables to define the shell's operating environment.
The shell uses environment variables to determine the command prompt, which is the PS1 variable in front of the super brother.
Environment variables can be set and created on the command line, but the values of the variables are lost when the user exits the command, so they can be written to the/etc/profile file, and the variables will be initialized every time the user logs in!
environment variable name all uppercase, with ENV view

To set a sign-in prompt

One:#cat/etc/motd'welcome Chaoge bakcome~~~~~~~~~ '

yudanl:~ yuchao$ ssh [email protected]
Last failed login:wed 11:20:20 CST 2018 from 111.231.199.24 on Ssh:notty
There were failed login attempts since the last successful login.
Last login:wed-10:45:51 2018 from 223.72.74.11
' Welcome Chaoge bakcome~~~~~~~~~ '

Defining variables

Note Points for variable definitions

    • There can be no spaces between the variable name and the equals sign
    • The name can only be used in English letters, numbers, underscores, the first character can not be used in numbers
    • You can't use punctuation.
    • Can't use keywords
Name='chaoge'
Echo $name
Echo ${name} #帮助解释器识别变量的边界
#变量重新赋值
Name= ' Chaogenb '

Set read-only variables

one: £ º]#readonly: #name ='  chaogenb3'-bash:name: read-only variable

Delete a variable

unset name

Variable type

    • Local variables are defined in a script or command and are valid only in the current shell
    • Environment variables All programs, shell programs can access environment variables
    • Shell variable special variables have both environment variables and local variables
String

Strings can be in single or double quotes, or without quotation marks

All:[]#addr ='shahe'one:  - ] #echo $addrshahe

Any string of single quotation marks is output as is, and the variable in the single quote string is invalid

Single quote string cannot appear single single quotation mark, escape character is also unavailable, can be used as string concatenation

Double quotes

Variables can be used in double quotes

Double quotes can have escape characters

[Email Protected]_python ~ One: $: on] #addr ='Changpingshahe'[[Email Protected]_python~ One: -: .] #name ='Yuchao'[[Email Protected]_python~ One: the:xx] #message ="hello,${name},your addr is \ "${addr}\""[[Email Protected]_python~ One: -: to] #echo $messageHello, Yuchao,your addr is "Changpingshahe"

Shell Programming Combat

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.