0

我想在我的颠覆工作副本中寻找差异之前运行 perltidy。在 svn 配置中我写道:

diff-cmd = /usr/bin/d.sh

正如大卫 W 在这个答案中所说的那样https://stackoverflow.com/a/5834900/1927848我制作了一个脚本 /usr/bin/d.sh:

#!/usr/local/bin/bash
/usr/local/bin/perltidy "$1" > "/tmp/$1"
/usr/local/bin/perltidy "$2" > "/tmp/$2"
/usr/bin/diff "$1" "$2"
/bin/rm "/tmp/$1" "/tmp/$2"
exit 0

当我在工作副本中制作 svn diff 时,我遇到了一些错误:

dev# svn diff
Index: nodeny/new_month.pl
===================================================================
Unknown option: u
Error on command line; for help try 'perltidy -h'
Option l is ambiguous (libpods, line-up-parentheses, logfile, logfile-gap, long-block-line-count, look-for-autoloader, look-for-hash-bang, look-for-selfloader)
Error on command line; for help try 'perltidy -h'
diff: option requires an argument -- L
/usr/bin/diff: Try `/usr/bin/diff --help' for more information.

我的错误在哪里?

UPD:$1 和 $2 不是文件名,$6 和 $7 包含文件名。感谢ikegami评论,我对代码做了一些修改

#!/usr/local/bin/bash

/usr/local/bin/perltidy "$6" -st > "/tmp/tidy001"
/usr/local/bin/perltidy "$7" -st > "/tmp/tidy002"
/usr/bin/diff "/tmp/tidy001" "/tmp/tidy002"
/bin/rm "/tmp/tidy001" "/tmp/tidy002"
exit 0

但现在脚本只执行第一个 perltidy 命令并等待......有什么问题?
UPD2:perl 脚本,有效:

#!/usr/bin/perl

use Text::Diff;

if (-e $ARGV[-2] && -e $ARGV[-1]) {
    my $str1 = `/usr/local/bin/perltidy -npro -pbp -nst -se -et=4 -bar -l=200 $ARGV[-2] -st`;
    my $str2 = `/usr/local/bin/perltidy -npro -pbp -nst -se -et=4 -bar -l=200 $ARGV[-1] -st`;
    my $diff = diff(\$str1, \$str2);
    print $diff;
}
else {
    print "Error file $ARGV[-2] or $ARGV[-1] not exists\n";
}
exit 0;
4

1 回答 1

1

我不是一个经验丰富的bash代码,所以以下可能不是最佳的,特别是考虑到冗余,但它通过假设最后两个 args 是文件名来解决你的问题。

#!/bin/bash

args=("$@")

f1_idx=$(( ${#args[@]} - 2 ))
f1="${args[$f1_idx]}"
/usr/local/bin/perltidy "$f1" -st > "/tmp/$f1"
args[$f1_idx]="/tmp/$f1"

f2_idx=$(( ${#args[@]} - 1 ))
f2="${args[$f2_idx]}"
/usr/local/bin/perltidy "$f2" -st > "/tmp/$f2"
args[$f2_idx]="/tmp/$f2"

/usr/bin/diff "$args[@]"
/bin/rm "/tmp/$f1" "/tmp/$f2"

exit 0

或者,如果您不关心实际文件名(正如您的更新所暗示的那样),您可以完全避免使用临时文件。

#!/bin/bash

args=("$@")

last_idx=$(( ${#args[@]} - 1 ))
f2="${args[$last_idx]}"
unset args[$last_idx]

last_idx=$(( ${#args[@]} - 1 ))
f1="${args[$last_idx]}"
unset args[$last_idx]

/usr/bin/diff "$args[@]" \
   <( /usr/local/bin/perltidy "$f1" -st ) \
   <( /usr/local/bin/perltidy "$f2" -st )

exit 0
于 2013-05-16T08:46:47.850 回答