0

I created the bash script below to replace the SVN URL for working copies.

I can confirm this works perfectly on my Linux system however it does NOT work on my Mac OS system. I'll be able to provide the error I get back later but wondered if someone could point me in the right direction. I believe it is to do with the space in the WORKING_DIR variable, I have tried many variations found on Google including escaping the space \ adding " and ' but still no luck.

#!/bin/bash
filepath=$(pwd)
URL=https://192.168.22.225/svn
WORKING_DIR="/Users/user/Documents/Working Copies"
cd "${WORKING_DIR}"
for f in "${WORKING_DIR}"/*
do
        if [[ -d $f ]]; then
        (
        cd "${f##*/}"
        #printf "\n$PWD\n${URL}/${f##*/}\n"
        svn relocate "${URL}"/"${f##*/}"
        )
        fi
done
cd "$filepath"

Error:

: No such file or directoryments/Working Copies
'bash: working.sh: line 7: syntax error near unexpected token `do
'bash: working.sh: line 7: `do
Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
TSUK
  • 587
  • 3
  • 8
  • 22
  • 4
    Quote your variables. Read this: https://unix.stackexchange.com/q/171346/4667 – glenn jackman Sep 07 '17 at 14:21
  • Use [shellcheck.net](http://www.shellcheck.net) -- it'll point out the unquoted variables, and some other problems as well. In this case: always test `cd` commands to make sure they work, or the rest of your script will execute in the wrong place! – Gordon Davisson Sep 08 '17 at 06:27
  • 1
    Your script has DOS newlines. Run `dos2unix`, or open it in vim and run `:set fileformat=unix` and save. – Charles Duffy Sep 09 '17 at 14:53

1 Answers1

1

This is the giveaway:

'bash: working.sh: line 7: syntax error near unexpected token `do
'bash: working.sh: line 7: `do

See the ' at the front of the line? That's supposed to be printed at the end of the line.

What's sending the cursor back to the beginning of the line is a CR character, otherwise known as $'\r'. Thus, instead of do, you have do$'\r', and when the shell tries to print unexpected token `do', the CR sends the cursor to the beginning of the line, so the closing ' is printed there.

This happens because on DOS-style systems, newlines are two characters, CRLF, whereas on UNIX they're just CR.

Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
  • I did display and ensure the line endings were UNIX style. It wouldn't explain why the script works on my Linux box perfectly though but not my Mac OS X system... I will double check but I did replace the line endings just to be sure – TSUK Sep 12 '17 at 08:01
  • It absolutely does explain it: However you copied the script between platforms didn't preserve line endings. – Charles Duffy Sep 12 '17 at 10:27
  • Ran brew install unix2dos. Then used dos2unix file-to-convert. Thanks very much! – TSUK Sep 12 '17 at 17:27