Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Open Files in Unix

by swartzieee (Initiate)
on Oct 08, 2008 at 17:39 UTC ( #716052=note: print w/replies, xml ) Need Help??

in reply to Open Files in Unix

#!/usr/local/bin/perl print "Making Recommended Logging Changes\n"; #chdir "/opt/tivoli/itcam/was/DC/"; @logging=`find . -name`; chomp @logging; print @logging; foreach $prop (@logging) { open (FILE,"<$prop"); @array=<FILE>; close(FILE); foreach (@array) { print @array; $_ =~ s/DEBUG_MIN/WARN/g; } } ####### print "Disable Aspect J And Enable BCI\n"; chdir "/opt/tivoli/itcam/was/DC/runtime/"; @disable='find . -name'; foreach $props (@disable) { open (FILE, ">> $props"); @array1=<FILE>; foreach (@array1) { print FILE "\n"; print FILE " +=true"; $_ =~ s/ +ierFactory/; } close(FILE); } ####### print "Recycle Configured JVM's\n"; exit 0;

Replies are listed 'Best First'.
Re^2: Open Files in Unix
by Fletch (Chancellor) on Oct 08, 2008 at 18:09 UTC

    You still haven't fixed the quoting to backticks on your second find command like ikegami pointed out in the very first reply.

    Addendum: And if you bothered checking the return value from open on the next line and printing out the error message you might have noticed this yourself . . .

    Continuing: And trying to read from a filehandle you've opened for writing (well, we'll presume that you eventually correct the other errors to that point) with the line @array1 = <FILE> after the unchecked open is meaningless. You either need to open the file for reading and writing and do some seek magic to rewind it, or (more simply) write the updated contents to a new file and rename over the original.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Open Files in Unix
by blazar (Canon) on Oct 14, 2008 at 17:37 UTC

    I personally believe that shell scripts should be written in shell, and Perl scripts in perl. Now, the distinction is not always clear, and there are corner cases, but yours is well close to the first category, to say the least: in fact someone suggested you inside-out solutions, namely, instead of a Perl program running find(1) a find(1) command running perl, and perl may be substituted with sed(1) there.


    You're missing the single two most important lines for your program:

    use strict; use warnings;

    Others would add diagnostics to the list, but I regard it as "something more" whereas the former two are the very strictly close to fundamental, and I would only avoid them in onliners and in the simplest scripts.

    chomp @logging; print @logging;

    Do you really want to print them like that? It will look awful!

    open (FILE,"<$prop");

    Here and for the rest of your program, do a favour to yourself and use the three args form of open, possibly along with "lexical handles" (which will dispense you from the need of explicit close's) and most importantly, checking for success. If you're lazy and don't want to check individual open()'s, then you can use Fatal instead.

    foreach (@array) { print @array;

    Do you really want to print the same string for each element of @array? (What an awful name for a variable, BTW: it's neither descriptive nor short enough to be used instead of a descriptive one...) Sorry: I now see it won't print "the same string" for all elements, but indeed it is so strange a logic that got me confused, and this is the reason why I stroke the text before rather than simply deleting it. Anyway, clarity is in the eye of the beholder, they say...

    $_ =~ s/DEBUG_MIN/WARN/g; }

    This is a specific idiodincrasy of mine: while perfectly valid Perl, it doesn't make much sense in that you should either take full advantage of $_ being a pronoun, and the topicalizer, thus used implicitly and by default by many functions and operators or use the binding operator =~ on a generic variable.

    @disable='find . -name';

    Here, you're thinking of using the backticks as above, but you made a typo. In absence of the checks that I and others, suggested you, you won't even notice. Which is the reason why we suggested you to do them in the first place.

    foreach $props (@disable) { open (FILE, ">> $props"); @array1=<FILE>;

    This is the most serious error you've made thus far. You're opening a file for appending and then you're attempting to read from it.

    All in all, if I understand the logic you're after from your broken code, I presume you may be interested in the -i cli switch, or in the ^I special variable, or in reproducing their effects "manually," which you don't, as of now. So no surprise "it doesn't work:" you can't expect the effect of a s/// to propagate to a file only because you read the variable you're applying it to from that particular file.

    If you can't understand the incipit, then please check the IPB Campaign.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716052]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-01-19 01:53 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.