"Learn Linux with older boys Koriyuki Shell Programming"-Chapter I preliminary introduction to shell scripts

Source: Internet
Author: User
Tags python script

This article is in the study of "with the old boy learning Linux Koriyuki Shell Programming Practical" the knowledge points recorded in this book. I have benefited greatly from reading this book, but this is only my personal opinion. Now let's get to the bottom of the shell script.

The shell itself is a command interpreter whose function is to interpret the commands and procedures for executing user input.

The type of Shell scripting language: The main shell in SH, ksh, bash, csh, tcsh,linux is bash, so this article and subsequent shell scripts are based on bash.

So how do we look at the default shell in a Linux system?

[[email protected] ~]# cat/etc/redhat-release ==> View Linux system version
CentOS Release 6.8 (Final)
[[email protected] ~]# echo $SHELL ==> View system default Shel
/bin/bash

Script Writing Specification:

1. First line at the beginning of the script

A canonical shell script indicates by which program (interpreter) to execute the contents of the script in the first row.

In the programming of Linux bash, it is generally:

#!/bin/bash

Or

#!/bin/sh

Tips:

SH is a soft connection to bash, and in most cases, there is no difference between the beginning of the script using "#!/bin/bash" and "#!/bin/sh", but a more canonical notation is to use "#!/bin/bash" at the beginning of the script.

Since the older version of Bash has been exposed to a more serious security vulnerability, we need to detect if there is a vulnerability in the bash version of the existing system, and how do we detect a vulnerability? Here's how:

[[Email protected] ~]# env x= ' () {;;}; echo Be careful ' bash-c "echo this is a test"
This is a test

If you return the following two lines, you need to upgrade bash as soon as possible, but it doesn't matter if you just use it for learning and testing.

Be careful

This is a test

Tip: Upgrade method: Yum-y Update bash

Tips:

(1) The first line of the script in different languages differs, as follows:

#!/bin/sh

#!/bin/bash

#!/usr/bin/awk

#!/bin/sed

#!/usr/bin/tcl

#!/usr/bin/expect ==>expect solve interactive language opening interpreter

#!/usr/bin/perl ==>perl language Interpreter

#!/usr/bin/env Python ==>python language interpreter

Different scripts are typically saved as different extensions, for example:

Shell script: xxx.sh

Python script: xxx.py

Expect script: Xxx.exp

(2) To view the version of bash:

[Email protected] ~]# bash--version

2. Script comments:

In a shell script, a comment that begins with the "#" sign, which is used to annotate the script.

3. Execution of the script:

When the shell script runs, it looks for the system environment variable, env, which specifies the environment variable file (the load order is usually/etc/profile, ~/.bash_profile,

~/.BASHRC,/ETC/BASHRC, and so on), the shell starts executing the contents of the shell script after the environment variables have been loaded.

A shell script is a command and statement that executes each line from top to bottom, from left to right, after executing a command and then executing the next, if a child script is encountered in the shell script, the contents of the child script are executed first, and then the parent script is completed to continue executing the subsequent commands and statements within the parent script.

Tip: When setting up a Linux Crond task, it is best to redefine the system environment variables in a timed task script, otherwise some system environment variables will not be loaded.

How the shell script executes:

(1) Bash script-name or sh script-name: A method that is often used when the script file itself does not have executable permissions, or a method that needs to be used at the beginning of a script file without specifying an interpreter.

(2) Path/script-name or./script-name: Script execution under the current path of the script (provided the script requires EXECUTE permission)

(3) source Script-name or. Script-name: This method is usually used with source or "." Reads in or loads the specified shell script file (such as san.sh). Execute all the statements in the script san.sh in turn. These statements will run in the current parent shell script father.sh process (several other modes will start a new process Execution child shell). Therefore, use source or "." You can pass the return value of the variable value or function in the san.sh script to the current parent shell script father.sh, which is the biggest difference from several other methods.

SOURCE or "." Command function: Executes the source or "." in the current shell. Loads and executes the commands and statements in the associated script file, rather than producing a child shell to execute the commands in the file.

(4) Sh<script-name or cat script-name|sh: Same for Bash, this method uses less.

Conclusion:

Through source or "." The executed script is loaded because the script is executed in the current shell, so after the script ends, the variables (including function) values in the script still exist in the current shell, and the SH and bash execution scripts initiate a new child shell execution, which is returned to the parent shell after execution is complete. Therefore, variables (including function) values cannot be persisted. When developing shell scripts, it is best to use source or "." If there is a requirement for a script to reference or execute the content or configuration file of another script. You can call source or "." After you load the script or configuration file, finish processing, and then load it under the script. Loaded scripts and configuration files in the variables and functions and so on.

Basic specifications and habits of 4.shell scripts:

(1) The first line of the shell script is the specified script interpreter, typically:

#!/bin/bash

Or

#!/bin/

(2) The beginning of shell script will add version, copyright and other information:

#Date: 2017-04-17

#Author: Create by Xiaoyu

#Description: This script function is ...

#Version: 1.1

You can modify the ~/.VIMRC configuration file to configure Vim to automatically add the above information when you edit a file.

(3) Try not to use Chinese in the shell script

In the shell script as far as possible in English comments, prevent garbled problem, if you want to load Chinese, according to its own client to the system character set adjustment, such as: Export lang= "ZH_CN. UTF-8 "and redefine the character set settings in the script, and the system remains consistent.

(4) The name of the shell script should be the extension of. shw.

(5) The shell script should be stored in a fixed path, such as:/server/scripts/

A good habit of writing 5.shell script code:

(1) Pairs of symbols should be written out as much as possible, and then backspace in the symbol to add content to prevent omission.

Pairs of symbols include: {}, [], ', ', ', ', '

(2) The brackets [] must have at least 1 spaces at each end.

(3) For the Process Control statements, you should write the format once, then add the content. Such as:

If statement:

If condition content

Then

Content

Fi

For loop:

For

Do

Content

Done

Hint: while and until, case, and so on statements are also the same.

(4) Make the code easier to read by indenting.

(5) The value of the string definition variable for a regular variable should be double quotation marks, and there must be no spaces before and after the equals sign, a quoted character reference is required, then a single quotation mark, and if it is a reference to a command, a backslash.

(6) The single quotation mark, double quotation mark and anti-apostrophe in the script must be the symbols in the English state.

Above for I read "with the old boy learn Linux Yun Koriyuki Shell programming Combat" This book when the note, if there is any copyright issues, please contact [email protected].

This article is from the "shayatou_1990" blog, make sure to keep this source http://shayatou1990.blog.51cto.com/12806916/1916590

Learn Linux Koriyuki Shell programming with older boys-Chapter I preliminary Introduction to shell scripting

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.