0

I want to rename *.DIF files to *.SUC files But the following script is giving "sh: bad substitution" error I cannot use "rename" becuase my OS is not Linux.

$com="for i in *.DIF; do mv \$i \${i/DIF/SUC}; done;";
print $com."\n";
print `$com`;

Output :

for i in *.DIF; do mv $i ${i/DIF/SUC}; done;

sh: bad substitution

Arjun Bora
  • 439
  • 2
  • 8
  • 20
  • while executing "for i in *.DIF; do mv $i ${i/DIF/SUC}; done;" on shell works fine. – Arjun Bora Sep 08 '14 at 08:19
  • Why don't you just use the perl `rename` function? – Barmar Sep 08 '14 at 08:33
  • Why does your script have `SUC`, but the output has `DIFFF`? – Barmar Sep 08 '14 at 08:33
  • 1
    http://stackoverflow.com/questions/15179446/bash-bad-substitution-when-using-code-in-sh-file – mpapec Sep 08 '14 at 08:40
  • 2
    backticks means `run with sh`. But really. Don't do this. Cross contaminating scripting languages is an excellent way to build yourself some horrific bugs and unmaintainable code. Stick to one language - perl or shell, doesn't matter which. – Sobrique Sep 08 '14 at 09:09

3 Answers3

1

If you are having problems with Perl's rename, use File::Copy, a platform-independent module for moving and copying files. It is a core module, so it should be already part of your Perl installation.

If the system command works when you enter it in the shell, but not in Perl, the most likely reason is that Perl isn't using the shell you expect. We would need more information about your system to be sure, though.

1

There's no need to shell out for file operations that you can easily do within Perl.

The following renames all of your .dif extension files as .suc.

use strict;
use warnings;

use File::Copy qw(move);

move($_, s/dif$/suc/r) for glob('*.dif');
Miller
  • 34,962
  • 4
  • 39
  • 60
0

be default perl was using sh, instead of bash, which allows {//} this question helped. Finally I used :

open my $bash_handle, '| bash' or die "Cannot open bash: $!";
print $bash_handle 'for i in *.SUC; do mv $i ${i/SUC/DIF}; done;';
Community
  • 1
  • 1
Arjun Bora
  • 439
  • 2
  • 8
  • 20