

shellscript 中操作多行字符串变量
source link: https://aimuke.github.io/linux/2020/11/06/linux-shell-multi-line/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

shellscript 中操作多行字符串变量
当我们在 shell 的 bash 里操作多行内容的字符串,我们往往会想到普通的字符串处理办法 例如:
1
2
string="Hello linux"
echo $string
其实 bash 提供了一个非常好的解决办法,就是 “Multi-line”
e.g. 包含变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cat > myfile.txt <<EOF
this file has $variable $names $inside
EOF
# 注入文档到 myfile.txt
cat myfile.txt
#输入:
#this file has
variable="ONE"
names="TWO"
inside="expanded variables"
cat > myfile.txt <<EOF
this file has $variable $names $inside
EOF
#print out the content of myfile.txt
cat myfile.txt
#输入:
#this file has ONE TWO expanded variables
不注入变量值
PS: 引用符号 “EOF” 决定是否需要输入变量
1
2
3
4
5
6
7
cat > myfile.txt <<"EOF"
this file has $variable $dollar $name $inside
EOF
cat myfile.txt
#得到
#this file has $variable $dollar $name $inside
转义 dollar “$” 符号,bash将取消变量的解析
1
2
3
4
5
6
7
cat > myfile.txt <<EOF
this file has $variable \$dollar \$name \$inside
EOF
cat myfile.txt
# 得到
# this file has $variable $dollar $name $inside
将多行文本赋值到变量
例1: 变量注入
1
2
3
4
5
6
7
8
read -d '' stringvar <<-"_EOF_"
all the leading dollars in the $variable $name are $retained
_EOF_
# 输入变量
echo $stringvar;
# all the leading dollars in the $variable $name are $retained
例2:直接定义,换行符被删除,引号含义变化
1
2
3
4
5
6
7
8
9
VARIABLE1="<?xml version="1.0" encoding='UTF-8'?>
<report>
<img src="a-vs-b.jpg"/>
<caption>Thus is a future post on Multi Line Strings in bash
<date>1511</date>-<date>1512</date>.</caption>
</report>"
echo $VARIABLE1
# <?xml version=1.0 encoding='UTF-8'?> <report> <img src=a-vs-b.jpg/> <caption>Thus is a future post on Multi Line Strings in bash <date>1511</date>-<date>1512</date>.</caption> </report>
注意: 定义变量的时候,version 后面的 1.0 是有引号的,输出后,引号没了
例3:使用cat 原样保留了原定义的数据
1
2
3
4
5
6
7
8
9
10
11
VARIABLE2=$(cat <<EOF
<?xml version="1.0" encoding='UTF-8'?>
<report>
<img src="a-vs-b.jpg"/>
<caption>Thus is a future post on Multi Line Strings in bash
<date>1511</date>-<date>1512</date>.</caption>
</report>
EOF
)
echo $VARIABLE2
#<?xml version="1.0" encoding='UTF-8'?> <report> <img src="a-vs-b.jpg"/> <caption>Thus is a future post on Multi Line Strings in bash <date>1511</date>-<date>1512</date>.</caption> </report>
同上,输出内容与原定义内容相同
1
2
3
4
5
6
7
8
VARABLE3=`cat <<EOF
<?xml version="1.0" encoding='UTF-8'?>
<report>
<img src="a-vs-b.jpg"/>
<caption>Thus is a future post on Multi Line Strings in bash
<date>1511</date>-<date>1512</date>.</caption>
</report>
EOF`
1
2
3
4
5
6
tee aa.txt << EOF
echo "Hello World 20314"
EOF
cat aa.txt
#echo "Hello World 20314"
例如10:
1
2
3
4
5
6
sudo sh -c "cat > /aaa.txt" <<"EOT"
this text gets saved as sudo - $10 - ten dollars ...
EOT
cat /aaa.txt
#this text gets saved as sudo - $10 - ten dollars ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat << "EOF" | sudo tee /aaa.txt
let's count
$one
two
$three
four
EOF
cat /aaa.txt
#let's count
#$one
#two
#$three
#four
关于 tee
1
2
3
4
5
6
7
8
9
10
11
12
> tee –help
Usage: tee [OPTION]… [FILE]…
Copy standard input to each FILE, and also to standard output.
-a, –append append to the given FILEs, do not overwrite
-i, –ignore-interrupts ignore interrupt signals
–help display this help and exit
–version output version information and exit
If a FILE is -, copy again to standard output.
Report tee bugs to [email protected]
GNU coreutils home page:
General help using GNU software:
For complete documentation, run: info coreutils ‘tee invocation’
References
Recommend
-
115
-
32
README.md Setup an iOS project environment
-
53
一、前言 通过创建 textarea 标签,并且指定其 rows 和 cols 属性,就可以创建一个多行文本输入框。 但是当输入的内容超过指定的 rows 之后,就会出现滚动条,如果用户想要查看全部内容,那就必须来回的拖动滚动条。而且这个滚动条只有在用
-
54
-
11
来自公众号:新世界杂货铺 前言 呼,这篇文章的准备周期可谓是相当的长了!原本是想直接通过源码进行分析的,但是发现TLS握手流程调试起来非常不方便,笔者怒了,于是实现了一个极简的 net.Conn 接口以...
-
8
【笨問題】JavaScript 的多行字串常數寫法-黑暗執行緒寫 JavaScript 遇到多行文字常數,我的直覺寫法是玩加號接接樂,像是這樣: <!DOCTYPE html> <html> <head> <style> fieldset { wi...
-
7
Python中使用re.MULTLINE匹配一个字符串的多行 2019-09-07 17:42:05 +08 字数:785 标签: Python 有时会遇到这种类型的需求:在一...
-
11
使用 CSS 实现多行省略漂洋过海来看你IT俱乐部-码出人生使用 CSS 实现多行省略Dec 17, 2020CSS6
-
5
在Python中定义一个函数时,就会把变量空间划分为全局变量(global)与局部变量(local),如果是定义在一个类的成员函数中,那么就还有额外的成员变量(self)空间。那么,如果在实际操作中,想把这几种不同的变量空间做一个分离的话,有没有办法呢? 读取和...
-
6
Shell中变量、字符串、数组、参数的技巧 2023-12-13 3 分钟阅读 变量子串 ${var} 返回变量var的内容,单独使用时有没有{}一样,混合多个变量和常量时,用{}界定变量名 ${#var} 返回变量var内容的...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK