首页 > 解决方案 > 意外的行尾 - dos2unix 不会修复它

问题描述

我有以下代码:

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail
set -u

se () { sed -n 's/\s*r('"$1"').*|r| =\s\+//p'}

### gets the number of cell opt steps
exec 0<"DEMLIR.out"
while read -r line
 do
 gawk 'BEGIN{FS="OPTIMIZATION STEP:"} {print $2}' | tr -s " "
 done>results

 sed -i '/^$/d' results
 #sed -e 's/^[ \t]*//' results

 step=$(tail -n 1 results)
 echo "${step}"

### gets the number of steps in each geo_opt output
 for i in $(seq 1 $step)
 do
 exec 0<"DEMLIR-GEO_OPT-$i.out"
 while read -r line
 do
 gawk 'BEGIN{FS="OPTIMIZATION STEP:"} {print $2}' | tr -s " "
 done>results_geo_$i

 sed -i '/^$/d' results_geo_$i

 step_geo=$(tail -n 1 results_geo_$i)
 echo "${step_geo}"

### goes through each line in distance.out and prints distances to array 
 exec 0<"DEMLIR-GEO_OPT-$i-distance-1.coordLog"
 while read -r line
 do
 for j in $(seq 0 $step_geo)
 do
 "$line" | se
 paste -d' ' <(printf '%s\n' $j) <(se 1,5) <(se 2,5) <(se 2,8)

 done
 done

 done>DEMLIR_task.txt

我尝试运行这个程序,但我一直unexpected end of line error在第 53 行。我看到这个问题已经有了答案,但是当我尝试时dos2unix它说 dos2unix: converting file script_step.sh to Unix format... 但就是这样。当我再次运行代码时,它不会工作。

我预计最后一个 for 循环会出现一些错误,所以如果你看到一些错误,你也可以指出它们。但是,如果它们与原始问题无关,那么您可以忽略它们。

标签: linuxbashsyntax-error

解决方案


问题在这里:

se () { sed -n 's/\s*r('"$1"').*|r| =\s\+//p'}

在 . 之前需要一个分号或换行符}。否则,它被视为sed论点的一部分。


推荐阅读