Shell script re-learning

Source: Internet
Author: User
Tags arithmetic array definition array length logical operators

#! /bin/bash


Two ways to run the shell

Change Permissions chmod u+x

Specifying the interpreter at run time


Shell variables

Variable type

accessing variables

$, ${}

Read-only variables

ReadOnly

Delete a variable

Unset cancels a variable, but cannot cancel a read-only variable

Myurl= "http://lingdandan.blog.51cto.com" unset myurlecho $myUrl

Operation Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/7B/wKiom1fAD5fRlJ6AAAATzUTtFHQ952.png "title=" A.png " alt= "Wkiom1fad5frlj6aaaatzuttfhq952.png"/>

Myurl= "readonly myurl unset Myurlecho $MYURL

Operation Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/7A/wKioL1fAEBCjEg1hAAAoRVezDBM645.png "title=" B.png " alt= "Wkiol1faebcjeg1haaaorvezdbm645.png"/>


Shell string

Strings can be in single or double quotes, or without quotes

Single quotation marks

Characters in single quotes are output as-is

Single quotation mark cannot have single quotation mark (escape character)

Double quotes

Double quotes inside can have variables

Single quotes can have escape characters

string concatenation

Language=english

echo "Hello ${language}"

String length calculation

echo ${#VAR}

Echo ${var} | Wc-c

echo $TEST _shell_var | awk ' {print length ($)} '

Extract substring

#* characters

##* characters

Characters

Percent characters *

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/7C/wKiom1fAJbmTBONLAACZDb9GjWc620.png "title=" C.png " alt= "Wkiom1fajbmtbonlaaczdb9gjwc620.png"/>

VAR stands for variable

${var:0:10} represents 10 characters from the beginning of the first character on the left

${var:7} represents all characters after the start of the seventh character

${var:0-3:10} represents 10 characters from the third character of the right number

${VAR:0-14} represents all characters after the start of the 14th character in the right number

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/7B/wKioL1fAJyzwBjf5AABsTw22U4o569.png "title=" D.png " alt= "Wkiol1fajyzwbjf5aabstw22u4o569.png"/>

Find string

Shell Array

Array definition

Bash supports only one-dimensional arrays and does not support multidimensional

Subscript starting from 0, can be an integer or an expression

Arr= (value 1 value 2 value 3) parenthesis space separated

You can also define individual components individually

Array Read

${arr[Subscript]}

Use @ To get all the elements of an array

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/7C/wKiom1fAM3PzV7ouAAAILIhB1ig993.png "title=" E.png " alt= "Wkiom1fam3pzv7ouaaailihb1ig993.png"/>

Get array length

${#arr [@]}

${#arr [*]}

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/7C/wKiom1fANG3i5bq1AAAw_c2VTnI927.png "title=" F.png " alt= "Wkiom1fang3i5bq1aaaw_c2vtni927.png"/>


Shell-Pass parameter

Format of getting parameters in script: $n

$ $

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/7C/wKioL1fASsXwImgMAAAjEmIwmdg660.png "title=" A.png " alt= "Wkiol1fassxwimgmaaajemiwmdg660.png"/>

$* and [email protected] All refer to all parameters, the difference is that $* the argument as a string, [email protected] The argument as a single string

$* "1 2 3" Only one parameter was passed

[Email protected] "1" "2" "3" came out of three parameters

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/7C/wKioL1fAUn3QtuGvAAAyHsoav3k280.png "title=" C.png " alt= "Wkiol1faun3qtugvaaayhsoav3k280.png"/>


Shell Basic Operations

Native bash does not support simple arithmetic operations and can be done with other commands such as awk and expr.

Arithmetic operations

+ - * / % = == !=

Var= ' Expr 3 \* 5 '

Relational operators

-eq-nq-gt-lt-ge-le

Relational operators only support numbers, and strings are not supported

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/7D/wKiom1fAe73Q4X74AAAsv0btqaE109.png "title=" D.png " alt= "Wkiom1fae73q4x74aaasv0btqae109.png"/>

String operations

= =-z-n Str


Expr http://lingdandan.blog.51cto.com/10697032/1791485

Expr match string expression expr string: expression

Expr Index string character

Expr length string

Expr substr string offset length (offset starting from 1)

Boolean operations

! -o-a

logical operators

&& | |

File Test Operators

[-F file]-d-b-c-p-r-w-x-s (file is greater than 0)-e (directory or file exists)


For

For variable in list

Do

Done

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/7D/wKiom1fAUc-wDOhQAABOIeeUR9o011.png "title=" B.png " alt= "Wkiom1fauc-wdohqaaboieeur9o011.png"/>



This article from "Zero Egg" blog, declined reprint!

Shell script re-learning

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.