String manipulation in shell basics learning, for Loop statement Example _linux Shell

Source: Internet
Author: User
Tags case statement

Copy Code code as follows:

#!/bin/bash
My_name= "JXQ"

Echo $my _name
Echo ${my_name}

# ------------------------------------
# string manipulation
# ------------------------------------

# single quote string limit, double quotes without these restrictions:
# any character in the single quotation mark is output as is, and the variable in the single quote string is invalid
# single quotation marks cannot appear in one-quote strings (not after using escape characters for single quotes)
Name= "'ll"
Age=24
My_full_name= ' ${name}${age} '
Echo ${my_full_name}
My_full_name= "${name}${age}"
Echo ${my_full_name}

# string concatenation
Echo ${name}${age}

# string length
echo ${#name} # 4

# substring
Message= "I want to be healthy"
Echo ${message:2} # Want to be health, 2 is position
Echo ${message:2:4} # want,2 is position,4 is Len

# Delete Shortest match from front: ${string#substring}
Echo ${message#*want}
# Delete Shortest match from: ${string%substring}
Echo ${message%healthy}

# Delete longest match from front: ${string# #substring}
Echo ${message##*h}
# Delete longest match from: ${string%%substring}
Echo ${message%%t*}

# Find and replace: ${string/pattern/replacement}
Book_name= "Catch Eye Eye"
Echo ${book_name/eye/cat}
# Find and replace all match: ${string//pattern/replacement}
Echo ${book_name//eye/cat}

File_path= "/usr/local/bin"
# only replace when match the beginning: ${string/#pattern/replacement}
Echo ${file_path/#\/usr/tmp}
# only replace when match ' end: ${string/%pattern/replacement}
Echo ${file_path/%bin/tmp}

# string Index
Stringz=abcabc123abcabc
echo ' expr index ' $stringZ ' C12 ' # MAC OS X does not support expr


# ------------------------------------
# statement
# ------------------------------------

# if
If True
Then
echo "OK, true"
Fi

# Write a line
If true; then echo "OK"; Fi

Var= ' 12 '
If [$var-eq 12]; Then
echo "This is a numeric comparison if example"
Fi

If ["$var" = "12"]; Then
echo "This is a string if comparison example"
Fi

if [["$var" = *12*]]; Then
echo "This is a string regular expression if comparison example"
Fi

Name= "JXQ"
If ["$name" = "jxq"]; Then
echo "Hello" $name
Fi


# Loop statement
For item in ' LS *.sh '
Todo
Echo $item
echo "Completed"
Done

# Write a line
For item in ' LS *.sh '; do Echo $item; echo "Completed"; Done

Counter=1
While:
Todo
echo "Bee"
Let "counter= $counter +1"
If [$counter-eq 3]; Then
Break # Break/continue is similar to Java
Fi
Done

# Case Statement
opt= "Install"
Case "${opt}" in
"Install")
echo "Install ..."
Exit


"Update")
echo "Update ..."
Exit


*) echo "bad opt"
Esac

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.