You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下批量执行mkrun命令时here-document报错,求解决方案

解决方案

问题根源

你遇到的错误是因为here-document的结束标记END必须顶格书写(不能有任何前导空格或缩进),而脚本里的END前面带有缩进,导致shell无法识别它作为结束标记,直到文件末尾都没找到匹配项,因此报错。

方法1:修正结束标记的缩进

将循环内的END改为顶格书写,同时给文件名变量加双引号,避免文件名含空格或特殊字符时出错:

#!/bin/sh
run_type=4
run_mode=2
mod_type=3
for additional_file in *.txt
do
  mkrun << END
$run_type
$run_mode
$mod_type
$(cat "$additional_file")
END
done

方法2:用<<-END忽略前导Tab缩进

如果想保持脚本缩进美观,可使用<<-END(注意减号),这样结束标记END前的Tab缩进会被忽略(空格不行),需把循环内的缩进替换为Tab:

#!/bin/sh
run_type=4
run_mode=2
mod_type=3
for additional_file in *.txt
do
	mkrun <<-END
	$run_type
	$run_mode
	$mod_type
	$(cat "$additional_file")
	END
done

注意:此处缩进必须是Tab字符,不能用空格代替。

方法3:优化输入传递方式(更简洁)

无需使用here-document,通过命令拼接直接传递输入,彻底避免缩进问题:

#!/bin/sh
run_type=4
run_mode=2
mod_type=3
for additional_file in *.txt
do
  { echo "$run_type"; echo "$run_mode"; echo "$mod_type"; cat "$additional_file"; } | mkrun
done

内容的提问来源于stack exchange,提问作者pro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:21:23