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 |