Sponsored Content
Full Discussion: du alternative in perl
Top Forums Shell Programming and Scripting du alternative in perl Post 302233882 by jim mcnamara on Monday 8th of September 2008 05:31:30 PM
Old 09-08-2008
du depends heavily on stat - which is POSIX. So writing C code is failry portable,
along with the dirent.h functions

perl implements stat, so that's cake. The other needed calls: closedir, opendir, and readdir to get file names to stat. Both of those are in perl as well.... go for it.

Perl by Example has an example getting all the file names in a directory.

This does something else with stat data as entry-level example code for the forums here, but you can clean it up for your use:

Code:
#!/usr/bin/perl 
    use strict;
	sub numeric;
	sub to_date;
	my @filelist = ();
	my @sortarray = ();
	my $idx = 0;
	my $value = "";
	my $file = "";
	opendir(DIR, "..") || die "can't open $!\n";
	@filelist = readdir(DIR);
	closedir(DIR);

	$idx = 0;
	@sortarray = ();
	foreach $file ( @filelist )
	{
		$sortarray[$idx] = sprintf("%d %s", (stat "../$file")[9], $file);
		$idx++;
	}

	@filelist = sort numeric @sortarray ;
    print "@filelist\n";
	foreach $value ( @filelist )
	{
		print &to_date( $value );
	}

	sub numeric { my @one = split(' ', $a);
		          my @two = split(' ', $b); 
		          return $one[0] <=> $two[0];
		        }
	sub to_date {
		 my @months = ();
		 my $filename = "";
		 my $mtime = 0;
		 my $sec = 0; 
		 my $min = 0; 
		 my $hr = 0;
		 my $mday = 0; 
		 my $mon = 0; 
		 my $yr = 0; 
		 my $wday = 0; 
		 my $yday = 0; 
		 my $dntcare =0;
		 my $retval = "";
		 
		 @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

         ($mtime, $filename) = split(' ', $_[0]);	     
	     ($sec, $min, $hr, $mday, $mon, $yr, $wday, $yday, $dntcare) =
	         localtime($mtime);
	     $retval = sprintf( "%-10s %3s %02d %4d %02d:%02d:%02d\n",
	         $filename, $months[$mon], $mday, $yr + 1900, $hr, $min, $sec );

         return $retval;
	}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

alternative for mailx

hi i am struggling with mailx command, my command is mailx -a attachment.txt -s "hi this is subject " my_name@domain.com (Interrupt -- one more to kill letter) the control doesn't comes out automatically, i have to press CTRL + C .. why is it happening so ? (1 Reply)
Discussion started by: achneaz1
1 Replies

2. Shell Programming and Scripting

help with while loop or any other alternative?

i=1 while do mm=02 dd=03 yy=2008 echo "$mm$dd$yy" i=$(( i+1)) echo "$i" done whenever i execute the script above i will get the error below: syntax error at line 30: `i=$' unexpected (3 Replies)
Discussion started by: filthymonk
3 Replies

3. Shell Programming and Scripting

Alternative for Cron

Hi... I want to know whether if there is any alternative for cron.:confused: I had written a script which checks for all system/application processes every 15 min(placed in cron though). But looks funny - what if cron daemon isn't running!! and expecting that script to update the OUTPUT FILE... (5 Replies)
Discussion started by: reddybs
5 Replies

4. HP-UX

alternative for egrep -o on HP-UX

Hello to all board members!! I have a problem on a HP-UX system. I should write a script. Therefore I need to search after IP addresses in the output of a command. On Debian this works: ifconfig | egrep -o "{1,3}\.{1,3}\.{1,3}\.{1,3}" The script where i need this is not ifconfig, but... (2 Replies)
Discussion started by: vostro
2 Replies

5. Shell Programming and Scripting

Alternative for wc -l

Hi techies .. This is my first posting hr .. Am facing a serious performance problem in counting the number of lines in the file. The input files i get will be in some 10 to 15 Gb of size or even sometimes more ..and I will load it to db I have used wc -l to confirm whether the loader... (14 Replies)
Discussion started by: rajesh_2383
14 Replies

6. Solaris

vi alternative

Is there any other editor, installed by 'default' in Sparc Solaris10, besides vi? I'd like to avoid installing anything new. If not, how to make vi more user-friendly? thanks. (8 Replies)
Discussion started by: orange47
8 Replies

7. Shell Programming and Scripting

Looking for an alternative to Tcl

I've created quite a collection of tcl scripts which have buttons, radio buttons, check boxes, text fields, etc. These tcl scripts in turn call and execute several hundred sh, csh, bash, perl scripts and pass in the args based on the gui selections on the same and other redhat machines. We're... (4 Replies)
Discussion started by: scottwevans
4 Replies

8. Shell Programming and Scripting

Alternative to Sleep?

Greetings. I've been wondering about this one for some time: Is there an alternative to sleep in bash? The reason: I'd like to simply limit the amount of processor usage in continuous while : script scenarios without spawning endless sleep processes as well. After beating the manpages, I... (14 Replies)
Discussion started by: LinQ
14 Replies

9. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

10. Cybersecurity

Alternative for chattr

Hello im working on "remover script" which try to remove "kthrotlds MINER VIRUS" in next part of my remover script i have to work on files that it destroyed, virus use chattr to open and lock files and replace them with malicious content im looking for a solution to remove chattr and disable... (9 Replies)
Discussion started by: nimafire
9 Replies
Test::Script(3pm)					User Contributed Perl Documentation					 Test::Script(3pm)

NAME
Test::Script - Basic cross-platform tests for scripts DESCRIPTION
The intent of this module is to provide a series of basic tests for 80% of the testing you will need to do for scripts in the script (or bin as is also commonly used) paths of your Perl distribution. Further, it aims to provide this functionality with perfect platform-compatibility, and in a way that is as unobtrusive as possible. That is, if the program works on a platform, then Test::Script should always work on that platform as well. Anything less than 100% is considered unacceptable. In doing so, it is hoped that Test::Script can become a module that you can safely make a dependency of all your modules, without risking that your module won't on some platform because of the dependency. Where a clash exists between wanting more functionality and maintaining platform safety, this module will err on the side of platform safety. FUNCTIONS
script_compiles script_compiles( 'script/foo.pl', 'Main script compiles' ); The "script_compiles" test calls the script with "perl -c script.pl", and checks that it returns without error. The path it should be passed is a relative unix-format script name. This will be localised when running "perl -c" and if the test fails the local name used will be shown in the diagnostic output. Note also that the test will be run with the same perl interpreter that is running the test script (and not with the default system perl). This will also be shown in the diagnostic output on failure. script_runs script_runs( 'script/foo.pl', 'Main script runs' ); The "script_runs" test executes the script with "perl script.pl" and checks that it returns success. The path it should be passed is a relative unix-format script name. This will be localised when running "perl -c" and if the test fails the local name used will be shown in the diagnostic output. The test will be run with the same perl interpreter that is running the test script (and not with the default system perl). This will also be shown in the diagnostic output on failure. SUPPORT
All bugs should be filed via the bug tracker at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Script> For other issues, or commercial enhancement and support, contact the author. AUTHOR
Adam Kennedy <adamk@cpan.org> SEE ALSO
prove, <http://ali.as/> COPYRIGHT
Copyright 2006 - 2009 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.10.1 2009-11-24 Test::Script(3pm)
All times are GMT -4. The time now is 07:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy