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

5
  • while executing "for i in *.DIF; do mv $i ${i/DIF/SUC}; done;" on shell works fine. Commented Sep 8, 2014 at 8:19
  • Why don't you just use the perl rename function? Commented Sep 8, 2014 at 8:33
  • Why does your script have SUC, but the output has DIFFF? Commented Sep 8, 2014 at 8:33
  • 1
    stackoverflow.com/questions/15179446/… Commented Sep 8, 2014 at 8: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. Commented Sep 8, 2014 at 9:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

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;';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.