本文介绍了Bash脚本,git和空格的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用bash脚本自动化一些与git的交互。要做到这一点,我有一个脚本,将SSH和列出所有的存储库,然后克隆它们。部分脚本显示在这里

  DIRS =`ssh $ uls $ p`; $ DIR $中的
;做
echossh:// $ u $ p $ DIR
echogit clone ssh:// $ u $ p $ DIR;
git clone ssh:// $ u $ p $ DIR
完成

示例参数为

  -u用户@主机

  -p / cygdrive / c / Documents \ and\设置/ somePath 

这对于名称中带有空格的文件路径来说很好,直到git clone部分关于太多空间的错误。如果我复制并粘贴由

  echogit clone ssh:// $ u $ p $ DIR回显的部分; 

然后克隆就好了。我试图用引号括起来,但是它引发了一个错误。

 看起来不是git仓库

我做错了什么?

编辑1



使用时的错误克隆ssh:// $ u $ p $ DIR

 致命:'/ cygdrive / c / Documents \ and\ Settings / somePath / repo1'似乎不是一个git仓库
致命:远程端挂了出乎意料


解决方案

我使用了和答案找到一个解决方案,以及对。



正如Gilles我避免使用 ls ,并且我使用Antak建议使用 IFS = $'\\\
'
避免在换行符中使用空格和分隔参数的多个参数。



我最终解决的代码是

  DIRS =`ssh$ u<< ENDSSH 
cd$ p
eval'for file in *;做echo \\ $文件;完成'
ENDSSH`

IFS = $'\ n'
用于DIR中的$ DIRS;
do
echossh:// $ u $ p / $ DIR
done
unset IFS


I am trying to automate some of my interactions with git using bash scripts. To do so I have a script that will ssh and list all the repositories and then clone them. Part of the script is shown here

DIRS=`ssh $u "ls $p"`;
for DIR in $DIRS; do
    echo "ssh://$u$p$DIR"
    echo "git clone ssh://$u$p$DIR";
    git clone ssh://$u$p$DIR
done

Example arguments are

-u user@host

and

-p /cygdrive/c/Documents\ and\ Settings/somePath

This work fine for filepaths with spaces in their names up until the git clone part where it throws an error about too many spaces. If I copy and pasted the part echoed by

echo "git clone ssh://$u$p$DIR";

then it clones just fine. I have tried to surround this in quotes but it throws an error

does not appear to be a git repository

What am I doing wrong?

Edit 1

The errors when using git clone "ssh://$u$p$DIR" is

fatal: '/cygdrive/c/Documents\ and\ Settings/somePath/repo1' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
解决方案

I've used information from Gilles's and Antak's answer to find a solution as well as some other posts on running ssh on a remote machine and using variables in a bash heredoc.

As suggested by Gilles I avoided using ls and I've used Antak's suggestion of using IFS=$'\n' to avoid multiple arguments from spaces and split arguments on line breaks.

The final code I've settled on is

DIRS=`ssh "$u" << ENDSSH
    cd "$p"
    eval 'for file in *; do echo \\$file; done'
ENDSSH`

IFS=$'\n'
for DIR in $DIRS;
    do
    echo "ssh://$u$p/$DIR"
done
unset IFS

这篇关于Bash脚本,git和空格的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:04