if or die!

Well, it's die this time round, I guess.

Your re-wording is bad, since it doesn't provide any further information. Here's my rephrasing, from what I can read from your code. Please correct that and post your correction.

Hello monks,

I have to munge shell scripts.

I have a list of files collected in a file called 'sys.sh'. These shell script files contain a case...esac statement, which I have to check for the occurrence of a 'bgs' case. If it is not in there, I want to write that case statement right after the 'syslogd' case branch. I open each file for output, then for input, and try to get at the relevant cases. I have difficulties getting at the relevant lines in those files and don't know how to change a complete case branch in one go. What am I missing?

to which I would respond:

See the documentation for open, or read perlopentut. If you open a file for output, you clobber it. A subsequent open for input just opens an empty file. You might want to use inplace edit, open it for update (+<) or write to a temporary file which you rename to the original after having closed the filehandle for writing.

You could slurp in the file in one go and do a match like /('bgs\).*?;;)/s to have the complete bgs case branch in $1 - read perlretut and perlre if things are unclear here.

You could also go through each file line by line, in which case you have to carry state variables you set and unset at the beginning and end of those case branches. You could weed out any eventual 'bgs' branches (by just ignoring them) and print your new 'bgs' branch right after the 'syslogd' branch, or before the closing 'esac' if it hasn't already been printed.

update:

One possible solution (if I understand rightly what you want to achieve):

foreach my $sh (@sh_f) { local $/ = ''; # slurp mode open IN, '<', $sh # really? or is it "$cf_dir/$sh" ? or die "Can't open '$sh' for input: $!\n"; open OUT, '>', "$cf_dir/$sh.tmp" or die "Can't write '$cf_dir/$sh.tmp': $!\n"; $_ = <IN>; s/^\s*'bgs'\).*?;;//msg; s/^(\s*'syslogd'\).*?;;/$1\n$bgs/ms; print OUT; close OUT or die "Can't close filehandle OUT properly: $!\n"; rename "$cf_dir/$sh.tmp", $sh; # if appropriate }

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: if or die! by shmem
in thread if or die! by wcj75019

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.