in reply to if or die!

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}

Replies are listed 'Best First'.
Re^2: if or die!
by wcj75019 (Acolyte) on Mar 02, 2008 at 17:55 UTC
    Dear Shmem, I like that. It is simple. Thanks.
    #!/usr/bin/perl -w use strict; my $agent = "best1agent_start"; my $bgs=<<HERE; 'bgs') /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect -q>>\$LOG 2>>\$LOG ;; HERE my $collect = " /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect -q>>\$LOG 2>>\$LOG\n" +; my $cf_dir = "/root/sysedge/cf_files"; #my $cf_dir = "/export/home/f358118/sysedge"; open SH_FILES, "sys.sh" or die "Error: $!"; my @sh_f=<SH_FILES>; close SH_FILES; 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 }
      Unmatched ( in regex; marked by <-- HERE in m/^( <-- HERE \s*'syslogd'\).*?;;/ at b.t line 27

      Yeah, closing paren missing. Oops :-)

      s/^(\s*'syslogd'\).*?;;)/$1\n$bgs/ms; # missing here --------^

      --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}
      Update See shmem's note (it's not the escaped paren): It means that you have escaped the closing paren of your capture... as did shmem... and thus have an open paren without a close.

      Please, please: learn to proofread your code, rather than merely asking for that which you then cargo-cult. And please read the relevant documents or texts.

        "Cargo Cult"...I didn't think anyone else had ever read that from Feynman!

        Love it!

        UPDATE: It's one of my all-time favorite stories,

        UPDATE (again): Sorry...really messed up the tags.

        ack Albuquerque, NM
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: if or die!
by wcj75019 (Acolyte) on Mar 02, 2008 at 20:12 UTC
    Thank You, Thank You, Thank You!