Shell learns a

Source: Internet
Author: User
Tags echo command php language

One, shell script

Open a text editor (you can use the Vi/vim command to create a file), a new file test.sh, the extension sh (sh for Shell), and the extension does not affect script execution

#!/bin/bash
echo "Hello world!"

"#!" is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell.

The echo command is used to output text to a window.

There are two ways to run a shell script: 1, as an executable program

Save the above code as test.sh and CD to the appropriate directory:

+./test.  #使脚本具有执行权限./test.  #执行脚本      


2. As an interpreter parameter

This works by running the interpreter directly, whose parameters are the file name of the shell script, such as:/bin/sh test. SH

Second, shell variables

1, when defining variables, the variable name does not add the dollar sign ($,php language variables required), such as:your_name="cc.com"

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. At the same time, the name of the variable names must follow the following rules:

    • The first character must be a letter (a-z,a-z).
    • You can use an underscore (_) without spaces in the middle.
    • Punctuation cannot be used.
    • You can't use the keywords in bash (you can see the reserved keywords using the help command).

In addition to explicitly assigning values directly, you can use statements to assign values to variables, such as:for file in ' ls/etc ' (The above statement loops the file name of the directory under/etc.) )2. Using variablesWith a defined variable, just precede the variable name with a dollar sign, such as:Your_name= "Cc.com"

Echo $your _name
Echo ${your_name}The curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variableFor skill in Ada coffe Action jave;do
echo "I am good at ${skill}scipt"
DoneOperating result: I am good at adascipt
I am Good at coffescipt
I am Good at actionscipt
I am Good at javescipt
It is a good programming habit to add curly braces to all variables.

A defined variable can be redefined, such as:
Your_name= "Tom"
Echo ${your_name}

Your_name= "Alibaba"
Echo ${your_name}

3. read-only variables

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

The following example attempts to change a read-only variable, resulting in an error: #!/bin/bash
Myurl= "http://www.w2cschool.cc"
ReadOnly Myurl

Myurl= "Http://www.baidu.com"

Running result:/usercode/file.sh:line 5:myurl:readonly variable

4. Delete variables:

Use the unset command to delete a variable that cannot be used again after it has been deleted. The unset command cannot delete a read-only variable.

#!/bin/sh Myurl="http://www.runoob.com"

Unset Myurl

Echo $MYURL

5. Variable type

When you run the shell, there are three different variables:

    • 1) local variable local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
    • 2) Environment variables All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
    • 3) shell variables shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.
Three, Shell string string is the most commonly used in shell programming the most useful data types (in addition to numbers and strings, there is no other type of use), strings can be used in single quotation marks, or double quotation marks, can also be used without quotation marks.

Single-Quote String restrictions:

    • 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 quote strings (not after using escape characters for single quotes).

Advantages of double quotes:

    • You can have variables in double quotes.
    • Escape characters can appear in double quotes
    • Your_name= "QiQi"
      Str= "Hello,i know you is \" ${your_name}\ "!\n"
      Echo ${str}
Stitching strings:Your_name= "QiQi"
greeting= "Hello," $your _name "!"
greeting_1= "hi,${your_name}!"
echo $greeting $greeting _1Running Result: hello,qiqi! hi,qiqi!Gets the length of a string, extracts a substring, finds the location of a child character#!/bin/bash

String= "Runoob is a great company"
echo ${#string} #获取字符串长度

Echo ${string:1:4} # Extract substring

echo ' expr index ' ${string} ' is ' # finding the position of the character I or S

Four, array

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:

Such as:
Array_name= (value0 value1 value2 value4)
Or:
Array_name= (
Value0
Value1
value2
Value3
Value4
)
You can also define individual components of an array individually:
Array_name[0]=value0
Array_name[1]=value1
Array_name[2]=value2

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

&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&

#! /bin/bash
Array_name= (value0 value1 value2 value4)

Array_name= (
Value0
Value1
value2
Value3
Valuevalue4
)


Array_name[0]=value0
Array_name[1]=value1
Array_name[2]=value2

Echo ${array_name[2]} # Read array
Echo ${array_name[@]} # reads all elements of a value
echo length=${#array_name [@]} #取得数组元素的个数
echo length=${#array_name [*]}
Echo length4=${#array_name [4]} # takes the length of an array of individual elements

Shell annotations

Lines that begin with "#" are comments, which are ignored by the interpreter.

There is no multiline comment in sh, only one # is added to each line. Can only be like this:

#--------------------------------------------# This is a note # Author: Rookie Tutorial # site:www.runoob.com# Slogan: Learn not only the technology, but also the dream! #--------------------------------------------##### user config area starts ##### # # # #Here you can add script description information # ##### 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.

Shell learns a

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.