![명령 출력이 비어 있는지 또는 빈 문자열인지 테스트하는 방법은 무엇입니까? [폐쇄]](https://linux55.com/image/44749/%EB%AA%85%EB%A0%B9%20%EC%B6%9C%EB%A0%A5%EC%9D%B4%20%EB%B9%84%EC%96%B4%20%EC%9E%88%EB%8A%94%EC%A7%80%20%EB%98%90%EB%8A%94%20%EB%B9%88%20%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%B8%EC%A7%80%20%ED%85%8C%EC%8A%A4%ED%8A%B8%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%ED%8F%90%EC%87%84%5D.png)
이전 SVN 상자에서 작동하도록 사전 커밋 후크 스크립트를 얻으려고 합니다. 매우 오래되었으며 Ubuntu Server 8.04를 실행합니다.
이 스크립트는 @echo off :: :: 빈 로그 메시지로 커밋을 중지합니다. ::
@echo off
setlocal
rem Subversion sends through the path to the repository and transaction id
set REPOS=%1
set TXN=%2
rem check for an empty log message
svnlook log %REPOS% -t %TXN% | findstr . > nul
if %errorlevel% gtr 0 (goto err) else exit 0
:err
echo. 1>&2
echo Your commit has been blocked because you didn't give any log message 1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2
exit 1
findstr 명령이 존재하지 않기 때문에 작동하지 않는 것 같습니다. 작동하는 것은 다음과 같습니다.
if [[ -n "" ]] ; then echo "yes"; else echo "no"; fi
그래서 스크립트를 다음과 같이 변경했습니다.
@echo off
::
:: Stops commits that have empty log messages.
::
@echo off
setlocal
rem Subversion sends through the path to the repository and transaction id
set REPOS=%1
set TXN=%2
rem check for an empty log message
::svnlook log %REPOS% -t %TXN% | findstr . > nul
::if %errorlevel% gtr 0 (goto err) else (goto exitgood)
::svnlook log %REPOS% -t %TXN% | findstr . > ""
::if %errorlevel% gtr 0 (goto err) else (goto exitgood)
SET LOG=`svnlook log %REPOS% -t %TXN%`
if [[ -n %LOG% ]]; then
(goto exitgood)
else
(goto err)
fi
:err
echo. 1>&2
echo Your commit has been blocked because you didn't give any log message 1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2
exit 1
:exitgood
exit 0
하지만 그것도 작동하지 않습니다. 둘 다 코드 255로 종료됩니다. 누군가 내가 뭘 잘못하고 있는지 말해 줄 수 있나요?
답변1
이는 MS Windows Batch와 마찬가지로 배치 스크립트입니다. 문자열 찾기Windows NT 4 리소스 키트에 도입되었습니다.
::
그리고 rem
댓글입니다. (또는 ::
실제로유효하지 않은이름).
아마도 Python에서 실행할 수 있지만 wine
cmd
일부 기본 스크립트(perl, python, bash 등)로 포팅하는 것이 더 좋습니다.
간단한 예:
#!/bin/bash
# Function to print usage and exit
usage()
{
printf "Usage: %s [repository] [transaction_id]\n" $(basename "$1") >&2
exit 1
}
# Check that we have at least 2 arguments
[ $# -ge 2 ] || usage
repo="$2"
trans_id="$2"
# Check that command succeed, and set variable "msg" to output
if ! msg="$(svnlook log "$repo" -t "$trans_id")"; then
printf "Check path and id.\n" >&2
usage
fi
# If msg is empty
if [ "$msg" = "" ]; then
printf \
"Your commit has been blocked because you didn't give any log message
Please write a log message describing the purpose of your changes and
then try committing again. -- Thank you.\n" >&2
exit 2
fi
# Else default exit AKA 0