Multiline assignment in bash

懵懂的女人 提交于 2019-12-08 16:54:17

问题


In .cmd files on windows I do:

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?


回答1:


The question implicitly requests single line output, as I will show.

test.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;



回答2:


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.

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 (not escaping it, as another answer suggested).

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 jar command, you might want to have something like:

#!/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



回答3:


or alternatively

SOMEVAR=$( cat <<EOF
value1
value2
value3
value4
value5
EOF
)


来源:https://stackoverflow.com/questions/11234577/multiline-assignment-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!