'bash:意外标记附近的语法错误`do

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46098690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:26:21  来源:igfitidea点击:

'bash: syntax error near unexpected token `do

bash

提问by TSUK

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

我创建了下面的 bash 脚本来替换工作副本的 SVN URL。

I can confirm this works perfectly on my Linux systemhowever 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.

我可以确认这在我的 Linux 系统上运行良好,但它在我的 Mac OS 系统上不起作用。我将能够提供我稍后返回的错误,但想知道是否有人可以指出我正确的方向。我相信这与 WORKING_DIR 变量中的空间有关,我尝试了在 Google 上发现的许多变体,包括转义空间 \ 添加 " 和 ' 但仍然没有运气。

#!/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

回答by Charles Duffy

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 endof 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.

将光标发送回行首的是一个 CR 字符,也称为$'\r'. 因此,do您有, 而不是do$'\r', 并且当 shell 尝试打印时unexpected token `do', CR 将光标发送到行的开头,因此在'那里打印结束。

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

这是因为在 DOS 风格的系统上,换行符是两个字符,CRLF,而在 UNIX 上它们只是 CR。