bash string 操作

最後更新: 2019-01-07

 

目錄

  • $EXCLUDEPATTER 在同一行
  • Trim space
  • 分割 String
  • echo
  • 反串
  • seq
  • String format check
  • Check String "start with"
  • 刪除 /n/r
  • printf
  • 過濾 String
  • regexp
  • Mulit-line text

$EXCLUDEPATTER 在同一行

EXCLUDEPATTER='--exclude=/var/src
               --exclude=/var/cache
               --exclude=/tmp
               --exclude=/proc
               --exclude=/dev
               --exclude=/sys
               --exclude=/lost+found'

 


Trim space

 

1) 清除了所有 space

echo " test " | tr -d ' '

2) 保留到中間 space

VAR=" testing 123 "
VAR=$(echo $VAR)
echo $VAR

 


分割 String

 

數量:

myvar=123abc123123

echo "${myvar:4:3}"                     # bc1

取某欄

cut

 


echo

  • -n     不會自動加 newline
  • -e     可以輸入 \t \n 之類的東西

 


反串

echo datahunter | rev

retnuhatad

 


`seq n m` 與 {n..m}

 

{n..m}

row=25
for m in {1..$row};do   # 會唔 work 的
    echo $row
done

seq

SYNOPSIS

       seq [OPTION]... LAST
       seq [OPTION]... FIRST LAST
       seq [OPTION]... FIRST INCREMENT LAST

seq 5

1
2
3
4
5

seq reverse

seq 5 -1 0

5
4
3
2
1
0

for loop 應用

row=25
for i in `seq $row`;do
    echo $i
done

 


String format check

 

"=~" usage

#!/bin/bash

for version in 1.2 1.2.3 1.2.3.4; do
    printf "%s\t" $version
    [[ $version =~ ^[0-9]\.[0-9]$ ]] && echo y || echo n
done

for version in 1.1 a.b aa.bb aa.bb.cc; do
    printf "%s\t" $version
    [[ $version =~ ^[a-z]+\.[a-z]{2,}$ ]] && echo y || echo n
done

# the string to the right of the operator is considered an extended regular expression and matched accordingly

DOC

http://regexr.com/

 


Check String "start with"

 

if [[ 'DEV-0-1' == DEV* ]]; then echo "yes"; fi

Or, you can use parameter expansion:

if [[ ${line:0:1} = \# ]] ; then
    echo "$line starts with #"
fi

 


刪除 /r/n

 

[2] COMMAND=$( echo $COMMAND | tr -d '\n' | tr -d '\r' )

[1] Pipe to sed -e 's/[\r\n]//g' to remove both Carriage Returns (\r) and Line Feeds (\n) from each text line

 


printf

 

Usage

printf <FORMAT> <ARGUMENTS...>

FORMAT

%d    Print the associated argument as signed decimal number
%s    Interprets the associated argument literally as string

Modifiers

N

Specifies a minimum field width,
if the text to print is shorter, it's padded with spaces,
if the text is longer, the field is expanded

.   

Together with a field width,
the field is not expanded when the text is longer,
the text is truncated instead.

*

the width is given as argument before the string or number.
Usage (the "*" corresponds to the "20"): printf "%*s\n" 20 "test string"

0

Pads numbers with zeros, not spaces

+

Prints all numbers signed (+ for positive, - for negative)

-

Left-bound text printing in the field (standard is right-bound)

 


過濾 String

 

Example

Rand_String=123

if [[ "${Rand_String//[0-9A-Za-z]/}" = "" ]];
then
    echo "Only contains alphanumeric"
else
    echo "Contains other characters"
fi

原理

用 Shell Parameter Expansion 去取代不要的字

To replace the first occurrence of a pattern with a given string, use

${parameter/pattern/string}

To replace all occurrences, use

${parameter//pattern/string}

 

特別字

i.e.

_
.
-

echo ${Rand_String//[0-9a-zA-Z_\.\-]}

 


regexp

 

Rand_String="3a"

# 其中一個中就 TRUE
regexp='[0-9]'
if [[ $Rand_String =~ $regexp ]];
then
    echo $Rand_String
fi

# 其中一個不中就 TRUE
regexp='[^0-9]'
if [[ $Rand_String =~ $regexp ]];
then
    echo $Rand_String
fi

Remark

適用於

regexp='^/home/vhosts/'
Rand_String="/home/vhosts/datahunter.org/public_html"

 


Mulit-line text

 

#!/bin/bash

string1=$(cat << EOF
<html lang="en">
<body>
test1
</body>
</html>
EOF
)

string2='
<html lang="en">
<body>
test2
</body>
</html>
'

echo $string1     # <html lang="en"> <body> test1 </body> </html>
echo $string2     # 也是一行過 output

 

 

Creative Commons license icon Creative Commons license icon