Shell Quick Tour

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


Summary from:
HTTPS://GITHUB.COM/QINJX/30MIN_GUIDES/BLOB/MASTER/SHELL.MD;
http://blog.itpub.net/14293828/viewspace-1447570
1. What is a shell script 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:


[[email protected] ~]# 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";
carried out:

/ usr / bin / php test.php
or:

chmod + x test.php
./test.php




2. 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.





3. Shell command and Process Control


There are three types of commands that can be used in shell scripts:



1) Unix Commands :



Although you can use any Unix command in a shell script , there are some relatively common commands. These commands are usually used for file and text operations.



Common command syntax and features :


echo "some text": Print the text on the screen.

ls: file list.

wc -l file wc -w file wc -c file: Count the number of lines in the file Count the number of words in the file Count the number of characters in the file.

cp sourcefile destfile: File copy.

mv oldname newname: Rename or move a file.

rm file: delete file.

grep ‘pattern’ file: Search for a string in a file. Example: grep ‘searchstring’ file.txt

cut -b colnum file: specify the file content range to be displayed, and output them to the standard output device. For example: output the 5th to 9th characters of each line. cut -b 5-9 file.txt must not be used with the cat command Confused, these are two completely different commands.

cat file.txt: Outputs the contents of the file to the standard output device (screen).

file somefile: get the file type.

read var: Prompts the user for input and assigns the input to a variable.

sort file.txt: Sort the lines in the file.txt file.

uniq: Delete the lines and columns that appear in the text file. Example: sort file.txt | uniq.

expr: Perform mathematical operations Example: add 2 and 3 expr 2 "+" 3.

find: Search for files. For example, find. -name filename -print based on the file name.

tee: Output data to standard output devices (screens) and files such as: somecommand | tee outfile.

basename file: Returns the name of the file without the path. For example: basename / bin / tux will return tux.

dirname file: returns the file path. For example: dirname / bin / tux will return / bin.

head file: print the first few lines of a text file.

tail file: print the last few lines of the text file.

sed: Sed is a basic find and replace program. You can read in text from standard input (such as a command pipeline) and output the result to standard output (screen). This command searches using regular expressions (see reference). Not to be confused with wildcards in the shell. For example: Replace linuxfocus with LinuxFocus: cat text.file | sed ‘s / linuxfocus / LinuxFocus /’> newtext.file.

awk: awk is used to extract fields from a text file. By default, the field separator is a space. You can use -F to specify other separators. cat file.txt | awk -F, ‘{print", "}’ Here we use it as a field separator and print the first and third fields at the same time. If the contents of the file are as follows:

Adam Bor, 34, IndiaKerry Miller, 22, USA

The command output is:

Adam Bor, Kerry Miller, India. 








2) concepts : Piping , redirection and backtick



These are not system commands, but they are really important.



Pipe (|) The output of one command as input to another command.


grep "Hello" file.txt | Wc-l


Searches for a row containing "Hello" in File.txt and calculates its number of rows. Here the output of the grep command is used as input to the WC command. Of course you can use multiple commands.



Redirect: Outputs the result of the command to a file instead of the standard output (screen).



> Write files and overwrite old files.



>> add to the end of the file, preserving the contents of the old file.



Inverse slash, use the backslash to make the output of one command a command- line argument for another command.



Command:


Find. -mtime-1-type F-print


Used to find files that have been modified in the last 24 hours (-mtime–2 represents the last 48 hours). If you want to hit a package with all the files you find, you can use the following script:


#!/bin/sh

# The Ticks is Backticks (') not normal quotes ('):

TAR-ZCVF lastmod.tar.gz ' Find. -mtime-1-type F-print '


3) Process Control



The "If" expression executes the following part if the condition is true:



If ....; Then




4. First shell script writing


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 inch  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_name

your_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 starts ipa, based on webfrogs' ipa-build writing: https://github.com/webfrogs/xcode_shell/blob/master/ipa-build

# Function: Automatically package for etao ios app, the output is an ipa package for 14 channels
# Features: fully automatic packaging, no need to enter any parameters
# --------------------------------------------

##### User Configuration Area Start #####
#
#
# Project root directory, this script is recommended to be placed in the root directory of the project, there is no need to change it here
# Application name, make sure it matches the target_name.app name under Product in Xcode
#
##### End of user configuration area #####


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 are \"$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} #Output: 4
Extract substring
string = "alibaba is a great company"
echo $ {string: 1: 4} #Output: liba
Finding substrings
string = "alibaba is a great company"
echo `expr index" $ string "is` # Output: 8, the meaning of this sentence is: find the position of the word is in this famous phrase
More


See resources at the end of this document for the advanced bash-scripting Guid Chapter 10.1


Flow control of array pipeline condition judgment


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


<?php
if (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 the java/php if it is an executable program (like CD, LS, grep). 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 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 condition
then
    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 :do    commanddone


Or


while truedo    commanddone


Or


for (( ; ; ))
Until
until conditiondo    commanddone
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 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


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 the path to the file, as in executing a file, not to write the filename, as in the example above:


. ./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` # $ 1 is a parameter entered by the user, such as function.sh
. $ real_path 
Resources
    • Advanced Bash-scripting Guide, very detailed, very easy to read, a large number of example, can be used as the introductory materials, can also be consulted as reference books
    • Unix Shell Programming
    • Linux Shell Scripting tutorial-a Beginner ' s Handbook


Shell Quick Tour


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.