Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Contents

1. What Is Perl?
2. Course Requisites and Goals
3. Perl References & Resources
4. State of Perl
5. Taste of Perl
6. Storing & Running Perl Programs
7. The Elements
8. Literals & Operators
    9. Loops and I/O
10. Grade Book Example
11. Pipe I/O and System Calls
12. Matching
13. Parsing
14. Simple CGI
15. Testing Perl Programs
16. Common Goofs

11. Pipe I/O and System Calls

    #!/usr/local/bin/perl
    # Report on disk usage under specified files
    # The Unix command "du -sk ..." (on BSD Unix, "du -s ...")
    # produces a series of lines:
    #   1942    bin
    #   2981    etc
    #   ...
    # listing the K bytes used under each file or directory.
    # It doesn't show other information, such as the
    # modification date or owner.
    # This program gets du's kbytes and filename, and merges
    # this info with other useful information for each file.
    #

    $files = join(' ',@ARGV);

    # The trailing pipe "|" directs command output
    # into our program:

    if (! open (DUPIPE,"du -sk $files | sort -nr |"))  {
        die "Can't run du! $!\n";
    }

    printf "%8s %-8s %-16s %8s %s\n",
        'K-bytes','Login','Name','Modified','File';
    while (<DUPIPE>) {
        # parse the du info:
        ($kbytes, $filename) = split;

        # Call system to look up file info like "ls" does:
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,
            $size,$atime,$mtime,$ctime)
            = stat($filename);

        # Call system to associate login & name with uid:
        if ($uid != $previous_uid) {
            ($login,$passwd,$uid,$gid,$quota,$comment,
                $realname,$dir,$shell) = getpwuid($uid);
            ($realname) = split(',',substr($realname,0,20));
            $previous_uid = $uid;
        }

        # Convert the modification-time to readable form:
        ($sec,$min,$hour,$mday,$mon,$myear) = localtime($mtime);
        $mmonth = $mon+1;

        printf "%8s %-8s %-16s %02s-%02d-%02d %s\n",
            $kbytes, $login, $realname,
            $myear, $mmonth, $mday, $filename;
    }

Demonstration Output

    K-bytes Login    Name             Modified File
      40788 c527100  Fred Flintstone  95-10-05 c527100
      32685 c565060  Peter Parker     95-10-05 c565060
      24932 c579818  Clark Kent       95-10-06 c579818
      15388 c576657  Lois Lane        95-10-06 c576657
       9462 c572038  Bruce Wayne      95-10-06 c572038
       8381 c517401  Eric McGregor    95-10-05 c517401
       7022 c594912  Asterisk de Gaul 95-10-05 c594912




 
 
[an error occurred while processing this directive]