Permalink
Browse files

Fix cat foo|grep bar rewrite rule

  • Loading branch information...
1 parent 53b9d00 commit 85354c8dcc6f3e02dedc4e66d484ee83fdfb8cb1 @holdenk committed Sep 5, 2012
Showing with 4 additions and 3 deletions.
  1. +2 −2 Unicorn/Shell.pm
  2. +2 −1 t/shell.t
View
@@ -17,10 +17,10 @@ sub fix_shell {
# http://www.pixelbeat.org/programming/shell_script_mistakes.html
$rt =~ s/$\s*if\s*\[\s*\-e\s*(\w+?)\s*\-e\s*(\w+?)\s*\]\s*$/if [-e $1] || [-e $2]/;
#Double negative
- $rt =~ s/$\s*if\s*\[\s*\!\-z\s*(\"\$\w*\")\]/if [$1]/;
+ $rt =~ s/$\s*if\s*\[\s*\!\-z\s*(\"\$\w*\")\]\s*$/if [$1]/;
#Check for cat pipe to grep
#i.e cat foo | grep baz
- $rt =~ s/^\s*cat\s*(\w+)\s*\|\s*grep\s+([\w\"]+)\s*$/grep $2 < $1/
+ $rt =~ s/$\s*cat\s*(\w+)\s*\|\s*grep\s+([\w\"\\]+)\s*$/grep $2 < $1/
} else {
return 0;
}
View
@@ -1,10 +1,11 @@
#Test the shell script thing
-use Test::More tests => 4;
+use Test::More tests => 5;
use Unicorn::Shell qw( fix_shell check_shell);
is (fix_shell("fake","#!/bin/bash\nif [-e foo -e bar]"), "#!/bin/bash\nif [-e foo] || [-e bar]", "Expands -e works");
is (check_shell("fake","#!/bin/bash\nif [-e foo -e bar]"), 1, "Expands -e works");
is (fix_shell("fake","#!/bin/bash\nif [-e foo -f bar]"), "#!/bin/bash\nif [-e foo -f bar]", "Does not expand -e / -f");
is (fix_shell("fake","#!/bin/bash\nif [!-z \"\$foo\"]"), "#!/bin/bash\nif [\"\$foo\"]", "Removes double negatives");
+is (fix_shell("fake","#!/bin/bash\ncat foo|grep bar"), "#!/bin/bash\ngrep bar < foo", "Remove unecessary cat");
done_testing();

0 comments on commit 85354c8

Please sign in to comment.