Shell scripting 30 minutes to get started

Source: Internet
Author: User
Tags function definition posix


What is a shell script
Shell scripting (English: Shell script), also known as shell Command Draft, programmatic script, is a computer program and text file, the contents of a series of shell commands, through the Unix shell literal translation of its contents after the operation. is designed as a scripting language that works in the same way as the literal language, where the Unix shell plays the role of the command-line interpreter, and after the shell script is read, the shell commands are run sequentially, and then the results are output. The shell script can be used for system management, file operation and so on.
Example


Let's look at an example:


 
#!/bin/sh  cd ~

mkdir shell_tut

cd shell_tut for ((i=0; i<10; i++)); do touch test_$i.txt

done
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:


[root@centosraw ~]# ls -l /bin/*sh

-rwxr-xr-x. 1 root root 903272 Feb 22 05:09 /bin/bash

-rwxr-xr-x. 1 root root 106216 Oct 17  2012 /bin/dash

lrwxrwxrwx. 1 root root      4 Mar 22 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

<?php

for ($i=0; $i < 10; $i++)

        echo $i . "\n";

run:

/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, even if the PHP kind of precompilation is not, if your script contains errors (such as calling a non-existent function), as long as not executed in this line, will not 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 convention mark 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  in ' ls/etc '
Using variables


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


Your_name="qinjx"$your _name${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 inch  Do  ' 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"$your _name  your_name="  Alibaba"$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 config area starts ########Project root directory, it is recommended to put this script at the root of the project, there is no need to change#application name, ensuring that the Target_name.app name is consistent with the product under Xcode### # # user config 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 not valid
    • • Single quotation marks cannot appear in single quote strings (not after using escape characters 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=""$your _name "  ! " greeting_1="Hello, ${your_name}! " "  $greeting$greeting _1
Get string Length:
string="abcd"${#string} #输出: 4
Extract substring
string="Alibaba is a great company"# output: Liba
Finding substrings
string="Alibaba is a greatcompany","$string" is " # Output: 8, this statement means: Find the word is in the position of the word
Flow control of array pipeline condition judgment
Unlike Java, PHP and other languages, the flow control of sh cannot be empty, such as:

<?php

If (isset($_GET["q"])) {

        Search(q);

}

Else {

        //do nothing

}

You can't write this in sh/bash. If there is no statement execution in the else branch, don't write this else.

Also note that if [ $foo -eq 0 ] in sh, this square bracket is very different from the parentheses after the if in Java/PHP. It is an executable program (like cd, ls, grep), unexpected. Right? On CentOS, it is in the /usr/bin directory:

Ll /usr/bin/[

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

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

If else

If

If condition

Then

        Command1

        Command2

        ...

        commandN

Fi

Write a line (for terminal command prompts):

If `ps -ef | grep ssh`; then echo hello; fi

The fi at the end is if the spell is reversed, and you will encounter similar ones later.

If else

If condition

Then

        Command1

        Command2

        ...

        commandN

Else

        Command

Fi

If else-if else

If condition1

Then

        Command1

Elif condition2

        Command2

Else

        commandN

Fi

For while

For

Demonstrated in the opening example:

For var in item1 item2 ... itemN

Do

        Command1

        Command2

        ...

        commandN

Done

Write a line: 




C-style for
For (( EXP1; EXP2; EXP3 ))

Do

        Command1

        Command2

        Command3

Done

While

While condition

Do

        Command

Done

Infinite loop

While :

Do

        Command

Done

or

While true

Do

        Command

Done

or

For (( ; ; ))

Until

Until condition

Do

        Command

Done

Case

Case "${opt}" in

        "Install-Puppet-Server" )

                Install_master $1

                Exit

        ;;

 

        "Install-Puppet-Client" )

                Install_client $1

                Exit

        ;;

 

        "Config-Puppet-Server" )

                Config_puppet_master

                Exit

        ;;

 

        "Config-Puppet-Client" )

                Config_puppet_client

                Exit

        ;;

 

        "Exit" )

                Exit

        ;;

 

        * ) echo "Bad option, please choose again"

Esac

The syntax of the case is very different from the C family language. It requires an esac (that is, the case is reversed) as the end tag, each case branch with the right parenthesis, and two semicolons for the 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 both read the contents of function.sh and execute their contents (similar to the include in PHP). For better portability, the second way is recommended.

To include a file and execute a file, you must also write the path to the file. You cannot write the file name, as in the above example:

. ./function.sh

Can't write:

. function.sh

If function.sh is a parameter passed in by the user, how do you get its absolute path? the way is:

Real_path=`readlink -f $1`#$1 is a parameter entered by the user, 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


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.