Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Scripting and The File system

Similar presentations


Presentation on theme: "Perl Scripting and The File system"— Presentation transcript:

1 Perl Scripting and The File system
Perl & Chapter 5 Perl Scripting and The File system grep –color=auto option

2 Hey real quick aliases Aliases
Aliases can be defined on the command line, in .bash_profile, or in .bashrc, using this form : alias name=‘command’ unalias will remove alias ll='ls -lh' alias ls='ls --color=auto' alias vi='vim ' alias c='clear’ $ unalias -a

3 Sessions A session started as a login session will read configuration details from the /etc/profile file first. It will then look for the first login shell configuration file in the user's home directory to get user-specific configuration details. It reads the first file that it can find out of ~/.bash_profile, ~/.bash_login, and ~/.profile and does not read any further files. In contrast, a session defined as a non-login shell will read /etc/bash.bashrc and then the user-specific ~/.bashrc file to build its environment. Non-interactive shells read the environmental variable called BASH_ENV and read the file specified to define the new environment.

4 Brief Perl Scripting Used for quick and dirty programs you can code in 3-5 minutes. (Using your favorite text editor) Perl comments are preceded with (#) A single line can be split into multiple lines with (\) and multiple lines can be put on a single line separated by (;)

5 Perl Scripting cont’ All perl scripts should start with:
print "Hello, world!\n” (example in text editor) Tells the kernel to interpret this file as perl script for execution To run do the following $ chmod +x hello.pl $ ./hello (lets open CentOS and see what happens) #!/usr/bin/perl print "Hello, World!\n";

6 More Perl In Windows the .exe extension designates an executable file
In Linux (and Unix) the execute bit is the designation. (we will learn more about the execute bit later in this chapter) Alternatively you can execute a bash script with perl filename OR ./filename (if its executable)

7 Output and Input The print command is for quick and dirty output.
<STDIN> reads input from stdin (lets open CentOS and see results) #!/usr/bin/perl print "Whatever you type I will randomly echo\n"; print "command>"; #$a=<STDIN>; $a=<>; while() { sleep rand(5)+1; print "Hi there!!! $a \n"; }

8 Perl INPUT/OUTPUT <STDIN> stands for standard input.
It can be abbreviated by using simple <>. By declaring a scalar variable and setting it equal to <STDIN> we set the variable equal to whatever will be typed by our user at the command prompt. The print command is for quick and dirty output. See example #!/usr/bin/perl print "Whatever you type I will randomly echo\n"; print "command>"; #$a=<STDIN>; $a=<>; while() { sleep rand(5)+1; print "Hi there!!! $a \n"; }

9 Scalar Data (Perl) Simplest type of data that Perl manipulates.
Most often: Numbers Floating point: 1.25, , 255.0, 7.25e45, -6.5e24, -12e-24, -1.2E-23 Integers: 0,2001,-40,255, Non decimals: 0377 (octals start with leading zero) Hex: 0xff (base 16 start with leading 0x) Binary: 0b (base 2 start with leading 0b)

10 Strings Sequence of characters
Like numbers strings have literal representations, which is the you represent the string in a Perl program. Literal strings come in two different flavors: single quoted and double quoted string literals

11 Single quoted string literals
The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted. For example, the character sequence \n is a newline character when it appears in a string with double quotes, but is literally the two characters, backslash and n, when it appears in single quotes.

12 Variables Perl has three types of variables: scalars, arrays and hashes. Think of them as ``things,'' ``lists,'' and ``dictionaries.'’ In Perl, all variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores.

13 Scalars Scalars are single things. This might be a number or a string.
The name of a scalar begins with a dollar sign, such as $i or $abacus. You assign a value to a scalar by telling Perl what it equals, like so: $i = 5; $pie_flavor = 'apple'; $constitution1776 = "We the People, etc.";

14 Scalar Data Continued Numbers in Perl can be manipulated with the usual mathematical operations: addition, multiplication, division and subtraction. (Multiplication and division are indicated in Perl with the * and / symbols, by the way.) $a = 5; $b = $a + 10; # $b is now equal to 15. $c = $b * 10; # $c is now equal to 150. $a = $a - 1; # $a is now 4

15 More Perl Background You need to test for scalar equality in different ways. For numbers use == and != to test for equality and non-equality. For strings you use eq and ne to test for the same things. See example-2.pl If you ran this you would see a 1 is placed in the place of $a==$b. When we compare them with a numerical comparison they are processed as numbers and therefore 5.0 and 5 are equal and it returns a 1 which means true in Perl. $a eq $b is replaced with nothing in the print statement because as strings the two variables are not equivalent. This is equivalent to false in Perl along with 0.

16 String Operators Strings values can be concatenated with the “.” operator “hello”.”world” # same as “helloworld” “hello” . ‘ ‘.”world” # same as ‘hello world’ ‘hello world’ . “\n” # same as “hello world\n” String repetition is single character lowercase “x” “fred” x 3 #same as “fredfredfred” 5 x 4 #same as “5” x 4 or “5555”

17 Double quoted string literals
Also Sequence of characters but now the backlash has special meaning to control characters

18 Arrays Arrays are basically a collection of scalars stored next to each other and accessed by indices from zero to the number of elements minus one. Note: when we're referring to an entire array we use at the beginning of its name. If we're referring to only one of its elements(which is a scalar) we use a $. See example-3.pl

19 Arrays Cont’ Arrays can store either strings or numbers or both.
$z[0]; #This returns the first element $simpsonsfamily[4]; #This returns the fifth element which is bart $z[3]=4; #This sets the 4th element to 4. One nice thing about arrays in Perl is that you don't have to set the size of them when you start so they will grow for you when you need them to. To find the size of an array you can you can do Which would return 3 originally and 4 after $a[4] was set to 4; You can get at the highest index by using the variable $# with the arrayname attached to the end. For example $#simpsonsfamily would be equal to 4.

20 Hashes Hashes are collections of scalars like arrays only they have indices or keys which are strings instead of integers. Hash variables are named with a % followed by at least one letter and then maybe a mix of some more numbers, letters, and underscores. Key and value are two important hash terms. The key is what you use to look up a value. The key is like the index in an array and the value is like the data stored there. See example-4.pl

21 The “if”Control Structure
Used to compare two values and execute logic based on result if ($name gt ‘fred’){ print “ ’$name’ comes after ’fred’ in sorted order.\n”; } else { print “ ’$name’ does not come after ’fred’ in sorted order.\n” }

22 Comparison Examples 35 != 30 + 5 # false 35 == 35.0 # true
’35’ eq ‘35.0’ # false (comparing as strings) ‘fred’ lt ‘free’ ‘fred’ lt ‘barney’ ‘fred lt ‘Fred’ ‘ ‘ gt ‘’

23 Calling Linux Shell Commands in Perl
Using system For example: system(”ls”,”-l”); system("ls -l"); System will execute the $command and return to your script when finished. 2. Using exec This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more. 3. Using backticks my $output = `ls -l`; The backtick operator and excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

24 The File system How your Linux systems represents system objects and resources. The file system contains: Some naming convention and organization Methods to navigate and manipulate the file system Security / access methods Code (software) that ties it all together

25 File system Types There are many different types of file systems used on different kernels FAT32 (Windows) NTFS (Windows) HFS+ (Mac OS X) Ext3 (Linux) Ext4 (Linux) XFS (Linux)

26 XFS – File System XFS is a highly scalable, high-performance file system which was originally designed at Silicon Graphics, Inc. XFS is the default file system for Red Hat Enterprise Linux 7 and CentOS 7. Main FeaturesXFS supports metadata journaling, which facilitates quicker crash recovery. The XFS file system can also be defragmented and enlarged while mounted and active. In addition, Red Hat Enterprise Linux 7 supports backup and restore utilities specific to XFS.

27 Pathnames The File system stores everything as a hierarchy starting with “/” This is known as the root directory Contains any number of subdirectories Traverse the file system using the “cd” command

28 Mounting A file tree is a collection of file systems
A file system is collection of pathnames and files You add file systems to the file tree with the “mount” command Usage: mount location mount-point mount /dev/hda4 /users After mounting you could do ls –al and see all of the contents of the directory /etc/fstab

29 Unmounting “unmount” is used to detach a mounted file system
(note**** nothing in the mounted file system can be use in order to unmount Usage: unmount mount-point The fuser command will tell you whats running in your mount-point Usage: fuser –mv mount-point

30 File tree Pathname Contents /bin Commands needed for minimal system operability /boot Kernel and files needed to load the kernel /dev Device entries for disks, printers, pseudo-terminals, etc. /etc Critical startup and configuration files /home Home directories for users /lib Libraries and parts of the C compiler /media Mount points for filesystems on removable media /opt Optional application software packages (not yet widely used) /proc Information about all running processes /root Home directory of the superuser (often just /) /sbin Commands for booting, repairing, and recovering the system /tmp Temporary files that may disappear between reboots /usr Hierarchy of secondary files and commands /usr/bin Most commands and executable files /usr/include Header files for compiling C programs /usr/lib Libraries; also, support files for standard programs /usr/local Local software (software you write or install) /usr/local/bin Local executables /usr/local/etc Local system configuration files and commands /usr/local/lib Local support files /usr/local/sbin Statically linked local system maintenance commands /usr/local/src Source code for /usr/local/* /usr/man On-line manual pages /usr/sbin Less essential commands for system administration and repair /usr/share Items that might be common to multiple systems (read-only) /usr/share/man On-line manual pages /usr/src Source code for nonlocal software packages (not widely used) /var System-specific data and configuration files /var/adm Varies: logs, system setup records, strange administrative bits /var/log Various system log files /var/spool Spooling directories for printers, mail, etc. /var/tmp More temporary space (preserved between reboots)

31 File Types Regular files Directories
Character device files (rw char by char) Block device files (rw block chunks) Local domain sockets Named pipes (FIFOs) Symbolic Links ls –al tells all rm (delete) removes all dev]# ls -l tty0 crw–w—- 1 root root 4, 0 Dec 31 11:45 tty0 dev]# dev]# ls -l sda1 brw-rw—- 1 root disk 8, 1 Dec 31 11:45 sda1 dev]# Named pipes are created via mkfifo or mknod: $ mkfifo /tmp/testpipe

32 Regular Files Collection of bytes
Data or text files, executable program, etc Nothing to see here move on……..

33 Directories You may have noticed “.” and “..” the are the directory itself and its parent directory representation mkdir creates a new directory rm –r deletes a directory and its subdirectories cp and ln (hardlink) (oldfile newfile) In-class examples

34 File Attributes $ ls -l /bin/gzip
-rwxr-xr-x 3 root root Jun /bin/gzip The first field specifies the file’s type and mode. The first character is a dash, so the file is a regular file. The next nine characters in this field are the three sets of permission bits. The next field in the listing is the link count for the file. Every time a hard link is made to a file, the count is incremented by 1. The next two fields in the ls output are the owner and group owner of the file. The next field is the size of the file in bytes. Next comes the date of last modification: June 15, 2004. The last field in the listing is the name of the file, /bin/gzip.

35 More Types Device and block files Local Domain Sockets Named Pipes
Device driver files that talk to system hardware Local Domain Sockets Connections between processes (not to be confused with network sockets but act in much the same way Aka UNIX domain sockets Named Pipes Like local domain sockets piping data from one process to another Symbolic Links ln –s command From the internet Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless (the same concept like Windows shortcut).

36 More with Files $touch a $ ln a a1 $ ls –al then view results
See that the Inode number for ‘a’ and ‘a1′ are same as we created ‘a1’ as hard link. But they are different for symbolic link Stat command

37 File Permissions Each file has set of 12 mode attributes called bits.
Read, write, execute, and file type information Nine (9) bits define permissions for file owner, group, and world (or everyone else) -r,w,x octal chmod command is extremely useful In class examples

38 File Permissions Cont’
Each file has set of 12 mode attributes called bits. Read, write, execute, and file type information Nine (9) bits define permissions for file owner, group, and world (or everyone else) -r,w,x octal chmod command is extremely useful In class examples

39 chmod Changes the file permissions In class examples
Only file owner or root can run chmod In class examples chown and chgrp(UNIX) commands allow you to change the ownership and group permission of a file umask command allows you to set a default list of permissions given to files you create


Download ppt "Perl Scripting and The File system"

Similar presentations


Ads by Google