-
Notifications
You must be signed in to change notification settings - Fork 1
/
bat2sh.pl
53 lines (49 loc) · 1.13 KB
/
bat2sh.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/perl
sub cat_ {
local *F;
open F, $_[0] or return;
my @l = <F>;
wantarray() ? @l : join '', @l
}
sub output {
my $f = shift;
local *F;
open F, ">$f" or die "output in file $f failed: $!\n";
print F foreach @_; 1
}
if (@ARGV != 2) {
die "Usage: bat2sh.pl <input .bat file> <output .sh file>\n";
}
my @contents = cat_($ARGV[0]);
foreach my $line (@contents) {
# rem -> #
$line =~ s/rem /# /i;
# move -> mv
$line =~ s/^\s*move\b/mv/;
# del -> rm
$line =~ s/^\s*del\b/rm/;
# dos eol to unix eol
$line =~ s/\r\n/\n/;
# set
$line =~ s/^\s*set //i;
# \ -> /
$line =~ s/\\/\//g;
# copy -> cp
if ($line =~ m/^\s*copy\b/ig){
$line =~ s/^\s*copy\b/cp/ig;
$line =~ s/\n//; #remove eol
$line .= " ./\n";
}
# %varName% -> $varName
while ($line =~ m/(%\w*%)/ig){
my $word = "\$" . substr($1, 1, -1);
$line =~ s/(%\w*%)/$word/ig;
}
# if ERRORLEVEL 1 goto ERROR -> if [ "$?" = "1" ]; then sub_error fi
if ($line =~ m/^\s*if\s*ERRORLEVEL\s*1\s*goto\s*(\w*)\s*$/ig){
$line = "if [ \"\$?\" = \"1\" ]; then \n exit 1\nfi\n";
}
}
my $line = "#!/bin/sh\n\n\n";
unshift @contents, $line;
output $ARGV[1], @contents;