0

My bash script is not working properly, i don't know why

test.sh

#!/bin/bash

CLEAN_FILES=".slug-post-clean"


while read file; do
  [[ ! -n "${file}" ]] && continue

  echo $file

  echo "if [[ -d ${file}]]; then exists ";

  if [[ -d "${file}" ]]; then
    echo "file exists"
  fi

done < ${CLEAN_FILES}

.slug-post-clean

src
public
node_modules/.cache

output

src
]]; then exists
public
]]; then exists
node_modules/.cache
]]; then exists dules/.cache

But this code works

if [[ -d "src" ]]; then
  echo "if works"
fi

output

if works

My Ubuntu version is 20.04LTS

Anyone knows what's happening?

ReZ
  • 313
  • 1
  • 3
  • 13
  • When I copied your code, I get the following output: src if [[ -d src]]; then exists public if [[ -d public]]; then exists node_modules/.cache if [[ -d node_modules/.cache]]; then exists I ran it on my Arch Linux desktop, with `bash --version version 5.1.16(1)-release`. – Alex O'Connor May 19 '22 at 23:58
  • 4
    The file has CRLF line breaks. Fix it with `dos2unix`. – Barmar May 20 '22 at 00:07
  • 3
    See [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/q/39527571/4154375) and [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/4154375). – pjh May 20 '22 at 00:14
  • dos2unix doesn't works, i have the same output, i'm using linux subsystem for windows(WSL). – ReZ May 21 '22 at 01:03
  • my bash desktop version is ```version 5.0.17(1)-release```, Is there a way to update bash in windows? – ReZ May 21 '22 at 01:06

0 Answers0