# How to Resolve EOL Conversion Issues in MongoDB Cluster Docker Scripts

> Resolve EOL conversion issues in MongoDB Docker scripts. Use dos2unix or gitattributes to change CRLF to LF and prevent container initialization failures.

- Repository: [Jin/mongodb-cluster-docker-compose](https://github.com/minhhungit/mongodb-cluster-docker-compose)
- Tags: how-to-guide
- Published: 2026-03-07

---

**Convert Windows line endings (CRLF) to Unix format (LF) in the repository's shell scripts using `dos2unix`, editor settings, or `.gitattributes` to prevent MongoDB container initialization failures.**

The `minhhungit/mongodb-cluster-docker-compose` repository provides Docker Compose configurations for deploying a sharded MongoDB cluster. When working with this repository on Windows systems, **EOL conversion issues** frequently cause the cluster initialization scripts to fail because Docker containers expect Unix-style line endings (LF) while Windows editors often produce CRLF characters.

## Why EOL Conversion Issues Break MongoDB Cluster Initialization

### The CRLF vs LF Problem in Docker Containers

The repository contains several Bash entry-point scripts located in the `scripts/` directory, including [`scripts/entrypoint-shard01.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/scripts/entrypoint-shard01.sh), [`scripts/entrypoint-router.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/scripts/entrypoint-router.sh), and [`scripts/entrypoint-configserver.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/scripts/entrypoint-configserver.sh). These scripts execute inside Linux-based Docker containers during cluster startup.

When a script contains Windows line endings (CRLF), the shell interpreter treats the carriage return (`\r`) as part of the command text. This invisible character corrupts command parsing and prevents the MongoDB sharding setup from completing successfully.

### Specific Error Messages You'll Encounter

According to the repository's documentation in [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) (lines 37-41), CRLF issues produce syntax errors such as:

```

E QUERY    [thread1] SyntaxError: unterminated string literal @(shellhelp2)

```

The documentation explicitly warns Windows users about this issue (lines 49-53) and references `assets/EOL-unix-mode.png`, which illustrates the Notepad++ workflow for converting line endings.

## How to Identify Files with Windows Line Endings

Before applying fixes, verify which files contain CRLF characters. You can check using these methods:

```bash

# Using file command (Linux/macOS/WSL)

file scripts/*.sh

# Using cat with special characters visible

cat -A scripts/entrypoint-shard01.sh | head -n 5

```

Files with CRLF will show `^M` characters at line endings or report as "with CRLF line terminators."

## Methods to Resolve EOL Conversion Issues

### Convert Files Manually in Your Editor

For immediate, one-off fixes, use your code editor's line ending conversion feature:

**Notepad++:** Edit → EOL Conversion → Unix (LF)

**VS Code:** Click the "CRLF" indicator in the status bar (bottom-right), then select "LF" from the dropdown. Add these settings to [`.vscode/settings.json`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/.vscode/settings.json) for automatic enforcement:

```json
{
  "files.eol": "\n",
  "files.trimTrailingWhitespace": true
}

```

**Sublime Text:** View → Line Endings → Unix

### Bulk Convert with dos2unix

The `dos2unix` utility provides the most reliable method for converting multiple files simultaneously. This is the recommended approach for the `minhhungit/mongodb-cluster-docker-compose` repository.

Install and run:

```bash

# Ubuntu/Debian

sudo apt-get install dos2unix

# macOS with Homebrew

brew install dos2unix

# Convert all shell scripts in the repository

dos2unix scripts/*.sh

# Also convert JavaScript files if needed (PSA/scripts/*.js)

find . -type f \( -name "*.sh" -o -name "*.js" \) -print0 | xargs -0 dos2unix

```

### Configure Git to Handle Line Endings

Set Git's `core.autocrlf` configuration to prevent CRLF from entering the repository:

```bash

# On Windows (convert CRLF to LF on commit, checkout as CRLF)

git config --global core.autocrlf true

# On macOS/Linux (convert CRLF to LF on commit, checkout as-is)

git config --global core.autocrlf input

```

Note that this only affects your local environment and does not enforce standards for other team members.

### Add .gitattributes for Team-Wide Enforcement

Create a `.gitattributes` file in the repository root to permanently resolve EOL conversion issues across all clones and contributors:

```bash

# Create .gitattributes file

cat <<'EOF' > .gitattributes

# Enforce Unix line endings for shell scripts

*.sh text eol=lf

# Enforce LF for JavaScript files

*.js text eol=lf

# Enforce LF for Docker and Compose files

*.yml text eol=lf
*.yaml text eol=lf
Dockerfile text eol=lf
EOF

```

Commit and push this file:

```bash
git add .gitattributes
git commit -m "Add .gitattributes to enforce LF line endings"
git push origin main

```

Once committed, Git automatically converts files to LF on checkout, regardless of the contributor's operating system.

### Automate with Pre-Commit Hooks

For teams that cannot modify repository settings, implement a pre-commit hook to catch CRLF before commits:

```bash
#!/bin/sh

# .git/hooks/pre-commit

# Convert any staged *.sh files to LF before committing

git diff --cached --name-only --diff-filter=ACM | grep '\.sh$' | while read file; do
  if [ -f "$file" ]; then
    dos2unix "$file"
    git add "$file"
  fi
done

```

Make the hook executable:

```bash
chmod +x .git/hooks/pre-commit

```

This ensures that even if a developer saves a file with CRLF, the commit automatically converts it to LF.

## Summary

Resolving EOL conversion issues in the `minhhungit/mongodb-cluster-docker-compose` repository requires ensuring all shell scripts use Unix line endings (LF) rather than Windows line endings (CRLF):

- **Immediate fix:** Run `dos2unix scripts/*.sh` to convert existing files
- **Editor configuration:** Set VS Code or Notepad++ to use LF by default
- **Repository enforcement:** Add a `.gitattributes` file with `*.sh text eol=lf` to prevent CRLF from entering the codebase
- **Git configuration:** Set `core.autocrlf` to `input` (macOS/Linux) or `true` (Windows) for local protection

These steps prevent the `SyntaxError: unterminated string literal` errors that occur when Docker containers execute CRLF-corrupted scripts during MongoDB cluster initialization.

## Frequently Asked Questions

### What causes the "unterminated string literal" error in MongoDB Docker scripts?

The error occurs when shell scripts contain Windows line endings (CRLF) instead of Unix line endings (LF). When Docker containers running Linux execute these scripts, the carriage return character (`\r`) remains attached to commands, causing the MongoDB shell to misinterpret string literals and syntax. This is documented in the repository's [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) at lines 37-41, which specifically warns about this syntax error when line endings are not converted to Unix mode.

### How do I check if a script has CRLF or LF line endings?

You can identify line ending types using several methods. On Linux or macOS, run the `file` command: `file scripts/entrypoint-shard01.sh` will report "with CRLF line terminators" if Windows endings are present. Alternatively, use `cat -A scripts/entrypoint-shard01.sh` to display special characters—lines ending with `^M$` indicate CRLF, while `$` alone indicates LF. In VS Code, the status bar at the bottom right displays "CRLF" or "LF" and allows one-click conversion.

### Is adding a .gitattributes file better than using dos2unix?

Both approaches serve different purposes and work best together. The `.gitattributes` file provides **permanent, team-wide protection** by forcing Git to checkout files with LF line endings regardless of the developer's operating system. This prevents CRLF from ever entering the working directory. However, `.gitattributes` only affects files checked out from Git—it does not fix files already saved with CRLF on your local disk. Therefore, run `dos2unix scripts/*.sh` first to convert existing files, then commit a `.gitattributes` file with `*.sh text eol=lf` to prevent future occurrences. This two-step approach resolves current issues and prevents them for all future contributors.