从多行here文档阅读Bash的变量 [英] Reading a Bash variable from a multiline Here Document

查看:104
本文介绍了从多行here文档阅读Bash的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是的完美的方式从一个猛砸这里读文件为多变量?

看我做这件事的方式如何返回的副作用 1 $? ..


猛砸code:

 #!/斌/庆典printf的注销... \\ N
未设置变量
printf的退出状态:%S \\ n$?
printf的变量:%S \\ n$ {}变量printf的\\ nprintf的设置为'富'... \\ N
变量='富'
printf的退出状态:%S \\ n$?
printf的变量:%S \\ n$ {}变量printf的\\ nprintf的从进程替换读取标准输入设置为'巴'... \\ n
读变量< ≤(回声'巴')
printf的退出状态:%S \\ n$?
printf的变量:%S \\ n$ {}变量printf的\\ nprintf的通过阅读从标准输入字符串在这里... \\ N为'巴兹'
here_string ='巴兹'
阅读变量和LT;<< $ {} here_string
printf的退出状态:%S \\ n$?
printf的变量:%S \\ n$ {}变量printf的\\ nprintf的通过阅读从标准输入文件在这里... \\ N为'QUUX'
阅读变量和LT;< - EOF
QUUX
EOF
printf的退出状态:%S \\ n$?
printf的变量:%S \\ n$ {}变量printf的\\ nprintf的设置为轰的一声<&换行符GT;砰'从这里阅读文档标准输入... \\ n
阅读-d''变量<< - EOF
轰的一声
轰的一声
EOF
printf的退出状态:%S \\ n$? #ONE?!一!oneone? :)
printf的变量:%S \\ n$ {}变量


输出

 取消设置...
退出状态:0。
变量:设置为'富'...
退出状态:0。
变量:foo中。设置为'巴'从进程替换读取标准输入...
退出状态:0。
变量:酒吧。设置为巴兹从这里串读标准输入...
退出状态:0。
变量:巴兹。设置为QUUX从这里文档阅读标准输入...
退出状态:0。
变量:QUUX。设置为轰的一声<&换行符GT;轰的一声'从这里读取标准输入
文件...
退出状态:1。
变量:轰的一声
轰的一声。


解决方案

返回1​​,当它到达EOF,在同一时间分配看最后的结果。这就是为什么不以换行符结尾的文件是有问题的,因为一个简单的循环读将不会运行的最后一行的循环体。

当您使用读-d''没有把一个NULL字节到输入,这就好比读取文件的最后一行。问题是,为什么你会永远想在这种情况下,返回0?它这样做就像它应该的。还要注意的是here文档(和herestrings)总是一个换行符添加到自动结束。

有些关联:

What is the perfect way to read a multiline variable from a Here Document in Bash?

Look at how my way of doing it has the side-effect of returning 1 as $?...


Bash code:

#!/bin/bash

printf "Unsetting...\n"
unset variable
printf "Exit status: %s.\n" "$?"
printf "Variable: %s.\n" "${variable}"

printf "\n"

printf "Setting to 'foo'...\n"
variable='foo'
printf "Exit status: %s.\n" "$?"
printf "Variable: %s.\n" "${variable}"

printf "\n"

printf "Setting to 'bar' by reading stdin from process substitution...\n"
read variable < <(echo 'bar')
printf "Exit status: %s.\n" "$?"
printf "Variable: %s.\n" "${variable}"

printf "\n"

printf "Setting to 'baz' by reading stdin from Here String...\n"
here_string='baz'
read variable <<< "${here_string}"
printf "Exit status: %s.\n" "$?"
printf "Variable: %s.\n" "${variable}"

printf "\n"

printf "Setting to 'quux' by reading stdin from Here Document...\n"
read variable <<- 'EOF'
quux
EOF
printf "Exit status: %s.\n" "$?"
printf "Variable: %s.\n" "${variable}"

printf "\n"

printf "Setting to 'thud <newline> thud' by reading stdin from Here Document...\n"
read -d '' variable <<- 'EOF'
thud
thud
EOF
printf "Exit status: %s.\n" "$?" # ONE?!one!!oneone? :)
printf "Variable: %s.\n" "${variable}"


Output

Unsetting...
Exit status: 0.
Variable: .

Setting to 'foo'...
Exit status: 0.
Variable: foo.

Setting to 'bar' by reading stdin from process substitution...
Exit status: 0.
Variable: bar.

Setting to 'baz' by reading stdin from Here String...
Exit status: 0.
Variable: baz.

Setting to 'quux' by reading stdin from Here Document...
Exit status: 0.
Variable: quux.

Setting to 'thud <newline> thud' by reading stdin from Here
Document...
Exit status: 1.
Variable: thud
thud.

解决方案

read returns 1 when it reaches EOF, at the same time as assigning the last result read. This is why files that don't end with a newline are problematic, because a simple loop with read will not run the loop body for the final line.

When you use read -d '' without putting a NUL byte into the input, this is just like reading the last line of a file. The question is why would you ever want read to return 0 in this case? It's doing just like it should. Also note that heredocs (and herestrings) always add a newline to the end automatically.

Somewhat related:

这篇关于从多行here文档阅读Bash的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆