bash文件返回意外令牌'$'do \ r' [英] bash file returns unexpected token `$'do\r''

查看:79
本文介绍了bash文件返回意外令牌'$'do \ r'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线找到了该脚本并尝试使用它:

I found this script online and tried to use it:

#!/bin/sh
# Target directory
TARGET=$3
echo "Copying to $TARGET"
for i in $(git diff --name-only $1 $2)
    do
        # First create the target directory, if it doesn't exist.
        mkdir -p "$TARGET/$(dirname $i)"
        # Then copy over the file.
        cp "$i" "$TARGET/$i"
    done
echo "Done";

我已经在线验证了脚本,脚本还可以.我也尝试过以各种方式更改它,但是它对我不起作用.

I've validated the script online, and the script is okay. I've also tried to change it in various ways, but it doesn't work for me.

我还尝试过运行类似的内容:

I've also tried running something like:

#!/bin/sh
# Target directory
TARGET=$3
echo "Copying to $TARGET"
for i in $(ls)
do
    echo "text"
done

我仍然遇到相同的错误:

And I still get the same error:

./git-copy.sh: line 6: syntax error near unexpected token `$'do\r''
'/git-copy.sh: line 6: `do

那是为什么?

推荐答案

您的脚本已经在基于DOS或Windows的系统上进行了编辑,并且包含Linux/Unix不喜欢的回车符(\r是) .您可以使用dos2unix将回车的行尾转换为正确的格式.如果您没有dos2unix,则可以使用awk之类的

Your script has been edited on a DOS or Windows based system and contains carriage-return characters that Linux/Unix does not like (that what \r is). You could use dos2unix to convert the carriage return line endings to the correct format; if you don't have dos2unix you might use awk like

awk '{ sub("\r$", ""); print }' git-copy.sh > git-copy2.sh
mv git-copy2.sh git-copy.sh

这篇关于bash文件返回意外令牌'$'do \ r'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆