将此处的文档传递给其他脚本
Pass here document to other script
我有一个脚本a.sh建立一个如下所示的文档
read -r -d "" message << EOF
line1
line2
line3
line4
EOF
然后我想将此消息(包含四行)传递给另一个程序,所以我做了
b.sh $message
但是在 b.sh 我做到了
reMess=
cat $reMess
它只显示第 1 行而不显示其余 3 行,谁能解释一下我应该怎么做才能实现这一点?
第二个问题我更改了 a.sh 中的代码,使消息存储在一个文件中,然后从 b.sh 中读取它,有人可以在 b.sh 中建议我如何读取这个文件请在此处的文档中。代码详情如下
a.sh
read -r -d "" message << EOF
line1
line2
line3
line4
EOF
echo "$message" > /tmp/message.txt
b.sh
read -r -d "" information << EOF
inforation1
$(cat /tmp/message.txt)
EOF
echo "$information"
它return我没有什么建议哪里错了?
感谢您的帮助
我看到您的代码的唯一问题是它不必要地复杂。
message='line1
line2
line3
line4'
information="information1
$message" # need double quotes rather than single to interpolate $message
如果您不喜欢嵌入的换行符,Bash 有 C 字符串语法。
message=$'line1\nline2\nline3\nline4'
但这确实没有任何好处,而且它妨碍了对其他 shell 的可移植性。您很快就会习惯多行字符串。
我有一个脚本a.sh建立一个如下所示的文档
read -r -d "" message << EOF
line1
line2
line3
line4
EOF
然后我想将此消息(包含四行)传递给另一个程序,所以我做了
b.sh $message
但是在 b.sh 我做到了
reMess=
cat $reMess
它只显示第 1 行而不显示其余 3 行,谁能解释一下我应该怎么做才能实现这一点?
第二个问题我更改了 a.sh 中的代码,使消息存储在一个文件中,然后从 b.sh 中读取它,有人可以在 b.sh 中建议我如何读取这个文件请在此处的文档中。代码详情如下
a.sh
read -r -d "" message << EOF
line1
line2
line3
line4
EOF
echo "$message" > /tmp/message.txt
b.sh
read -r -d "" information << EOF
inforation1
$(cat /tmp/message.txt)
EOF
echo "$information"
它return我没有什么建议哪里错了? 感谢您的帮助
我看到您的代码的唯一问题是它不必要地复杂。
message='line1
line2
line3
line4'
information="information1
$message" # need double quotes rather than single to interpolate $message
如果您不喜欢嵌入的换行符,Bash 有 C 字符串语法。
message=$'line1\nline2\nline3\nline4'
但这确实没有任何好处,而且它妨碍了对其他 shell 的可移植性。您很快就会习惯多行字符串。