-1

so in a bash script, have a while loop that read lines from the outpt of some pippelined sort commands. i get an error: line 13: syntax error near unexpected token 'done' line 13 is the last line, where the "done" and the pipeline is. my script:

#!/bin/bash

while read -a line; do
  if [[ ${line[2]} < $1 ]]; then
    continue
  fi
  if [[ $2 != -MM ]]; then
    if [[ ${line[3]} = N ]]; then
        continue
    fi
  fi
  echo -n "${line[0]} "
  echo ${line[1]}
  done < <(sort -b rooms | sort -sk2 | sort -sk4 | sort -sk3)

tnx.

doronbs11
  • 15
  • 1
  • 4
  • The syntax looks ok to me (for bash, not for other shells, so don't run it with `sh scriptname`). Is it possible the script has DOS/Windows line endings [like this question](https://stackoverflow.com/questions/31886144/why-is-a-shell-script-giving-syntax-errors-when-the-same-code-works-elsewhere)? – Gordon Davisson Aug 12 '18 at 02:36

2 Answers2

0

I would first try to write the date to temp so I can see what the sorting is doing by inspecting the temp file. Then read that in line by line.

#!/usr/bin/env bash

TMPF="/tmp/tmp-file.dat"
sort -b rooms | sort -sk2 | sort -sk4 | sort -sk3 > "${TMPF}"

while IFS= read -r line ;do
  if [[ ${line[2]} -ge $1 ]] && [[ $2 != -MM ]] && [[ ${line[3]} = N ]]; then
    echo -n "${line[0]} "
    echo ${line[1]}
  fi
done < "${TMPF}"
Mike Q
  • 6,716
  • 5
  • 55
  • 62
-1

You can use without using ()

like done < rooms |sort -b | sort -sk2 | sort -sk4 | sort -sk3