#!/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