我一直在谷歌上搜索这个问题无济于事。我正在工作中自动化构建过程,我要做的就是获取版本号和可能是多行的构建的微小描述。它运行的系统是 OSX 10.6.8。
我已经看到了从使用 CAT 到根据需要处理每一行的所有内容。我不知道我应该使用什么以及为什么。
read -d '' versionNotes
Run Code Online (Sandbox Code Playgroud)
如果用户必须使用退格键,则会导致输入乱码。也没有终止输入的好方法,因为 ^D 不会终止,而 ^C 只是退出进程。
read -d 'END' versionNotes
Run Code Online (Sandbox Code Playgroud)
有效...但如果需要退格键,输入仍然会出现乱码。
while read versionNotes
do
echo " $versionNotes" >> "source/application.yml"
done
Run Code Online (Sandbox Code Playgroud)
没有正确结束输入(因为我来不及查找空字符串的匹配项)。
poi*_*ige 28
man bash
提到«…
命令替换 $(cat file) 可以替换为等效但速度更快的 $(< file)。
……»
$ myVar=$(</dev/stdin)
hello
this is test
$ echo $myVar
hello this is test
$ echo "$myVar"
hello
this is test
Run Code Online (Sandbox Code Playgroud)
我同意这是值得一提的 -echo "$myVar"
会按照给出的方式显示输入。
voi*_*ces 15
尝试这个:
user@host:~$ read -d '' x <<EOF
> mic
> check
> one
> two
> EOF
Run Code Online (Sandbox Code Playgroud)
没有换行符:
user@host:~$ echo $x
mic check one two
Run Code Online (Sandbox Code Playgroud)
有换行符:
user@host:~$ echo "$x"
mic
check
one
two
Run Code Online (Sandbox Code Playgroud)
你有几种方法。
最简单的方法之一是:
MYVAR=$(yourcommand)
echo $"MYVAR"
Run Code Online (Sandbox Code Playgroud)
例如:
MYVAR=$(ls /)
echo $"MYVAR"
Run Code Online (Sandbox Code Playgroud)