Shell scripting 30 minutes to get started

Source: Internet
Author: User
Tags closing tag php programming posix

What is a shell script sample

Let's look at an example:

#!/bin/shcd ~mkdir Shell_tutcd shell_tutfor (i=0; i<10; i++); Do    Touch Test_$i.txtdone
Example explanation
    • Line 1th: Specify the script interpreter, here is the interpreter with/bin/sh
    • Line 2nd: Switch to the current user's home directory
    • Line 3rd: Create a directory Shell_tut
    • Line 4th: Switch to the Shell_tut directory
    • Line 5th: Cycle conditions, 10 cycles
    • Line 6th: Create a test_1 ... 10.txt file
    • Line 7th: End of loop body

CDs, MKDIR, and touch are all programs that come with the system, typically in the/bin or/usr/bin directory. For, does, done is the keyword of the SH scripting language.

The concept of shell and shell scripting

A shell is an application that provides an interface through which users access the service of the operating system kernel. Ken Thompson's SH is the first UNIX shell,windows Explorer to be a typical graphical interface Shell.

Shell script, a scripting program written for the shell. The shell is often referred to by the industry as a shell script, but reader friends know that Shell and shell script are two different concepts. For the sake of simplicity, the "shell programming" that appears in this article refers to Shell scripting, not the development of the shell itself (such as Windows Explorer extension development).

Environment

Shell programming is like Java and PHP programming, as long as there is a text editor that can write code and a script interpreter that can explain execution.

OS

The current mainstream operating system supports shell programming, the shell programming described in this document refers to the shell under Linux, the basic is the POSIX standard functionality, so it also applies to UNIX and BSD (such as Mac OS).

Linux

The Linux default installation brings the Shell interpreter.

Mac OS

Mac OS not only contains the two most basic interpreters, SH, bash, but also includes Ksh, CSH, zsh and other less frequently used interpreters.

Emulator on Windows

Windows does not have a built-in shell interpreter at the factory and needs to be installed on its own, in order to use grep, awk, curl and other tools, preferably a Cygwin or mingw to simulate the Linux environment.

    • Cygwin
    • MinGW
Script Interpreter SH

The Bourne shell,posix (Portable Operating System Interface) Standard shell interpreter, its binary file path is usually/bin/sh, developed by Bell Labs.

This article is about SH, if you use other languages as Shell programming, please refer to the documentation in the appropriate language.

Bash

Bash is a Bourne shell alternative, GNU Project, and the binary file path is usually/bin/bash. The industry often mixes bash, sh, and Shell, as you'll often see in the copywriting of an OPS engineer: familiar with Linux bash programming and proficient in shell programming.

In CentOS,/bin/sh is a symbolic link that points to/bin/bash:

[Email protected] ~]# ls-l/bin/*sh-rwxr-xr-x. 1 root root 903272 Feb 05:09/bin/bash-rwxr-xr-x. 1 root root 106216 Oct  2012/bin/dashlrwxrwxrwx. 1 root root      4 Mar-10:22/bin/sh-bash

But not on Mac OS,/bin/sh and/bin/bash are two different files, although their size is only about 100 bytes apart:

imac:~ wuxiao$ ls-l/bin/*sh-r-xr-xr-x  1 root  wheel  1371648  6 Nov 16:52/bin/bash-rwxr-xr-x  2 root  Wheel   772992  6 Nov 16:52/bin/csh-r-xr-xr-x  1 root  wheel  2180736  6 Nov 16:52/bin/ Ksh-r-xr-xr-x  1 root  wheel  1371712  6 Nov 16:52/bin/sh-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52/bin/tcsh-rwxr-xr-x  1 root  wheel  1103984  6 Nov 16:52/bin/zsh
Advanced programming languages

In theory, as long as a language provides an interpreter (not just a compiler), the language is capable of scripting, and common explanatory languages can be used for scripting, such as Perl, TCL, Python, PHP, and Ruby. Perl is the oldest scripting language, and Python has become a preset interpreter for Linux distributions over the years.

Compiled languages, as long as there is an interpreter, can also be used as scripting, such as C shell is built-in (/BIN/CSH), Java has a third-party interpreter Jshell,ada has a charge interpreter adascript.

Here is a php Shell script example (assuming the file is named test.php):

#!/usr/bin/php<?phpfor ($i =0; $i < $i + +)        echo $i. "\ n";

Perform:

/usr/bin/php test.php

Or:

chmod +x test.php./test.php
How to choose the Shell programming language familiarity vs. unfamiliar

If you have mastered a programming language (such as PHP, Python, Java, JavaScript), it is recommended that you write a script directly in this language, although some may be a bit verbose, but you can take advantage of the experience in this language field (unit testing, single-step debugging, IDE, Third-party class libraries).

The new learning cost is minimal, as long as you learn how to use the Shell interpreter (Jshell, adascript).

Simple vs Advanced

If you feel familiar with the language (such as Java, C) writing shell scripts is too verbose, you just want to do some backup files, install software, download data and other things, learning to use Sh,bash will be a good idea.

The shell only defines a very simple programming language, so if your scripting program is more complex, or the data structure you want to manipulate is more complex, you should use a scripting language like Python, Perl, or a high-level language that you're already good at. Because SH and bash are weak in this regard, such as:

    • Its function can only return a string and cannot return an array
    • It doesn't support object-oriented, you can't implement some elegant design patterns
    • It is interpreted as one side of the interpretation of the execution, and even PHP that kind of precompilation is not, if your script contains errors (such as calling a nonexistent function), as long as the failure to execute this line, no error
Environmental compatibility

If your script is available to other users, using SH or bash, your script will have the best environmental compatibility, Perl is standard for Linux early on, and Python has become standard for some Linux distributions over the years, as for Mac OS, which has Perl installed by default, Python, Ruby, PHP, Java and other mainstream programming languages.

The first shell script is written

Open a text editor, create a new file, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write a shell script in PHP, the extension is good with PHP.

Enter some code, the first line is usually this:

#!/bin/bash#!/usr/bin/php

“#!” is a contract tag that tells the system what interpreter the script needs to execute.

Run

There are two ways to run a shell script:

As an executable program
chmod +x test.sh./test.sh

Note, be sure to write./test.sh, not test.sh, run other binary programs as well, direct write Test.sh,linux system will go to the path to find there is no test.sh, and only/bin,/sbin,/USR/BIN,/USR /sbin wait in path, your current directory is usually not in path, so write test.sh will not find the command, to use./test.sh tells the system that it is looking in the current directory.

To run the bash script this way, the first line must be written so that the system can find the correct interpreter.

The "system" here is actually the shell application (imagine Windows Explorer), but I deliberately write the system, is easy to understand, since this system refers to the shell, then a script using/bin/sh as an interpreter can save the first line? Yes.

As an interpreter parameter

This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:

/bin/sh test.sh/bin/php test.php

The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.

Variable definition variable

When defining a variable, the variable name does not have a dollar sign ($), such as:

Your_name= "QINJX"

Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with.

In addition to explicitly assigning values directly, you can use statements to assign values to variables, such as:

For file in ' ls/etc '
Using variables

With a defined variable, just precede the variable name with a dollar sign, such as:

Your_name= "QINJX" Echo $your _nameecho ${your_name}

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:

For skill in Ada coffe Action Java; Do    Echo ' I am good at ${skill}script ' done

If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.

It is a good programming habit to add curly braces to all variables. IntelliJ when Idea writes Shell script, the IDE prompts for curly braces.

Redefine variables

A defined variable can be redefined, such as:

Your_name= "QINJX" Echo $your _nameyour_name= "Alibaba" Echo $your _name

This is legal, but note that the second assignment can not be written $your_name= "Alibaba", the use of variables when the dollar symbol.

Comments

Lines that begin with "#" are comments, which are ignored by the interpreter.

Multi-line comments

There is no multiline comment in sh, only one # is added to each line. Just like this:

#--------------------------------------------# This is a script that automatically plays IPA, based on Webfrogs's ipa-build writing: Https://github.com/webfrogs /xcode_shell/blob/master/ipa-build# Features: Automatic packaging for Etao iOS app, 14 channels of Output IPA package # Features: Fully automatic packaging, no need to enter any parameters #---------------------- ----------------------##### User Configuration area to start ######## the project root directory, it is recommended to put this script at the root of the project, there is no need to change the # application name, to ensure and Xcode product under the target_ Name.app name consistent ###### User Configuration area end  #####

What if, in the course of development, you encounter a large segment of code that needs to be annotated temporarily and then uncomment later? Each line with a # symbol is too laborious, you can put this piece of code to be annotated with a pair of curly braces, defined as a function, there is no place to call this function, the code will not be executed, to achieve the same effect as the annotation.

String

Strings are the most common and useful data types in shell programming (except numbers and strings, and no other type works well, haha), strings can be in single quotes or double quotes, or without quotes. The difference between single and double quotes is similar to PHP.

Single quotation marks
Str= ' This is a string '

Single-Quote String restrictions:

    • Any character in a single quotation mark is output as is, and the variable in the single-quote string is invalid
    • Single quotation marks cannot appear in single quote string (not after using escape character for single quotes)
Double quotes
Your_name= ' QINJX ' str= "Hello, I know your is \" $your _name\ "! \ n "
    • You can have variables in double quotes.
    • Escape characters can appear in double quotes
String manipulation stitching string
Your_name= "QINJX" greeting= "Hello," $your _name "!" greeting_1= "Hello, ${your_name}!" echo $greeting $greeting _1
Get string Length:
string= "ABCD" Echo ${#string} #输出: 4
Extract substring
String= "Alibaba is a great company" Echo ${string:1:4} #输出: Liba
Finding substrings
String= "Alibaba is a great company" Echo ' Expr index "$string" is "#输出: 8, this statement means: Find out the position of the word is in this word
Flow control of array pipeline condition judgment

Unlike Java, PHP and other languages, SH's process control is not empty, such as:

<?phpif (Isset ($_get["Q")) {    search (q);} else {    //do nothing}

You can't write this in Sh/bash, and if the Else branch doesn't have a statement execution, don't write this else.

Also note that if in sh [$foo-eq 0], this square bracket is very different from the parentheses in java/php if it is an executable program (like CD, LS, grep), fancy it? On CentOS, it is in the/usr/bin directory:

Ll/usr/bin/[-rwxr-xr-x. 1 root root 33408 June  2012/usr/bin/[

Just because the square brackets here is an executable program, the square brackets must be followed by a space, cannot be written as if [$foo-eq 0]

If ElseIf
If Conditionthen    command1     command2 ...    CommandN fi

Write a line (for the terminal command prompt):

If ' Ps-ef | grep ssh ';  then echo hello; Fi

The FI at the end is if you spell it backwards, and you'll encounter a similar

If Else
If Conditionthen    command1     command2 ...    Commandnelse    COMMANDFI
If else-if Else
If Condition1then    command1elif condition2    command2else    commandnfi
For Whilefor

Shown in the opening example:

For Var in item1 item2 ... itemndo    command1    command2    ...    Commandndone

Write a line:

For Var in item1 item2 ... itemn; Do Command1; Command2. Done;
C-style for
for ((EXP1; EXP2; EXP3)) do    Command1    command2    command3done
While
While Conditiondo    Commanddone
Infinite loops
While:d o    commanddone

Or

While Truedo    Commanddone

Or

for ((;;))
Until
Until Conditiondo    Commanddone
Case
Case "${opt}" in    "Install-puppet-server")        install_master "        exit    ;;    " Install-puppet-client ")        install_client"        exit    ;;    " Config-puppet-server ")        config_puppet_master        exit    ;;    " Config-puppet-client ")        config_puppet_client        exit    ;;    " Exit ")        exit    ;    *) echo "bad option, choose Again" Esac

The syntax of the case differs greatly from the C family language, which requires a ESAC (which is case, in turn) as the closing tag, each case branch with a closing parenthesis and a two semicolon for break

The function definition call file contains

You can use the source and. Keywords, such as:

Source./function.sh. ./function.sh

In bash, source and. Are equivalent, they are read into function.sh content and execute its content (similar to PHP include), in order to better portability, it is recommended to use the second way of writing.

Include a file and execute a file, as well as write the path to the file, not to write the filename, such as the above example:

. ./function.sh

Can not write:

. function.sh

If function.sh is a user-passed parameter, how do you get its absolute path? The method is:

Real_path= ' Readlink-f $ ' #$1 is a user-entered parameter, such as function.sh. $real _path
User input executes script when incoming script runs in input select menu stdin and stdout common commands

The SH script combines the power of a system command, in the field of character processing, with grep, awk, sed Three musketeers, grep is responsible for finding a specific line, awk can split the row into multiple fields, and SED can implement the update insert Delete write operations.

Ps

View the list of processes

grep excludes grep itself from finding results adjacent to target awksed Insert Replace delete xargscurl

Shell scripting 30 minutes to get started

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.