Skip to content

Commit

Permalink
添加内容
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwt committed Nov 7, 2021
1 parent 7e9f4b2 commit 93686ee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Empty file.
78 changes: 78 additions & 0 deletions website/content/ChapterSix/NotNullargs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: 6.02 非空变量展开
type: docs
weight: 1
---

# 字符串展开

**${#parameter}**
foo="abcd"
echo ${#foo}
输出结果 4
该展开的意思是返回字符串的长度

**${parameter:m}**
foo="ab abc cde"
echo ${foo:5}
输出结果 c cde
该展开的意思是返回从位置为5至最末位的字符串,注意这里的位置类似于切片,位置0对应的是第一个a

**${parameter:m:n}**
foo="ab abc cde"
echo ${foo:5:4}
输出结果 c cd
该展开的意思是返回一个字符串,从位置为5开始,字符串长度为4

**${parameter#para}**
foo="file.txt.zip"
echo ${foo#*.}
输出结果 txt.zip
该展开的意思是匹配开头的字符串清除,但是这个匹配可能会是多种情况,比如*.可以匹配成file.也可以匹配成file.txt.,但这里取最短的匹配项即file.

**${parameter##para}**
foo="file.txt.zip"
echo ${foo##*.}
输出结果 zip
该展开的意思是匹配开头的字符串清除,但是这个匹配可能会是多种情况,比如*.可以匹配成file.也可以匹配成file.txt.,但这里取最长的匹配项即file.txt

**${parameter%para}**
foo="file.txt.zip"
echo ${foo%.*}
输出结果 file.txt
该展开的意思是匹配末尾的字符串清除,但是这个匹配可能会是多种情况,比如.*,可以匹配成.zip,也可以匹配成.txt.zip,这里取最短的匹配项即.zip

**${parameter%%para}**
foo="file.txt.zip"
echo ${foo%%.*}
输出结果 file
该展开的意思是匹配末尾的字符串清除,但是这个匹配可能会是多种情况,比如.*,可以匹配成.zip,也可以匹配成.txt.zip,这里取最短的匹配项即.txt.zip

**${parameter/para/string}**
foo="JPGJPG"
echo ${foo/JPG/jpg}
输出结果 jpgJPG
该展开的意思是匹配JPG,然后用jpg去替换,但是只替换第一个JPG

**${parameter//para/string}**
foo="JPGJPG"
echo ${foo//JPG/jpg}
输出结果 jpgjpg
该展开的意思是匹配JPG,然后用jpg去替换,且替换所有的JPG

**${parameter/#para/string}**
foo="JPGJPG"
echo ${foo/#JPG/jpg}
输出结果 jpgJPG
该展开的意思是匹配字符串JPG,但必须是字符串开头的JPG,然后用jpg替换

**${parameter/%para/string}**
foo="JPGJPG"
echo ${foo/%JPG/jpg}
输出结果 JPGjpg


<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.linuxwt.com/linuxwtbash/ChapterSix/Nullargs/">⬅️上一页</a></p>
<p><a href="https://books.linuxwt.com/linuxwtbash/NotNullargs/Jisuan">下一页➡️</a></p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ do
shift
done
```
因为bash里一切皆为字符,在比较纯数字时需要通过[[]]来使得里面的变量可以当作整数来使用,当然也可以使用(( init_long < long ))来代替
因为bash里一切皆为字符,在比较纯数字时需要通过[[]]来使得里面的变量可以当作整数来使用,当然也可以使用(( init_long < long ))来代替,wc -c计算的是字符串所占的字节数

------------------------------------------------
上面的脚本有一个地方是没有考虑到的,如果文件中存在多个与找出的字符串长度一样的字符串,就会存在bug,举个例子
Expand Down

0 comments on commit 93686ee

Please sign in to comment.