Latest web development tutorials

Shell? Variable

Shell Variables

When you define a variable, the variable name without a dollar sign ($, PHP language variables needed), such as:

your_name="w3big.com"

Note that there is no space between the variable name and equal sign, which may be familiar to you and all programming languages ​​are not the same. Meanwhile, the variable name must follow these naming rules:

  • The first character must be a letter (az, AZ).
  • No spaces, use an underscore (_).
  • You can not use punctuation.
  • You can not use bash in keyword (help command to view the available reserved keywords).

In addition to explicitly direct assignment, you can also assign values ​​to variables with statements such as:

for file in `ls /etc`

The above statement the file name / etc directory out of circulation.


Using Variables

Use a defined variable, as long as the variable name preceded by a dollar sign, like this:

your_name="qinjx"
echo $your_name
echo ${your_name}

Variable name outside the curly braces are optional, plus without all right, add braces to help explain the boundary identification variables, such as the following case:

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"
done

If you do not give skill variables plus braces, written echo "I am good at $ skillScript", the interpreter will put $ skillScript as a variable (its value is empty), code execution is not the result we expect the same again.

Tell all the variables braces, it is a good programming practice.

Defined variables, can be re-defined, such as:

your_name="tom"
echo $your_name
your_name="alibaba"
echo $your_name

Write is legal, but note that when the second assignment can not write $ your_name = "alibaba", use a variable when it is added to the dollar sign ($).

Read-only variables

Use the command readonly variables can be defined as read-only variable, the value of read-only variables can not be changed.

The following examples try to change the read-only variables, the results being given:

#!/bin/bash
myUrl="http://www.w3cschool.cc"
readonly myUrl
myUrl="http://www.w3big.com"

Run the script with the following results:

/bin/sh: NAME: This variable is read only.

Remove variable

Use the unset command to delete a variable. grammar:

unset variable_name

Variable can not be reused after being removed. unset command can not remove the read-only variables.

Examples

#!/bin/sh
myUrl="http://www.w3big.com"
unset myUrl
echo $myUrl

Examples of the implementation of the above will have no output.

Variable Types

When you run the shell, there will be three parameters simultaneously:

  • 1) Local variables Local variables defined in the script or command is only valid in the current shell instance, other shell startup program can not access local variables.
  • 2) environmental variables all programs, including the shell program started, can access environment variables, some applications require environment variables to ensure their normal operation. When necessary, the shell script can also define environment variables.
  • 3) shell variables shell variable is a special variable set by the shell program. shell variable part of the environment variable, some are local variables to ensure the normal operation of the shell

Shell string

String shell programming is the most common and useful data types (except numbers and strings, also lacks other types of handy), strings can be enclosed in single quotes, double quotes can also be used, or may not be in quotation marks. The difference between single and double quotation marks with PHP is similar.

apostrophe

str='this is a string'

Single-quoted strings restrictions:

  • Any characters in single quotes are output as single quotes in the string variable is invalid;
  • Single-quoted strings can not appear in a single quote (use single quotes after the escape character nor).

Double quotes

your_name='qinjx'
str="Hello, I know your are \"$your_name\"! \n"

Double quotes advantages:

  • Double quotes can have variable
  • Double quotes can appear escape character

String concatenation

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 examples from the first two character strings start capturing four characters:

string="w3big is a great site"
echo ${string:1:4} # 输出 unoo

Find substring

Find character "i or s" position:

string="w3big is a great company"
echo `expr index "$string" is`  # 输出 8

Note: The above script "` "anti-quotes rather than single quotes" ' "Do not be deceived, oh.


Shell array

bash supports one-dimensional array (does not support multidimensional arrays), and does not define the size of the array.

Similar to the C language, the subscript of array elements from 0 start number. Gets an array of elements to take advantage of the subscript, the subscript may be an integer or arithmetic expression whose value should be greater than or equal to 0.

Define arrays

In the Shell, use parentheses to denote an array, the array elements with the "space" symbols separated. The general form of an array is defined as follows:

数组名=(值1 值2 ... 值n)

E.g:

array_name=(value0 value1 value2 value3)

or

array_name=(
value0
value1
value2
value3
)

You can also define separately for each component of the array:

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

You can not use a continuous index, and there is no limit under the target range.

Read array

The general format is read array element value:

${数组名[下标]}

E.g:

valuen=${array_name[n]}

Use the @ symbol can get an array of all the elements, such as:

echo ${array_name[@]}

Get length of the array

Gets an array of lengths of string length and get the same way, for example:

# 取得数组元素的个数
length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得数组单个元素的长度
lengthn=${#array_name[n]}

Shell Notes

Line with "#" at the beginning of the comment, will be ignored by the interpreter.

sh there are no multi-line comments, each line can only add a # sign. Only like this:

#--------------------------------------------
# 这是一个注释
# author:本教程
# site:www.w3big.com
# slogan:学的不仅是技术,更是梦想!
#--------------------------------------------
##### 用户配置区 开始 #####
#
#
# 这里可以添加脚本描述信息
# 
#
##### 用户配置区 结束  #####

If the development process, encounter large chunks of code need to temporarily comment up after a while and uncommented, how to do it?

Each row add a # symbol too laborious, you can put some code to be annotated with a pair of curly brackets, defined as a function, no place to call this function, this code will not be executed, and reached as annotations Effect.