0

I want to run perltidy before i look for diff in my subversion working copy. in svn config i wrote:

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

As David W said in this answer https://stackoverflow.com/a/5834900/1927848 i make a script /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

and when i make svn diff in working copy i got some errors:

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.

where is my errors?

UPD: $1 and $2 are not file names, $6 and $7 contains file names. i made some modifications to code, thanks to ikegami comment

#!/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

but now script only does first perltidy command and wait... whats wrong?
UPD2: perl script, that works:

#!/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;
Community
  • 1
  • 1
Suic
  • 2,441
  • 1
  • 17
  • 30
  • 1
    You simply need more robust argument handling. Looks like (at least) `-u` and `-l` are passed to the script in addition to the file names. – ikegami May 16 '13 at 07:55
  • @ikegami, thanks, updated my question. got other problem:( – Suic May 16 '13 at 08:32
  • Is either of the file names `-` or something that starts with `-`? – ikegami May 16 '13 at 08:55
  • svn file name `/tmp/tru/.svn/pristine/0d/0de49951070d7da521e9b26cde1853e6b66a665a.svn-base` contains `-`. but it doesnt matter already. i hate bash. i rewrote this script in perl, and it works fine. a love perl, thanks for your help – Suic May 16 '13 at 09:30
  • ug, you went back to fragile hard-coded arg nums and to using temporary files. You already got bitten by the former once, maybe even twice! – ikegami May 16 '13 at 09:35
  • i'll try to fix it now, thanks for critics – Suic May 16 '13 at 09:36
  • `/usr/bin/diff "$args[@]" \ <( /usr/local/bin/perltidy "$f1" -st ) \ <( /usr/local/bin/perltidy "$f2" -st )` does this command will work in perl? – Suic May 16 '13 at 09:41
  • huh? no. `/usr/` will look like a regex match, which Perl would expect to be followed by an operator, but `bin` is not an operator. (`Having no space between pattern and following word is deprecated at -e line 1. Bareword found where operator expected at -e line 1, near "/usr/bin" (Missing operator before bin?)`) – ikegami May 16 '13 at 20:08

1 Answers1

1

I'm not an experienced bash code, so the following may not be optimal, especially given the redundancy, but it solves your problem by assuming the last two args are the file names.

#!/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

Or if you don't care about the actual file names (as your update implies), you can avoid the temporary files altogether.

#!/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
ikegami
  • 367,544
  • 15
  • 269
  • 518