Sh scripts combined with system commands have powerful power. in the character processing field, grep, awk, and sed are three musketers. grep is responsible for identifying specific lines, awk can split rows into multiple fields, and sed can perform write operations such as update, insert, delete, and so on. What is a Shell script example?
Let's look at an example:
#!/bin/shcd ~mkdir shell_tutcd shell_tutfor ((i=0; i<10; i++)); do touch test_$i.txtdone
Example
Row 1st: specifies the script interpreter. The Interpreter/bin/sh is used here.
Row 2nd: switch to the home directory of the current user
Row 3rd: create a directory named shell_tut.
Row 4th: switch to the shell_tut directory.
Row 3: cyclic condition, with a total of 10 loops
Row 6th: Create a test_1... 10.txt file
Row 3: The end of the loop body
Cd, mkdir, and touch are all built-in programs in the/bin or/usr/bin directory. For, do, and done are keywords of the sh script language.
Concepts of shell and shell scripts
Shell is an application that provides an interface through which users can access the services of the operating system kernel. Ken Thompson's sh is the first Unix Shell, and Windows Explorer is a typical GUI Shell.
Shell script is a script program written for shell. The shell mentioned in the industry usually refers to shell scripts, but readers should know that shell and shell script are two different concepts. For the sake of conciseness, the "shell programming" mentioned in this article refer to shell script programming, not the development of shell itself (such as Windows Explorer extension development ).
Environment
Shell programming is the same as 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
Currently, all mainstream operating systems support shell programming. the shell programming described in this document refers to the shell in Linux, which is basically about functions in the POSIX standard. therefore, it is also applicable to Unix and BSD (such as Mac OS ).
Linux
The shell interpreter is installed in Linux by default.
Mac OS
Mac OS not only includes sh and bash, but also built-in non-commonly used interpreters such as ksh, csh, and zsh.
Simulator on Windows
Windows does not have a built-in shell interpreter. you need to install it on your own. to use tools such as grep, awk, and curl at the same time, you 'd better install cygwin or mingw to simulate the linux environment.
Script interpreter sh
That is, the Bourne shell, POSIX (Portable Operating System Interface) standard shell interpreter. its binary file path is usually/bin/sh, which is developed by Bell Labs.
This article is about sh. if you use other languages for shell programming, please refer to the documentation in the corresponding language.
Bash
Bash is a replacement of the Bourne shell, which belongs to the GNU Project. the binary file path is usually/bin/bash. Bash, sh, and shell are usually mixed in the industry. for example, you will often see it in the text of recruiting O & M engineers: familiar with Linux Bash programming and proficient in Shell programming.
In CentOS,/bin/sh is a symbolic link pointing 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/dashlrwxrwxrwx. 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 difference is only about 100 bytes:
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 language
Theoretically, as long as a language provides an interpreter (not just a compiler), this language can be competent for script programming. common interpreted languages can be used for script programming, such: perl, Tcl, Python, PHP, and Ruby. Perl is the oldest scripting language. Python has become a preset interpreter for some linux releases over the years.
Compiled language, as long as there is an interpreter, can also be used as script programming, such as C shell is built-in (/bin/csh), Java has a third-party interpreter Jshell, ada has a paid interpreter AdaScript.
The following is an example of a PHP Shell Script (assuming the file name is test. php ):
#! /Usr/bin/php
Run:
/usr/bin/php test.php
Or:
chmod +x test.php./test.php
How to select a shell programming language to familiarize yourself with vs strangersIf you have mastered a programming language (such as PHP, Python, Java, and JavaScript), we recommend that you directly use this language to write a script program, although it may be a bit cool in some places, however, you can leverage your experience in this language field (unit testing, single-step debugging, IDE, and third-party class libraries ).
The new learning cost is very small. you only need to learn how to use the shell interpreter (Jshell and adas.
Simple vs AdvancedIf you think that writing shell scripts in familiar languages (such as Java and C) is too cool, you just want to do something like backing up files, installing software, and downloading data, bash is a good idea to learn to use sh.
Shell only defines a very simple programming language. Therefore, if your script program is complicated or the data structure to be operated is complicated, you should still use scripting languages such as Python and Perl, or advanced languages that you are already very good. Sh and bash are weak in this regard, for example:
Its function can only return strings, but cannot return arrays.
It does not support object-oriented design, and you cannot implement some elegant design patterns.
It is interpreted, while interpreting and executing, and does not support PHP pre-Compilation. if your script contains errors (such as calling a function that does not exist ), no error is reported if this row is not executed.
Environment Compatibility
If your script is provided to other users and sh or bash is used, your script will have the best environment compatibility. perl is a standard edition for linux, python has become a standard for some linux releases over the years. for mac OS, it has installed mainstream programming languages such as perl, python, ruby, php, and java by default.
Write the first shell script
Open the text editor and create a new file with the extension sh (sh stands for shell). The extension does not affect script execution. if you use php to write shell scripts, php is enough for the extension.
Enter some code. The first line is generally like this:
#!/bin/bash#!/usr/bin/php
"#!" Is an agreed tag, which tells the system what interpreter is required to execute this script.
Run
There are two methods to run Shell scripts:
As an executable program
chmod +x test.sh./test.sh
Note that you must write it. /test. sh, not test. sh to run other binary programs. write test directly. sh, the linux system will go to the PATH to find whether it is named test. sh, and only/bin,/sbin,/usr/bin,/usr/sbin and so on in the PATH. your current directory is usually not in the PATH, so it is written as test. sh cannot find the command. /test. sh tells the system to find it in the current directory.
Run the bash script in this way. The first line must be correct so that the system can find the correct interpreter.
Here, the "system" is actually the shell application (imagine Windows Explorer), but I deliberately write it as a system for ease of understanding. Since this system refers to shell, can a script using/bin/sh as the interpreter save the first line? Yes.
As interpreter parameter
In this running mode, run the interpreter directly. the parameter is the name of the shell script file, for example:
/bin/sh test.sh/bin/php test.php
This method does not need to specify the interpreter information in the first line.
Variable definition variable
When defining a variable, the variable name does not contain the dollar sign ($), for example:
your_name="qinjx"
Note that there is no space between the variable name and equal sign, which may be different from all programming languages you are familiar.
In addition to explicitly assigning values directly, you can also assign values to variables using statements, such:
for file in `ls /etc`
Use variables
To use a defined variable, you only need to add a dollar sign before the variable name, such:
your_name="qinjx"echo $your_nameecho ${your_name}
The curly braces outside the variable name are optional and can be added without adding them. the curly braces are used to help the interpreter identify the boundary of the variable, for example:
for skill in Ada Coffe Action Java; do echo "I am good at ${skill}Script"done
If you do not add curly brackets to the skill variable and write it as echo "I am good at $ skillScript", the interpreter regards $ skillScript as a variable (its value is null ), the code execution result is not what we expect.
We recommend that you add curly braces to all variables. this is a good programming habit. When writing shell scripts in IntelliJ IDEA, IDE will prompt to add curly braces.
Redefinition of variables
The defined variables can be redefined, for example:
your_name="qinjx"echo $your_nameyour_name="alibaba"echo $your_name
This write method is legal, but note that $ your_name = "alibaba" cannot be written for the second value assignment. The dollar character is added only when the variable is used.
Note
The line starting with "#" is a comment and will be ignored by the interpreter.
Multi-line comment
Sh does not contain comments from multiple lines. Only one # sign can be added to each line. Like this:
# ---------------------------------------- # This is an automatic ipa script, based on webfrogs ipa-build writing: https://github.com/webfrogs/xcode_shell/blob/master/ipa-build# function: automatically for etao ios app packaging, ipa packages with 14 output channels # Features: fully automated packaging, you do not need to enter any parameters # -------------------------------------- ###### start of the user configuration area ####### the Project root directory. we recommend that you put this script in the project root directory, you do not need to change the application name here. make sure that the application name is consistent with the target_name.app name under Product in Xcode ##### end of the user configuration area #####
What should I do if a large code segment needs to be commented out temporarily and the comment is canceled later? Adding a # symbol to each line is too laborious. you can enclose the code to be annotated with a pair of curly brackets and define it as a function. This function is called everywhere, this code won't be executed, achieving the same effect as the annotation.
String
Strings are the most commonly used and useful data types in shell programming (except for numbers and strings, there are no other types to use, haha). strings can be single quotes or double quotes, you can also ignore quotation marks. The difference between single and double quotation marks is similar to that in PHP.
Single quotes
str='this is a string'
Restrictions on single quotes:
Any character in single quotes is output as is, and the variable in single quotes is invalid.
Single quotation marks are not allowed in single quotation marks (not after single quotation marks are used as escape characters)
Double quotation marks
your_name='qinjx'str="Hello, I know your are \"$your_name\"! \n"
String operation concatenation string
your_name="qinjx"greeting="hello, "$your_name" !"greeting_1="hello, ${your_name} !"echo $greeting $greeting_1
Obtain the string length:
String = "abcd" echo $ {# string} # output: 4
Extract substring
String = "alibaba is a great company" echo $ {string: 1: 4} # output: liba
Search for substrings
String = "alibaba is a great company" echo 'expr index "$ string" is '# output: 8. This statement indicates finding the position of the word is in this statement.
Array MPs queue condition judgment process control
Unlike Java, PHP, and other languages, the sh process control cannot be blank, for example:
It cannot be written in sh/bash. if the else branch has no statement execution, do not write this else.
Note that if [$ foo-eq 0] in sh is different from the parentheses behind if in Java/PHP, it is an executable program (the same as cd, ls, grep? On CentOS, it is in the/usr/bin directory:
Ll/usr/bin/[-rwxr-xr-x. 1 root 33408 2012/usr/bin /[
Square brackets are an executable program. square brackets must be followed by spaces and cannot be written as if [$ foo-eq 0].
If elseifif conditionthen command1 command2 ... commandN fi
Write a line (applicable to terminal command prompt ):
if `ps -ef | grep ssh`; then echo hello; fi
The fi at the end is the spelling of if, and similar
If elseif conditionthen command1 command2 ... commandNelse commandfi
If else-if elseif condition1then command1elif condition2 command2else commandNfi
For whileforIn the example at the beginning, we demonstrated the following:
for var in item1 item2 ... itemNdo command1 command2 ... commandNdone
Write a line:
for var in item1 item2 ... itemN; do command1; command2… done;
C-stylefor (( EXP1; EXP2; EXP3 ))do command1 command2 command3done
Whilewhile conditiondo commanddone
Infinite Loopwhile :do commanddone
Or
while truedo commanddone
Or
for (( ; ; ))
Untiluntil conditiondo commanddone
Casecase "${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 case is very different from that of C family. it requires an esac (that is, the case in turn) as the end mark. each case branch uses the right parentheses and two semicolons (;) to represent the break.
Function definition call file inclusionYou can use the source and. keywords, such:
source ./function.sh. ./function.sh
In bash, source and. they are all reading functions. sh content and execute its content (similar to include in PHP). For Better portability, the second method is recommended.
Similar to executing a file, a file path must be written. the file name cannot be written simply. for example:
. ./function.sh
Cannot write:
. function.sh
If function. sh is a user-passed parameter, how can we obtain its absolute path? The method is:
Real_path = 'readlink-f $ 1' #$1 is a user input parameter, such as function. sh. $ real_path
When the user inputs the execution script, input the common commands stdin and stdout from the select menu in the script running.Sh scripts combined with system commands have powerful power. in the character processing field, grep, awk, and sed are three musketers. grep is responsible for identifying specific lines, awk can split rows into multiple fields, and sed can perform write operations such as update, insert, delete, and so on.
PsView process List
Grep exclude grep self-Search Results adjacent to target awksed insert replace delete xargscurl