开发者

perl + set parameter correctly in the syntax

I use the following find command to rename directories & files in Linux system

remark: see rename.pl script down below

    find  /  -name 'host1' -print0 | xargs -0 /var/tmp/rename.pl 'prin开发者_JS百科t "changing $_\n"; s/host1/host_10/g'

in order to set parameter in place host1 name I set the following

OLD_HOST=host1

Example:

    find  /  -name "$OLD_HOST"  -print0 | xargs -0 /var/tmp/rename.pl 'print "changing $_\n"; s/$OLD_HOST/host_10/g'

the problem is that now after setting the $OLD_HOSTS (as s/$OLD_HOST/host_10/g' )

it doesn't replace host1 with host_10

my question: how to use correctly $OLD_HOST in the syntax in order to replace host1 with host_10 ?

#

rename.pl script:

 #!/usr/bin/perl
 #
 # rename script examples from lwall:
 #       rename 's/\.orig$//' *.orig
 #       rename 'y/A-Z/a-z/ unless /^Make/' *
 #       rename '$_ .= ".bad"' *.f
 #       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

 $op = shift;
  for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
  }


It's not very clear what you are trying to do.

I guess you are setting

OLD_HOST=host1

in the shell and you are trying to get the value of $OLD_HOST env variable from a Perl script. If so you can make use of

$ENV{'OLD_HOST'} 

as:

find  /  -name "$OLD_HOST"  -print0 | xargs -0 /var/tmp/rename.pl 'print "changing $_\n"; s/$ENV{OLD_HOST}/host_10/g'
                                                                                            ^^^^^^^^^^^^^^
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜