To prevent from destroying the symlinks, I found the solutions, but they did not work. Should it be applied to Perl or Bash/Shell?
In the continuation of Is it bad to use the conditions for find
ing the directories and to cd
multiple specific folders of a found directory to call the function?, I would like to preserve the symlnks, but I am not sure if it should be applied to Perl or to Bash/Shell. The guys gave me the solutions, but I tested and it did not work.
My original:
for f in *svg; do
case "$color" in
a)
perl arrogrin.pl "$f" > tmpFile && mv tmpFile "$f"
;;
black)
perl black.pl "$f" > tmpFile && mv tmpFile "$f"
;;
esac
done
The file arrogin.pl
:
#!/bin/perl
my $replacement=<<EoF;
<linearGradient id="grad" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color: #dd9b44; stop-opacity: 1" />
<stop offset="100%" style="stop-color: #ad6c16; stop-opacity: 1" />
</linearGradient>
EoF
## This is just to fix SE's syntax highlighting /
my $foundSvg = 0;
while (<>)
{
## Insert the replacement after the 1st line matching '<svg'
if (/<\s*svg/)
{
$foundSvg++;
}
if ($foundSvg == 1)
{
## $_ is the value of the current line. If we have found the <svg,
## append $replacement to this line
$_ .= $replacement;
## Increment $foundSvg so we don't do this twice
$foundSvg++;
}
## For all lines, replace all occurrences of #5c616c with url(#grad1)
s/#5c616c/url(#grad)/g;
## Print the line
print;
}
The file black.pl
:
#!/bin/perl
use strict; use warnings;
while (<>)
{
## For all lines, replace all occurrences of #5c616c with another colour
s/#5c616c/#404747/g
## Print the line
print;
}
The guys gave me the solutions, both almost worked, both preserved the symlinks, but they made all them unusable. Here are:
fool.pl
:
#!/usr/bin/env perl
use strict;
use warnings;
for my $arg (@ARGV)
{
$arg = readlink $arg if -l $arg;
}
# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";
while (readline)
{
s/#5c616c/#8bbac9/g;
print;
}
And I modified the original:
for f in *svg; do
case "$color" in
a)
perl arrogrin.pl "$f" > tmpFile && mv tmpFile "$(readlink -f "$f")"
;;
black)
perl black.pl "$f" > tmpFile && mv tmpFile "$(readlink -f "$f")"
;;
esac
done
Observations: I would like to replace Perl files for Bash files, because Bash offers --in-place --follow-symlinks
, but I am not sure I can something similar in Bash to that in Perl.