What is shell script programming?

Source: Internet
Author: User

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/94/F8/wKioL1kQPLbgWWanAABD3g9VJlg304.jpg "title=" 1.jpg "alt=" Wkiol1kqplbgwwanaabd3g9vjlg304.jpg "/>

What is a shell script

Example:

#!/bin/sh

cd~

Mkdirshell_tut

Cdshell_tut

For ((i=0;i<10;i++));d o

Touchtest_$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

Cd,mkdir,touch is the system comes with the program, generally in the/bin or/usr/bin directory. For,do,done is the key word for the SH scripting language.

The concept of shell and shell scripting

A shell is an application that provides an interface through which the user visits the operating system kernel. Kenthompson SH is the first type of unixshell,windowsexplorer that is a typical graphical interface shell.

Shell script (shell

Script), is a scripting program written for the shell. The shell that the industry says often refers to shell scripts, but reader friends know that shells and shells

Script is a two different concept. For reasons of habit and brevity, the "shell programming" presented in this article refers to shell scripting , not the development of the shell itself (e.g.

WindowsExplorer extended 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 MacOS).

Defining variables

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:

Forfilein ' Ls/etc '

Using variables

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

Your_name= "QINJX"

Echo$your_name

Echo${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:

Forskillinadacoffeactionjavado

echo "Iamgoodat${skill}script"

Done

If you do not add curly braces to the skill variable and write echo "Iamgoodat$skillscript", the interpreter will take $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. Intellijidea you write Shellscript, the IDE prompts you to add 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.

Single quotation marks

Str= ' thisisastring '

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,iknowyourare\" $your _name\ "!\n"

You can have variables in double quotes.

Escape characters can appear in double quotes

String manipulation

Stitching strings

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= "Alibabaisagreatcompany"

Echo${string:1:4} #输出: Liba

Finding substrings

String= "Alibabaisagreatcompany"

Echo ' Exprindex ' $string ' is ' #输出: 8, this sentence means: Find out the position of the word is in the word.

Original link: http://www.magedu.com/71404.html


This article is from the "Marco Linux Training" blog, so be sure to keep this source http://mageedu.blog.51cto.com/4265610/1923379

What is shell script programming?

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.