bash 中的多行赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11234577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiline assignment in bash
提问by Stepan Yakovenko
In .cmd files on windows I do:
在 Windows 上的 .cmd 文件中,我这样做:
SET JARS=^
./lib/apache-mime4j-0.6.jar;^
./lib/apache-mime4j-0.6.jar;^
./lib/bsh-1.3.0.jar;^
./lib/cglib-nodep-2.1_3.jar;^
./lib/commons-codec-1.6.jar;^
./lib/commons-collections-3.2.1.jar;^
./lib/commons-exec-1.1.jar;^
./lib/commons-io-2.0.1.jar;^
./lib/commons-io-2.3.jar;
How can I do such multiline assignment in shell?
如何在 shell 中进行这样的多行分配?
采纳答案by Steven Penny
The question implicitly requests single line output, as I will show.
这个问题隐含地要求单行输出,正如我将展示的。
test.bat
测试.bat
@SET JARS=^
./lib/apache-mime4j-0.6.jar;^
./lib/apache-mime4j-0.6.jar;^
./lib/bsh-1.3.0.jar;^
./lib/cglib-nodep-2.1_3.jar;^
./lib/commons-codec-1.6.jar;^
./lib/commons-collections-3.2.1.jar;^
./lib/commons-exec-1.1.jar;^
./lib/commons-io-2.0.1.jar;^
./lib/commons-io-2.3.jar;
@echo %JARS%
Output
输出
c:\home\Steven\Desktop>test.bat
./lib/apache-mime4j-0.6.jar;./lib/apache-mime4j-0.6.jar;./lib/bsh-1.3.0.jar;./li
b/cglib-nodep-2.1_3.jar;./lib/commons-codec-1.6.jar;./lib/commons-collections-3.
2.1.jar;./lib/commons-exec-1.1.jar;./lib/commons-io-2.0.1.jar;./lib/commons-io-2
.3.jar;
test.sh
测试文件
JARS=\
'./lib/apache-mime4j-0.6.jar;'\
'./lib/apache-mime4j-0.6.jar;'\
'./lib/bsh-1.3.0.jar;'\
'./lib/cglib-nodep-2.1_3.jar;'\
'./lib/commons-codec-1.6.jar;'\
'./lib/commons-collections-3.2.1.jar;'\
'./lib/commons-exec-1.1.jar;'\
'./lib/commons-io-2.0.1.jar;'\
'./lib/commons-io-2.3.jar;'
echo "$JARS"
Output
输出
$ ./test.sh
./lib/apache-mime4j-0.6.jar;./lib/apache-mime4j-0.6.jar;./lib/bsh-1.3.0.jar;./li
b/cglib-nodep-2.1_3.jar;./lib/commons-codec-1.6.jar;./lib/commons-collections-3.
2.1.jar;./lib/commons-exec-1.1.jar;./lib/commons-io-2.0.1.jar;./lib/commons-io-2
.3.jar;
回答by ghoti
So many ways to skin this cat.
给这只猫剥皮的方法有很多。
JARS='
./lib/apache-mime4j-0.6.jar;
./lib/apache-mime4j-0.6.jar;
./lib/bsh-1.3.0.jar;
./lib/cglib-nodep-2.1_3.jar;
./lib/commons-codec-1.6.jar;
./lib/commons-collections-3.2.1.jar;
./lib/commons-exec-1.1.jar;
./lib/commons-io-2.0.1.jar;
./lib/commons-io-2.3.jar;
'
This gets you multiline input in a variable, per your question.
根据您的问题,这可以让您在变量中进行多行输入。
But if you're planning to USE these files in a shell script, you need to tell us how, so that we can come up with appropriate answers, rather than making us guess. For use in a shell script, files need to be delimited by something useful.
但是,如果您打算在 shell 脚本中使用这些文件,则需要告诉我们如何使用,以便我们得出合适的答案,而不是让我们猜测。要在 shell 脚本中使用,文件需要用有用的东西分隔。
You asked, "How can I do such multiline assignment in shell", but the assignment in your example is actually a SINGLE line, with the ^at the end of each input line negating the following newline (notescaping it, as another answer suggested).
你问,“我怎么能在 shell 中做这样的多行分配”,但你的例子中的分配实际上是一个单行,^在每个输入行的末尾否定以下换行符(不是转义它,正如另一个答案所建议的那样) .
My solution in this answer is multiline, but you'll need to explain more about what you need this for in order to determine what will be useful.
我在这个答案中的解决方案是多行的,但是您需要更多地解释您需要它的用途,以确定哪些是有用的。
For example, if you need to step through a list of files that will be processed with the jarcommand, you might want to have something like:
例如,如果您需要遍历将使用该jar命令处理的文件列表,您可能需要如下内容:
#!/bin/sh
JARS='
./lib/apache-mime4j-0.6.jar
./lib/bsh-1.3.0.jar
...
'
set $JARS
for jarfile in "$@"; do
jar xf "$jarfile" ...
done
回答by matcheek
or alternatively
或者
SOMEVAR=$( cat <<EOF
value1
value2
value3
value4
value5
EOF
)

