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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |