NetAdminTools.com
 
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


Perl File Tests
Topic: Perl   Posted:2003-02-08
Printer Friendly: Print

spacerspacer
There are quite a few simple file tests you can use while waltzing around your filesystem with perl. Here are a list of tests. Let's run the test from the command line. To test if a file exists:

srv-44 tmp # mkdir -p /tmp/perltest
srv-44 tmp # cd /tmp/perltest
srv-44 perltest # cat /dev/null > ruktest
srv-44 perltest # perl -e "print \"ok\\n\" if (-e \"ruktest\")";
ok
srv-44 perltest # rm ruktest
srv-44 perltest # perl -e "print \"ok\\n\" if (-e \"ruktest\")";
srv-44 perltest # 

The -e option for perl means to run the stuff in the quotes as a script. Since the script is in quotes, we have to "escape" the quotes for the parameters inside the quotes. To get the \n in there, we have to escape the escape n. :) If this was inside a script, it would look like this:

srv-44 perltest # vi t.pl
srv-44 perltest # perl t.pl
srv-44 perltest # cat /dev/null > ruktest
srv-44 perltest # perl t.pl
ok
srv-44 perltest # cat t.pl
print "ok\n" if (-e "ruktest");
srv-44 perltest #

We often like to run little snippits of perl from the command line before putting the snippit in a script. Also, it is useful to run one liner perl scripts inside shell scripts. Note that this perl script is equivalent (and related) to this bash command:

srv-44 perltest # if test -e ruktest; then echo ok; fi
ok 





Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com