linux - 带有 cat EOF 问题的嵌套 for 循环
问题描述
这是我的代码,我有两个文件,其中一个包含文件位置,另一个包含输入名称。我想创建一个用于合并这两个文件的 for 循环。我在这里试过
#!/bin/bash -l
x=`cat x_file_locations`
y=`cat y_list`
for i in $x
do
for j in $y
do
cat << EOF >> ./algo/job_$j.sh
#!/bin/bash -l
#
#$ -N $i
#$ -cwd
#$ -pe smp 10
#$ -l mem_free=20G
algo --input_se $i --output $j --threads=10
EOF
done
done
我收到这个错误
line 24: warning: here-document at line 10 delimited by end-of-file (wanted `EOF')
line 25: syntax error: unexpected end of file
我究竟做错了什么?
解决方案
当你这样做时<< EOF
,整行必须EOF
没有空格,也不能制表符,不能在EOF
字符串之前或之后。
做:
cat << EOF >> ./algo/job_$j.sh
#!/bin/bash -l
#
#$ -N $i
#$ -cwd
#$ -pe smp 10
#$ -l mem_free=20G
algo --input_se $i --output $j --threads=10
EOF
^ no spaces after
^ no spaces in front
或者,您可以使用<< -EOF
前导选项卡(并且仅限选项卡!)将被忽略。
笔记:
- 不要使用反引号`。使用
$(..)
intead。https://wiki-dev.bash-hackers.org/scripting/obsolete for i in cat
是一种反模式。如何在 bash 中逐行读取文件。
在我的情况下,我将如何使用 while read 来逐行读取文件?
while IFS= read -r x; do
while IFS= read -r y; do
something something
done < y_list
done < x_file_locations
推荐阅读
- xamarin.forms - 如何为 Xamarin 配置 log4net
- amazon-mws - 如何使用亚马逊 MWS API 从卖家 ID 中获取卖家名称
- java - @Cacheable 请求字段的条件缓存
- db2 - 获取一列具有不同值的记录,同时在多条记录时替换另一列的值
- javascript - 如何使用 AngularJs 在 Django 中更新 Restfull api 列表
- c# - 使用 GraphAPI / C# 在 AzureAD 中删除应用程序角色的问题
- angular - Angular innerHTML 无法识别 HTML 标签
- jdbc - 如何将 Google Data Studio 连接到 AWS Athena?
- angular - 每当关闭浏览器选项卡/窗口时,Angular 应用程序变得不可用
- select - 在 DB2 中为 type1 或 type2 选择前 5 行