コマンド出力が空であるか空の文字列であるかをテストする方法は? [閉鎖]

コマンド出力が空であるか空の文字列であるかをテストする方法は? [閉鎖]

以前の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

おすすめ記事