Shell Programming----Basics in Linux

Source: Internet
Author: User

1, there are two common ways to run a shell script:

First give executable permission, then enter the directory where the file is located, enter:./name.sh;

Run the interpreter and execute the script:/bin/sh name.sh, in which case there can be no #! in the script /bin/bash the situation.

2,shell Variable     Variable naming rules:
    1. Names can only be used in English letters, numbers and underscores, and the first character cannot be numerically

      Beginning.
    • Variable name and equal sign, there is no space in the middle of the variable name, you can use an underscore (_).
    • Punctuation cannot be used.
    • You can't use the keywords in bash
Variable assignment:

Direct Assignment

You can use the for-XX in name Loop, for I in "ls/home" for I in $ (ls/home); PS: The first I will be directly equal to ' ls/home ', which is not the same as Python, it is directly the entire string, the second is more reliable, traverse all home/home folder and file name. There is another kind to distinguish, is for I in ' ls/home ', (note this is not a quotation mark, is ~ that key), the effect and $ () like

Using variables:

For example: your_name="QINJX"

Usage 1:echo $your _name $ direct Add variable name

Usage 2 :echo ${your_name} ${} is the variable name in the middle, and curly braces help the shell to identify the boundary.

For example: your_name="QINJX"

 GR Eeting= "Hello,"  $your _name "!" #用法1  Greeting_1= "Hello, ${your_name}!" #用法2    

Special usage: read-only variable: readonly your_name, plus readonly, the variable can only be read, cannot be re-assigned, cannot be changed.

Delete variable: unset your_name, note You cannot delete a read-only variable

Variable Type

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.

   Shell string

The types of variables in the shell are basically two, numbers and strings, and strings can use single double quotes to indicate that double quotes are better because the double quotes internally support escape characters and reference other variables.

Like what:

astring=' qinjx '

str="Hello, I know you is \" $astring \ "! \ n"

      common strings Action

astring= " Hello Shell"

Get string length: Echo ${#astring} output 11

Intercept string: Echo ${astring:0:4} output Hell is different from Python, which represents starting with a string after 0 and intercepting 4 backwards, by the way, the shell subscript is not the same as Python, and the shell subscript starts with 1.

Find string: Echo ' expr index ' $astring ' o ' output 5. (This is the anti-quote) means to find the character O from the variable astring,

3 Shell Array

Shell supports arrays. The subscript of the array starts with 0. The array is denoted by parentheses, and the numbers are separated by a space.

Methods for defining Arrays 1:alist= (1 2 3 4 5)

Method 2:alist[0] =1;alist[1]=alist[2]=3;alist[3]=4 ...

To read an array: ${array name [subscript]}, for example ${alist[0]}, @ can represent all subscripts in the array: ${alist[@]}

Get the number of array elements: Echo ${#alsit [@]}

Get the length of an element of an array: Echo ${#alist [number]}

4 Shell Annotations

Two kinds of annotations should suffice.

1, #在开头

2,

:<<EOF Comment content ... Comment Content ... Comment Content ... EOF    

Shell Programming----Basics in Linux

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.