Learning About UNIX-GNU/Linux
Perl Cross-Reference
Some people who have used these notes and/or taken the corresponding
courses on which they are based have considerable background in Perl, but
virtually none in the UNIX-GNU/Linux programming environment. This document,
called from the main course Modules, correlates some UNIX-GNU/Linux commands
and concepts with those in Perl. The order of the sections is the order in
which they are called from the Modules.
Clicking on a penguin (always a fun thing to do!) provides a "Backlink"
to the Module and Section that references that particular part of this
document; it opens the page in the current window.
Some users have reported that they occasionally get confused about
whether they are looking at the UNIX/Linux commands or the corresponding Perl
ones. Thus this page has a background of a camel, the Perl logo; I have made
him (her?) as unobtrusive as possible.
- Enclosing a UNIX/Linux command between graves or backticks `
` executes the command and returns the result:
- The trailing newline from the date command will also be
in the variable $date, so you may wish to use chomp to remove
it.
- As we shall discuss in Module 4, every UNIX/Linux command returns a
status code. Using the built-in system function executes the command
and returns the status code. The lower eight bits that are returned are the
status from the UNIX/Linux wait command, so the actual status code
from the command is gotten with:
$status_code = system("date")/256; |
- By default system outputs the output from its
argument.
- Typically the output will be directed to your terminal
window.
- That output is not available to the Perl program that invoked
it.
- The built-in function unlink provides the same functionality
as rm:
- Since Perl was written by UNIX/Linux geeks, many Perl functions
are identical in name to the UNIX/Linux ones.
- Other functions, such as unlink, take their name from
the UNIX/Linux C programming libraries.
- You can copy a file by opening the original file for reading and the
file to be copied into for writing and then just read the input file and write
to the output file.
- The File::Copy module provides a copy function:
use File::Copy;
copy CV.pdf, old-CV.pdf; |
- The module also provides the same function with the name cp:
use File::Copy "cp";
cp CV.pdf, old-CV.pdf; |
- This form is identical to the shell command except that the file
names are separated by commas in Perl and just by spaces in the shell.
- The built-in function rename moves a file:
rename committee, minutes.txt; |
- The Cwd module provides a cwd function that returns
the current working directory:
- In Linux-speak it is called the "present working directory."
- The built-in function chdir changes the current (present)
working directory:
- The built-in function mkdir creates a directory:
- The built-in function rmdir removes empty directories:
- The File::Path module provides a rmtree function
which removes a directory and all of its contents:
Use File::Path;
rmtree('NonEmptyDirectory'); |
- This is identical to the shell command rm -r.
- You can construct and execute pipelines with graves or
system:
$number_logged_in = `who | wc -l`; |
- You can merge stderr with stdin with the shell and
test whether the command issued an error message.
- Recall that rm is totally silent unless you have asked
it to remove a non-existent file or made some other mistake:
die "rm spoke!\n" if `rm no_such_file 2>&1`; |
- The %ENV hash contains the current environment variables:
- The regular expressions known by Perl are a superset of the
standard UNIX/Linux ones.
- The s2p program converts sed commands to Perl:
[you@faraday you]$ echo "s/[Tt]he/Das/" | s2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
while ($ARGV[0] =~ /^-/) {
$_ = shift;
last if /^--/;
if (/^-n/) {
$nflag++;
next;
}
die "I don't recognize this switch: $_\\n";
}
$printit++ unless $nflag;
$\ = "\n"; # automatically add newline on print
LINE:
while (<>) {
chop;
s/[Tt]he/Das/;
print if $printit;
}
[you@faraday you]$ _ |
- Here is another way to open a UNIX/Linux process to be read from. We
print the output from ps omitting the first line:
open PS, "ps |";
<PS>;
print while (<PS>);
close PS; |
- The a2p program converts awk to Perl:
[you@faraday you]$ echo ' { print $2 } ' | a2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator
while (<>) {
($Fld1,$Fld2) = split(' ', $_, 9999);
print $Fld2;
}
[you@faraday you]$ _ |
- The file tests provided by the shell are almost identical to those
provided by Perl:
if( -f "/etc/passwd") {
print "the password file exists\n";
} |
- The use of an exclamation mark to negate a test was borrowed from the
shell by Perl:
if( ! -f "/etc/YouHaveBeenHacked") {
print "You have not been hacked\n";
} |
- The built-in function chmod uses the same form as the
numerical mode of the UNIX/Linux command of the same name, except you must
explictly tell Perl that it is an octal number:
- The built-in function umask has almost exactly the same
syntax as the numerical form of the UNIX/Linux one:
# Get the existing umask
$existing = umask();
# Turn off read, write and execute bits for the group
# and for others.
umask(077);
open FOO, ">somefile";
# Restore the umask
umask($existing); |
- The built-in function link makes links to a file:
link some_file, link_to_file; |
- The built-in function symlink makes symbolic links:
symlink some_file, symln; |
This document was written by David M.
Harrison in June, 2002.
The document is Copyright © 2002 David M.
Harrison.
It may be distributed only subject to the terms and conditions
set forth in the Open Content License, v1.0 or later (the latest version is
presently available at http://opencontent.org/opl.shtml).
This
is $Revision: 1.3 $, $Date: 2002/06/26 12:39:41 $ (y/m/d UTC).