![문자열을 정규식과 일치시키고 캡처링 그룹을 사용하여 조건부로 작업을 수행하는 방법](https://linux55.com/image/101013/%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%84%20%EC%A0%95%EA%B7%9C%EC%8B%9D%EA%B3%BC%20%EC%9D%BC%EC%B9%98%EC%8B%9C%ED%82%A4%EA%B3%A0%20%EC%BA%A1%EC%B2%98%EB%A7%81%20%EA%B7%B8%EB%A3%B9%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EC%A1%B0%EA%B1%B4%EB%B6%80%EB%A1%9C%20%EC%9E%91%EC%97%85%EC%9D%84%20%EC%88%98%ED%96%89%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
여기서 아이디어는 모든 git 저장소의 원격을 http에서 ssh로 변경하는 것입니다.
find / -type d -name '.git' 2>/dev/null | xargs -I {} $SHELL -c \
'cd $(dirname {}) && echo $(pwd) > /tmp/log.log && git remote | \
perl -ne "if (`git config --get remote.$_.url` =~ m#https://(.*)/(username.*)#){`git remote remove $_ && git remote add $_ git\@$1:$2`}"
내가 원하는 것은 내 모든 (perl 정규식의 사용자 이름) 저장소를 찾아서 http 대신 ssh를 사용하도록 전환하는 것입니다. Perl 스크립트를 테스트했는데 잘 작동하지만 명령에서 사용하면 다음과 같이 출력됩니다.
fatal: No such remote: remote syntax error at -e line 1, near "( =~" syntax error at -e line 1, near ";}" Execution of -e aborted due to compilation errors. xargs: /bin/zsh: exited with status 255; aborting
답변1
나는 당신이 원하는 것이 무엇인지 (정확히 예상되는 명령이 무엇인지) 잘 모르겠지만 다음과 같습니다.
printf "%s\n" 'https://github.com/username/reponame.git' \
'[email protected]:username/reponame' | perl -lne \
'if (m#https://(.*?)/(.*/)#) {print "git remote remove $_ && git remote add $_ git\@$1:$2"}'
인쇄해야 함
git remote remove https://github.com/username/reponame.git && git remote add https://github.com/username/reponame.git [email protected]:username/
(확실히 명령을 실행하려면 print
다음으로 변경하세요.)system
입력 URL을 Perl의 표준 입력으로 변경했습니다 for r in xyz
. 명령줄에 입력하려면 이렇게 하면 됩니다.
perl -le '$_=shift; if (m#http://(.*?)/(.*/)#) {print "blah $_ $1:$2"}' http://foo.bar/user/something
명령줄 인수를 입력합니다( 를 사용하여 다른 것을 지정하지 않는 한 $_
암시적으로 사용됩니다 ).m//
$var =~ m//
@
또한 배열 변수의 각인이므로 문자열에서 이스케이프 하는 것이 좋습니다 .