1

I get that perl thinks the part of the string with $ are perl variables. What I'm trying to do with the perl call, is redefine variables preceded by a $ as \$, but that's throwing other errors. I think I have an issue with arguments being in the wrong order.

The code I have is:

perl -0pE "use strict; my $repo = "\$repo"; my $arch = "\$arch"; 
s|\rsync:\/\/[^\]]+|"${linesToWrite}"|"

The compiler warning I'm receiving is Can't use global $! in "my" at -e line 1, near "-p destination: $!"

I found plenty of documentation detailing on how to deal with it in a perl doc, but not much about the solution for running into problems in bash.

I tried doing this, but that just broke everything:

perl ""my $repo = "\$repo"; my $arch = "\$arch"" -0pE "use strict; 
s|\"rsync:\/\/[^\]+|"${linesToWrite}"|" 

How would I properly format this to keep perl from striping the '$' out of strings, and getting it to compile with use strict turned on?

5
  • have you tried \Q$ ?
    – Rahul
    Jun 16, 2016 at 6:15
  • 1
    Use single quotes for you perl command. In double quotes things like $repo get interpreted by the shell before perl sees it.
    – jordanm
    Jun 16, 2016 at 6:17
  • Yeah, that got it to compile. It's still giving me issues since I have a bash variable in there I need interpretted by the shell first.
    – ZeroPhase
    Jun 16, 2016 at 6:27
  • can you post what issue you are getting now ?
    – Rahul
    Jun 16, 2016 at 6:33
  • What are you actually trying to do? That seems overly complicated. Why use strict in a one liner? Why do you have two variables you never use? We might be able to help you better if you give us more details.
    – terdon
    Jun 16, 2016 at 9:51

2 Answers 2

1

Assuming you just want the shell variables $repo $arch and $linesToWrite expanded, put them in double quotes and the rest in single quotes. Quoted strings next to each other make a single string ("a"'b' is ab). (I don't know what you are doing with the backslashes in the substitute command, so I left them, but you probably don't want them).

perl -0pE 'use strict; my $repo = "'"$repo"'"; my $arch = "'"$arch"'"; 
s|\rsync:\/\/[^\]]+|'"${linesToWrite}"'|'
1
  • I'm going to try to keep those $repo and $arch. Ok, it works now it was having a space between the two string that caused the later errors.
    – ZeroPhase
    Jun 16, 2016 at 6:56
0

Perl has access to exported shell variables through the %ENV hash. It would probably be much simpler to use that. Something like:

$ export repo arch linesToWrite
$ perl -0pE 'use strict; my $repo = $ENV{"repo"}; my $arch = $ENV{"arch"}; 
             s|\rsync:\/\/[^\]]+|$ENV{"linesToWrite"}|'

I don't understand what you're trying to do with that expression though. Are you really trying to match \r (line feed, is this a file coming from Windows?) or is that rsync? Why are you escaping the / if you're using | as a separator? Also, there's not really much reason to use strict in one liners. I would probably simplify the whole thing to:

 perl -0pE '$repo = $ENV{"repo"}; $arch = $ENV{"arch"}; 
         s|\rsync://[^\]]+|$ENV{"linesToWrite"}|'

Or, even better, forget the $repo=$ENV{"repo"} stuff and just use $ENV{"repo"}, $ENV{"arch"} etc. directly.

4
  • I'm using it as a script for updating the powerpill rsync servers used weekly. The names in the servers have the $repo and $arch values. I'm guessing internally they're used for downloading packages of different repos and architectures. I'm just updating them weekly since last week an issue with the rsync servers prevented me from updating my system.
    – ZeroPhase
    Jun 16, 2016 at 9:55
  • @ZeroPhase OK, then are you sure you want \rsync and not rsync? And really, if you tell us what you're doing in more detail, show us what you're trying to parse, I'm sure we can give you a simpler approach.
    – terdon
    Jun 16, 2016 at 10:24
  • I'm updating the rsync servers used to update the package database. Here's the script I have so far. gist.github.com/Zerophase/3dfc16f9593b884b5b2627a84344d873
    – ZeroPhase
    Jun 16, 2016 at 22:44
  • @ZeroPhase OK, then this is certainly more complex than you need. I'm still having trouble understanding though since your input file doesn't actually contain the string rsync:// that you're trying to match. Anyway, I'd be happy to help you simplify this. If you're interested, give me a ping in /dev/chat.
    – terdon
    Jun 17, 2016 at 9:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .