Shell interpreter, variable, string, array

Source: Internet
Author: User
Tags php programming

1.Shell Introduction

The shell is a program written in C that is a bridge for users to use Linux. The shell is both a command language and a programming language. A Shell is an application that provides an interface through which users access the service of the operating system kernel. Shell programming, like Java, PHP programming, as long as there is a text editor can write code and a can explain the execution of the script interpreter can be common is:

    • Bourne Shell (/usr/bin/sh or/bin/sh)
    • Bourne Again Shell (/bin/bash)
    • C Shell (/USR/BIN/CSH)
    • K Shell (/usr/bin/ksh)
    • Shell for Root (/sbin/sh)

Execute shell script Two methods, one is executed as an executable program, first to give execution permission, and the other is to execute as an interpreter parameter;

chmod +x./test.sh

./test.sh

/bin/sh test.sh

/bin/php test.php

2.shell the variable

Defining variables

When defining a variable, it is given directly, with the symbol $ when the variable is used, and the curly braces help the interpreter identify the boundary of the variable name;

Your_name= "Runoob.com"

echo $your _name or Echo ${your_name}

Read-only variables

Myurl= "http://www.w3cschool.cc"

ReadOnly Myurl

Myurl= "http://www.runoob.com" Changes the variable will error

Delete a variable

Use the unset command to delete a variable. Grammar:

Unset variable_name

3.shell string

Defining strings

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

Your_name= ' QINJX '

Str= "Hello, I know your is \" $your _name\ "! \ n "

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 quotation marks (not after using the escape character for single quotes). You can have variables in double quotes.

Escape characters can appear in double quotes

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

The following instance intercepts 4 characters starting with the 2nd character of the string:

String= "Runoob is a great site"

echo ${string:1:4} # Output Unoo

Finding substrings

Find the location of the character "I or S":

String= "Runoob is a great company"

echo ' expr index ' $string ' is ' # output 8

In the above script "'" is the anti-quote, not the single quotation mark "'", do not look wrong oh.

4.Shell Array

Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.

Similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.

Defining arrays

In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of the definition array is:

Array name = (value 1 value 2 ...) Value N)

For example:

Array_name= (value0 value1 value2 value3)

Or

Array_name= (

Value0

Value1

value2

Value3

)

You can also define individual components of an array individually:

Array_name[0]=value0

Array_name[1]=value1

Array_name[n]=valuen

You can not use successive subscripts, and there is no limit to the range of subscripts.

Reading an array

The general format for reading array element values is:

${array name [subscript]}

For example:

Valuen=${array_name[n]}

Use the @ symbol to get all the elements in the array, for example:

Echo ${array_name[@]}

Gets the length of the array

The method of getting the length of the array is the same as getting the string length, for example:

# Gets the number of array elements

length=${#array_name [@]}

# or

length=${#array_name [*]}

# Gets the length of an array of individual elements

lengthn=${#array_name [n]}

Shell interpreter, variable, string, array

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.