Shellscript: String operations

Source: Internet
Author: User

Truncate string

$ Echo 'expr substr 123 2 1'


Truncate from 0 to 3

$ Echo $ {path: 0: 3}


Replace spaces-

$ Echo "a B" | TR """-"



Var1 = "hello"

Var2 = "he"


String inclusion

Method 1-> intercept a string

Echo $ {var1: 0: 2} # extract 2 bits starting from 0->$ {Variable: 0: n}

If [$ {var1: 0: 2 }=$ var2]; then

Echo "1: Include"

Fi


Method 2

-Eq-> two equal values (equal)

$? -> Check the execution result of the last command. The value 0 indicates normal, and the value 0 indicates abnormal. [The default value is 1].

Grep "keywords" File

-> If keywords are available, the output result code is 0.

-> NO keywords does not output content, but the result code is 1 [Echo $?]

-> Grep "keywords" file-Q [-Q-> quiet; Do not write anything to standard output. exit immediately with zero status if any match is found, even if an error was detected.]

Echo "$ var1" | grep-Q "$ var2"

If [$? -EQ 0]; then

Echo "2: Include"

Fi


# Method 3

Echo "$ var1" | grep-Q "$ var2" & Echo "include" | echo "not"


# Method 4

[["$ {Var1/$ var2 /}"! = "$ Var2"] & Echo "include" | echo "not"


This article is from the "andyhdchoice" blog, please be sure to keep this source http://andyhdchoice.blog.51cto.com/1521623/1548777

Shellscript: String operations

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.