Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Would there be an easier way to do this in perl?
#!/bin/tcsh #1. Create a file of all disk images from the Netbackup catalog that N +etbackup thinks is on disk. echo "Creating list of all images that Netbackup thinks is on disk." cp /dev/null catall grep "F:\\backup" cat.txt | sed -e "s/F:\\backup\\/\*/g" | awk -F\* '{ +print "F:\\backup\\" $2}' >> catall grep "G:\\backup" cat.txt | sed -e "s/G:\\backup\\/\*/g" | awk -F\* '{ +print "G:\\backup\\" $2}' >> catall grep "H:\\backup" cat.txt | sed -e "s/H:\\backup\\/\*/g" | awk -F\* '{ +print "H:\\backup\\" $2}' >> catall #2. Create file of what's on disk echo "Create file listing of all images on disk." cp /dev/null dirall grep "_" Fdir.txt | awk '{print "F:\\backup\\" $5}' > dirall grep "_" Gdir.txt | awk '{print "G:\\backup\\" $5}' >> dirall grep "_" Hdir.txt | awk '{print "H:\\backup\\" $5}' >> dirall #3. sort the files echo "Sorting the two files" sort catall > catall.srt sort dirall > dirall.srt #4. check em echo "Checking the differences." diff -u dirall.srt catall.srt |grep "^-" |grep -v "\-\-\-" |sed -e s/\ +-//g > delfile.list #5. create batch file echo "Creating a batch file to delete the files that Netbackup doesn't + know about." awk '{print "del " $1}' delfile.list > delfile.bat

Retitled by g0n from 'tcsh'.

Replies are listed 'Best First'.
Re: Listing Files in a Directory (Translation From Shell Script)
by graff (Chancellor) on Sep 07, 2005 at 23:16 UTC
    Perl vs. tcsh?? No contest in this case. The perl version will be a lot easier and make more sense. (Run time is probably not an issue, but I expect it will run faster than the shell script + batch file.)
    #!/usr/bin/perl use strict; my %files; # load %files with names of all images on disk for my $disk (qw/F G H/) { if ( open( I, $disk."dir.txt" )) { while (<I>) { next unless ( /_/ ); my $name = "$disk:\\backup\\" . ( split )[4]; $files{$name} = undef; } } else { warn "unable to open dir.txt for $disk: -- $!\n"; next; } } # eliminate hash elements in %files if Netbackup thinks they are on di +sk if ( open( I, "cat.txt" )) { while (<I>) { if ( /([FGH]:\\backup\\)/ ) { my $path = $1; s/\Q$path\E/*/g; my $name = $path . ( split /\*/ )[1]; delete $files{$name}; } } } else { # best to stop here if that last open failed: die "open failed for cat.txt: $!"; } # delete files that are left in the hash unlink for ( keys %files );
    (I hoped to say the perl version would be shorter, but that would depend on how you measure script length -- the perl script above is more lines, but fewer characters compared to your shell script, and it does not require the creation of a separate batch file to be run at some other time. And of course, the perl could be shortened to fewer lines.)
      It's the error checking that makes it longer, error checking that wasn't present in the original script. It's hard to complain that it's longer if one gets more functionality and reliability :)
Re: Listing Files in a Directory (Translation From Shell Script)
by Tanktalus (Canon) on Sep 07, 2005 at 22:59 UTC

    There would be an easier way to do that in sed or awk!

    #1 ... sed -e "s/.*([FGH]):\\backup\\/\1:\\backup\\ /" < cat.txt > catall #2 ... # (bourne shell syntax, sorry) for d in F G H; do awk '/_/{print "'$d'":\\backup\\" $5 } < ${d}dir.txt done > dirall #3 ... could have done this above by piping through sort. #4 & 5 ... diff -u dirall.srt catall.src | sed -e '/---/d' -e '/^[^-]/d' -e 's/-/ +/g' -e 's/^/del /' > delfile.bat

    But, to answer your question, yes, using some hashes, you could figure this out and even unlink them all directly without a batch file.

Re: Listing Files in a Directory (Translation From Shell Script)
by Fletch (Bishop) on Sep 07, 2005 at 22:52 UTC

    Wow, I actually upvoted an AM post . . . (the who cares one)

    Yes you could do that in perl, but the questions to ask are:

    • Is it working now?
    • Do you have enough shell knowledge to maintain it?
    • Does it need to do anything different?

    If it's "yes yes no" then if it's not broke . . . . Otherwise go right ahead and rewrite it.

Re: Listing Files in a Directory (Translation From Shell Script)
by Anonymous Monk on Sep 07, 2005 at 22:45 UTC
    Who cares?