0

What is the problem with the following bash code ?

#!/bin/bash
if [ -z "$1" ]; then
  echo "Usage: $0 {file}"
fi

Generates an error of "Unexpected EOF". Cygwin 2.10.0(0.325/5/3) in Windows 10.

Tickseeker
  • 141
  • 1
  • 8

1 Answers1

1

You have DOS line endings in your file, which means the bash parser sees

if [ -z "$1" ]; then\r
  echo "Usage: $0 {file}"\r
fi\r

Rather than a complete if statement, it sees the beginning of one, one whose condition consists of (so far) the commands [ -z "$1" ], then\r, echo "Usage: $0 {file}"\r, and fi\r. The parser is still looking for the then keyword to terminate the condition list, but finds the end of the file instead.

Save your script as a POSIX text file using \n as the line endings, not \r\n.

chepner
  • 497,756
  • 71
  • 530
  • 681