28
Linux Admin RAID in Linux The term RAID is an acronym for the phrase, Redundant Array of Independent Disks. RAID is a way of combining the storage available across multiple disks and supplying users a single, unified virtual device. RAID can be used to provide: 1. data integrity 2. fault tolerance 3. improved performance 4. greater storage capacity Hard disks are mechanical devices involving moving parts and unfortunately tend to fail over time. There are also physical limits to the speed at which data can be read and/or written to disks. RAID helps mitigate this risk by protecting data stored on hard disks and improving disk performance by writing the data to multiple physical locations according to several different schemas, known as "RAID Levels". Furthermore, RAID can be provided by either dedicated, specialized hardware or by the operating system at a virtual layer. Hardware RAID vs. software RAID? Hardware RAID solutions exist that operate as dedicated devices, usually as PCI expansion cards or directly on the motherboard. The independent disks attach to the hardware interface. In a true hardware RAID, the operating system simply writes data to the hardware RAID controller which handles the multiplicitous reads and writes to the associated disks. Other so−called hardware RAIDs rely on special drivers to the operating system; these act more like software RAIDs in practice. With current technology, hardware RAIDconfigurations are generally chosen for very large RAIDs. Additionally, some operating systems, including Linux®, provide RAID functionality within a software layer.RAID partitions are logically combined and a virtual device appears to higher layers of the operating system in place of the

Linux admin

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Linux admin

Linux AdminRAID in Linux

The term RAID is an acronym for the phrase, Redundant Array of Independent Disks. RAID is a way of combining the storage available across multiple disks and supplying users a single, unified virtual device.RAID can be used to provide:

1. data integrity2. fault tolerance3. improved performance4. greater storage capacity

Hard disks are mechanical devices involving moving parts and unfortunately tend to fail over time. There are also physical limits to the speed at which data can be read and/or written to disks. RAID helps mitigate this risk by protecting data stored on hard disks and improving disk performance by writing the data to multiplephysical locations according to several different schemas, known as "RAID Levels". Furthermore, RAID can be provided by either dedicated, specialized hardware or by the operating system at a virtual layer.

Hardware RAID vs. software RAID?Hardware RAID solutions exist that operate as dedicated devices, usually as PCI expansion cards or directly on the motherboard. The independent disks attach to the hardware interface. In a true hardware RAID, the operating system simply writes data to the hardware RAID controller which handles the multiplicitous reads and writes to the associated disks. Other so−called hardware RAIDs rely on special drivers to the operating system; these act more like software RAIDs in practice. With current technology, hardware RAIDconfigurations are generally chosen for very large RAIDs.Additionally, some operating systems, including Linux®, provide RAID functionality within a software layer.RAID partitions are logically combined and a virtual device appears to higher layers of the operating system in place of the multiple constituent devices. This solution is often a high−performance and inexpensive alternative available for RAID users. RAID levelsThere are many RAID levels. It will be impossible to list them all here. Here we mention the most common & most important RAID types, all of which are fully supported by Linux. RAID0 (Striping) 

Page 2: Linux admin

This level is achieved by grouping 2 or more hard disks into a single unit with the total size equaling that of all disks used.Practical example: 3 disks, each 80GB in size can be used in a 240GB RAID0 configuration.RAID0 works by breaking data into fragments and writing to all disk simultaneously. This significantly improves the read and write performance.On the other hand, no single disk contains the entire information for any bit of data committed. This means that if one of the disks fails, the entire RAID is rendered inoperable, with unrecoverable loss of data.RAID0 is suitable for non-critical operations that require good performance, like the system partition or the /tmp partition where lots of temporary data is constantly written. It is not suitable for data storage.Usable Space in Raid level0 = (smallest disk) * (no. of disks)

RAID1 (Mirroring)

 This level is achieved by grouping 2 or more hard disks into a single unit with the total size equaling that of the smallest of disks used. 

Page 3: Linux admin

This is because RAID1 keeps every bit of data replicated on each of its devices in the exactly same fashion, create identical clones. Hence the name, mirroring. Practical example: 2 disks, each 80GB in size can be used in a 80GB RAID1 configuration. Usable space of Raid level1= smallest-disk

On a side note, in mathematical terms, RAID1 is an AND function, whereas RAID 0 is an OR. Because of its configuration, RAID1 reduced write performance, as every chunk of data has to be written n times, on each of the paired devices. The read performance is identical to single disks.Redundancy is improved, as the normal operation of the system can be maintained as long as any one disk is functional.RAID 1 is suitable for data storage, especially with non-intensive I/O tasks.  RAID5 

 This is a more complex solution, with a minimum of three devices used. Two or more devices are configured in a RAID0 setup, while the third (or last) device is a parity device. If one of the RAID 0 devices malfunctions, the array will continue operating, using the parity device as a backup. The failure will be transparent to the user, save for the reduced performance.RAID 5 improves the write performance, as well as redundancy and is useful in mission-critical scenarios, where both good throughput and data integrity are important. RAID 5 does induce a slight CPU penalty due to parity calculations.Usable Space for Raid Level5 = smallest-disk*(no of disk -1) Linear RAID This is a less common level, although fully usable. Linear is similar to RAID0, except that data is written sequentially rather than in parallel. Linear RAID is a simple grouping of several devices into a larger volume, the total size of which is the sum of all members. For instance, three disks

Page 4: Linux admin

the sizes of 40, 60 and 250GB can be grouped into a linear RAID the total size of 350GB. Linear RAID provides no read/write performance, not does it provide redundancy; a loss of any member will render the entire array unusable. It merely increases size. It’s very similar to LVM. Linear RAID is suitable when large data exceeding the individual size of any disk or partition must be used.Now, move backup to tape or other server: Nested RAID Levels RAID0+1 

Exapmle of RAID Level 0+1 A RAID 0+1 (also called RAID01), is a RAID level used for both replicating and sharing data among disks.The minimum number of disks required to implement this level of RAID is 3 (first, even numbered chunks on all disks are built – like in RAID0 – and then every odd chunk number is mirrored with the next higher even neighbour) but it is more common to use a minimum of 4 disks. 

Page 5: Linux admin

The difference between RAID0+1 and RAID 1+0 is the location of each RAID system — RAID0+1 is a mirror of stripes although some manufacturers (e.g. Digital/Compaq/HP) use RAID0+1 to describe striped mirrors, consequently this usage is now deprecated so that RAID0+1 and RAID1+0 are replaced by RAID10 whose definition correctly describes the correct and safe layout, i.e. striped mirrors.RAID1 + 0A RAID1+0, sometimes called RAID1&0 or RAID10, is similar to a RAID0+1 with exception that the RAID levels used are reversed — RAID10 is a stripe of mirrors.

Hard disks are mechanical devices involving moving parts and unfortunately tend to fail over time. There are also physical limits to the speed at which data can be read and/or written to disks. RAID helps mitigate this risk by protecting data stored on hard disks and improving disk performance by writing the data to multiple

4 THOUGHTS ON “LINUX ADMIN”

1. Nitin Makasare on August 31, 2012 at 5:19 pm said:

Best and formative topic. If possible explain how to recover data from H/W RAID if one of the disk fails in RAID-1 or RAID-5

Thanking you

Page 6: Linux admin

Reply ↓

2. arvind on September 6, 2012 at 10:43 am said:

this is very needful for us. one things is important , how can i convert data if one hdd is fail.

Reply ↓

3. Praveen on September 11, 2012 at 1:28 pm said:

Very helpful…. Thank you for sharing this….

Reply ↓

4. baskar on September 13, 2012 at 7:48 am said:

Hi, is it possible to create software RAID10 in linux?if so, how to do it?

Regards Boss

How To Install and Configure a File and Print Server in Windows Server 2003

Page 7: Linux admin

You can create a Windows Server 2003 file server and print server manually, or you

can use the wizards that are provided in the Configure Your Server Wizard

administrative tool.

How to Install a File Server on Windows Server 2003 by Using the

Configure Your Server Wizard

Click Start, point to Administrative Tools, and then

click Configure Your Server Wizard.

Click Next.

Click Next.

Click File server in the Server role box, and then click Next.

On the “File Server Disk Quotas” page, configure any quotas you

need to control disk-space usage on the server, and then click Next.

On the “File Server Indexing Service” page, click the indexing

configuration that is appropriate for your server, and then clickNext.

Click Next.

Click Finish.

The Share a Folder Wizard starts. ClickNext.

Click Browse, locate the folder that you want to share, and then

click OK.

Click Next.

Type a share name for the folder, and then click Next.

Click one of the basic permissions for the folder, or

click Customize to set custom permissions on the folder.

Click Finish.

Click Close.

How to Manually Install a File Server on Windows Server 2003

Click Start, and then click Windows Explorer.

Locate the folder that you want to share.

Right-click the folder, and then click Sharing and Security.

Click Share this folder, and then accept the default name or type

a different name for the share.

Optionally, configure the number of users who can connect,

configure permissions for this folder, and then configure the caching

options.

Page 8: Linux admin

Click OK.

A little hand is displayed in the Windows Explorer window to

indicate that the folder is being shared.

Quit Windows Explorer.

Install a Windows Server 2003 Print Server

How to Install a Print Server on Windows Server 2003 by Using

the Configure Your Server Wizard

Click Start, point to Administrative Tools, and then

click Configure Your Server Wizard.

Click Next.

Click Next.

Click Print server in the Server role box, and then click Next.

On the “Printers and Printer Drivers” page, click the types of

Windows clients that your print server will support, and then

clickNext.

Click Next.

On the “Add Printer Wizard Welcome” page, click Next.

Click Local printer attached to this computer, click to clear

theAutomatically detect and install my Plug and Play

printer check box, and then click Next.

Click the port for your printer, and then click Next.

Click the printer make and model or provide the drivers from the

printer manufacturer media, and then click Next.NOTE: If you are

prompted to keep or not keep your existing printer driver, either

keep the existing driver or replace the existing driver. If you replace

the driver, you must provide the manufacturer driver for this printer.

Click Next to continue

Linux AdminHow To Add New Disk To The Hardware Raid Controller Without Rebooting The Server In Linux First find out your RAID hardware as we will need appropriate OS utility to manage the samefrom the same vendor. In our case it was lsi which can be managed via Megacli utility. 

Page 9: Linux admin

[root@server 8.02.21_Linux_MegaCLI]# lspci | grep -i raid07:00.0 RAID bus controller: LSI Logic / Symbios Logic MegaRAID SAS 2108 [Liberator] (rev 05)Lets first install the utilty :To do this we need to install the Megacli utility on our server Centos 6.2.Step:1. Install the below two packages.(downlaod from http://www.lsi.com/downloads/Public/MegaRAID%20Common%20Files/8.02.21_MegaCLI.zip) .

#rpm -ivh Lib_Utils-1.00-09.noarch.rpm#rpm -ivh MegaCli-8.02.21-1.noarch.rpm

Step:2. By default the utility is placed at location as below..[root@server 8.02.21_Linux_MegaCLI]# cd /opt/MegaRAID/MegaCli/[root@server MegaCli]# lsinstall.log MegaCli64 MegaSAS.log

Step:3. Run the below command before adding the brand new disk drive.[root@server MegaCli]# /opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll | grep -e “Enclosure Device ID” -e SlotEnclosure Device ID: 252Slot Number: 0Enclosure Device ID: 252Slot Number: 1

Step:4. Run the command again after injecting the new HDD in drive slot.[root@server MegaCli]# /opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll | grep -e “Enclosure Device ID” -e SlotEnclosure Device ID: 252Slot Number: 0Enclosure Device ID: 252Slot Number: 1Enclosure Device ID: 252Slot Number: 3

Step:5. Now you have the following information the Enclosure Device ID: 252 and Slot Number: 3 for the newly added drive. Step:6. Once added the new drive will be in foreign state. Give the below command to clear the foreign status.

Page 10: Linux admin

[root@server MegaCli]#/opt/MegaRAID/MegaCli/MegaCli64 -CfgForeign -Clear -aALL

Step:7. Add the new drive to the desired RAID level by running below command.#/opt/MegaRAID/MegaCli/MegaCli64 -CfgLdAdd -r0 [252:3] -a0

Options:-r1 = RAID1252 = Enclosure Device ID2 and 3 = Slot Number-a0 = AdapterIn our case :-r0 = RAID0252 = Enclosure Device ID3 = Slot Number-a0 = Adapter

Step:8. tail the /var/log/messages for the new disksd 0:2:1:0: [sdb] Mode Sense: 1f 00 00 08sd 0:2:1:0: [sdb] Write cache: enabled, read cache: enabled, doesn’t support DPO or FUAsdb: sdb1 sdb2sd 0:2:1:0: [sdb] Attached SCSI disk

Now you can use the new disk as any other normal drive.

5 THOUGHTS ON “LINUX ADMIN”

1. pradeep on July 21, 2012 at 11:22 am said:

You are right

mind blowing topic , it helped me a lot !!!!!!!!!!!

Reply ↓

Page 11: Linux admin

2. Sanjay Negi on August 1, 2012 at 4:59 pm said:

Thanks for sharing this information…..It is of great help to me……

Reply ↓

3. Sanjay Negi on August 1, 2012 at 5:01 pm said:

Thank you for sharing this very usefull information. It will enhance my knowledge of Linux….

Reply ↓

4. Shiva on August 2, 2012 at 7:07 pm said:

Thank you very much. Very good information.

Reply ↓

Page 12: Linux admin

5. PRASAD RANADIVE on August 4, 2012 at 10:25 am said:

Thank u for sharing a very usefull information.

Windows AdminConfigure NTP (Network Time Protocol) on Windows Server 2008

The Network Time Protocol (NTP) in Windows 2008 provides your server with the ability to control the system clocks on your Windows client machines. This keeps all machines on the network synchronized with the same system time. This is beneficial for Windows administrators who need to schedule time-sensitive services on the network. You set the Windows 2008 NTP service in the registry.Step 1- Click the Windows 2008 “Start” button and enter “regedit” into the search box. Press Enter to open the Windows registry editor.Step 2- Navigate to the “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\“.registry key

Step 3- Double click the key value labeled “AnnounceFlags” in the center detail pane.

Step 4- Enter “A” into the DWORD value text box. Click “OK” to save your settings. Close the Windows registry editor.

Page 13: Linux admin

Step 5- Click the Windows “Start” button and enter “cmd” into the text box. Press Enter to open the command prompt. Enter the following into the command prompt and press Enter:net stop w32time && net start w32timeThis restarts the NTP system time settings and loads the new registry values for your Windows 2008 server.

3 THOUGHTS ON “WINDOWS ADMIN”

1. Sanjay Negi on August 1, 2012 at 4:55 pm said:

This information provided is of great benefit to me.Now I can synchronize all the machines on my network with my sysrem time.This was very easy to understand and it was easy to follow the steps.Thank you.

Reply ↓

2. santhosh on August 23, 2012 at 9:24 am said:

this is very silmple and easy to learn

Reply ↓

Page 14: Linux admin

3. Nitin Makasare on August 31, 2012 at 5:32 pm said:

Nice and very easy steps to configure NTP Server. Thanks.

Network AdminHow to get netbios name in linux

NetBios stands for Network Basic Input/Output System. It provides

services related to the session layer of the OSI model allowing

applications on separate computers to communicate over a local area

network.NetBIOS provides three distinct services:

1. Name service for name registration and resolution.

2. Session service for connection-oriented communication.

3. Datagram distribution service for connectionless communication.

Enabling NetBIOS over TCP/IP

We can enable NetBT for computers running Windows Server 2003 or

Windows XP by opening Network Connections, right-clicking a connection,

clicking the Internet Protocol (TCP/IP) component, clicking Properties,

clicking Advanced, clicking the WINS tab, and clicking the appropriate

option in NetBIOS setting.  Below Figure  shows the WINS tab.

Page 15: Linux admin

For the NetBIOS setting, you can choose any of the following options:

* Default Enables NetBT if the network connection has a static IPv4

address configuration. If the network connection uses the Dynamic Host

Configuration Protocol (DHCP), it uses the DHCP options in the received

DHCPOffer message to either disable NetBT or enable and configure

NetBT. To disable NetBT using DHCP, configure the Disable NetBIOS over

TCP/IP (NetBT) Microsoft vendor-specific option for the value of 2. This is

the default setting for LAN connections.

 

*   Enable NetBIOS over TCP/IP Enables NetBT, regardless of the DHCP

options received. This is the default setting for remote connections (dial-

up or virtual private network).

*    Disable NetBIOS over NetBIOS Disables NetBT, regardless of the DHCP

options received

Page 16: Linux admin

If We are using linux and want to administer a network full of windows

machine, there is a tool called nbtscan to easily scan through the network

and list all machines with their netbios name.

To install nbtscan in linux(Centos):

# yum install nbtscan

To use it on single machine(let’s say the machine’s ip is 10.0.0.100):

# nbtscan 10.0.0.100

To scan the whole class C and list netbios names:

# nbtscan 10.0.0.0/24

To scan ip range:

# nbtscan 10.0.0.10-100

To get all options to use nbtscan:

# nbtscan -h

Database AdminHow To  take backup and restore mysql databases in Linux 

To backup(dump) single mysql database to a file , first we need to open  the terminal  and use the below syntax

# mysqldump -u <username> -p <databasename>  > backup.sql

Page 17: Linux admin

Username = this is your database usernamedatabasename = the name of your database which you want to backupbackup.sql = the filename for your database backup

Example: # mysqldump    -u    root    -p    plist > /root/backuplist.sql

where -u is for username, -p is for mysqldump to ask for password, plist is the name of the database backuplist.sql is the file to dump the database.

To restore back mysql database from dumpfile:

Method:1 Open the  terminal , use below command # mysql    -u    root    -p    plist < /root/backuplist.sql

-u and -p are similar to above, plist is the name of the database and backuplist.sql is the name of the dumpfile.

Method:2  From inside mysql console where we have to access mysql console first:mysql> source backuplist.sqlormysql> \. backuplist.sql

where backuplist is the dumpfile

2 THOUGHTS ON “DATABASE ADMIN”

1. Sumit Basu on August 25, 2012 at 8:39 pm said:

Really useful info I found…..Thanks to the developers..

Reply ↓

Page 18: Linux admin

2. Sachin Chavan on September 15, 2012 at 5:52 pm said:

really nice in4mation…!!!

Database AdminHow To Enable Binary Logs In MYSQL Server In Linux The binary log contains events that describe database changes such as table creation operations or changes to table data. It also contains events for statements that potentially could have made changes (for example, a DELETE which matched no rows). The binary log also contains information about how long each statement took that updated data. The binary log has two important purposes:

For replication, the binary log is used on master replication servers as a record of the statements to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master.

Certain data recovery operations require use of the binary log. After a backup has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup.

Step:1 Stop the mysql service using below command # service mysqld stop

 Step:2 Edit the mysql configuration files (/etc/my.cnf) and add below lines log-bin = /var/lib/mysql/<application-name>-mysql-bin.logORlog-bin=mysql-binexpire_logs_days = 2# binary logging format – mixed recommendedbinlog_format=mixed 

Page 19: Linux admin

The logs will go to the mysql data directory and will have the mysql-bin prefix if we use “log-bin=mysql-bin” otherwise we can mentioned the location as we have done in above file.“expire_logs_days” system variable to expire binary log files automatically after a given number of days. Step:3 Now start the mysql service using below command# service mysqld start

ONE THOUGHT ON “DATABASE ADMIN”

1. Sanjay Negi on August 1, 2012 at 5:07 pm said:

This topic is indeed is very helpful for understanding the steps to enable

binary logs In MYSQL server In Linux.

Reply ↓

ProgrammingFunctions In Perl

 

print length(“There’s more than one way to do it”), “\n”;

length() is a built-in function that tells how many characters are in a

string. A function is a named sub-routine that accepts several arguments

and returns a value that can be further evaluated as part of a greater

expression, or used directly.

To help us understand functions further, let’s inspect the perl

function substr (short for “substring”).substr retrieves sub-strings of a

given string. The first argument to it is a string, the second is the offset

from which to take the substring, and the third is the length of the

substring to be taken. The third one is optional and if unspecified, returns

the substring till the end of the string.

Page 20: Linux admin

print substr(“A long string”, 3), “\n”;

print substr(“A long string”, 1, 4), “\n”;

print substr(“A long string”, 0, 6), “\n”;

ong string

lon

A long

The int() function

This function takes a number and rounds it down to a near integer (=

whole number). Here’s an example:

print “The whole part of 5.67 is ” . int(5.67) . “\n”;

 

More about strings 

In perl, strings and numbers are seamlessly converted into each other

depending on the context in which they are used.

 

print ((“5″ + “6″), “\n”);

print ((56 . 23), “\n”);

The output of this program is:

11

5623

Escape Sequences

We have already encountered the \n “escape sequence” which can come

inside strings and designates a newline character. There are many others

in perl. Here is a list of the most important ones:

\\ – designates an actual backslash (\)

\” – designates an actual double-quote character (“)

\$ – an actual dollar sign (a real $ does something else)

Page 21: Linux admin

\@ – an actual at-sign (a non-escaped @ does something else)

\n – a newline character

\r – a carriage return sign

\t – a tab character

\xDD – where “DD” are two hexadecimal digits – gives the character

whose ASCII code is “DD”.

Here’s an example to illustrate some of them:

print “I said \”hi!\” to myself, and received no reply.\n”;

print “This program will cost you \$100 dollars.\n”;

print “The KDE\\GNOME holy war makes life in the Linux world ” .

“more interesting.\n”;

whose output is:

I said “hi!” to myself, and received no reply.

This program will cost you $100 dollars.

The KDE\GNOME holy war makes life in the Linux world more interesting.

Variables in Perl

Variables are named cells stored in the computer memory that can hold

any single Perl value. One can change the value that a variable holds, and

one can later retrieve the last value assigned as many times as wanted.

 

Variables in Perl start with a dollar sign ($) and proceed with any number

of letters, digits and underscores (_) as long as the first letter after the

dollar is a letter or underscore.

To retrieve the value of a variable one simply places the variable name

(again including the dollar sign) inside an expression.

To assign value to a variable, one places the full variable name (including

the dollar sign) in front of an equal sign (=) and places the value to the

right of the equal sign. This form is considered a statement and should be

followed by a semicolon. The value assigned may be an expression that

may contain other variables (including the assigned variable itself!).

Page 22: Linux admin

An example will illustrate it:

$myvar = 17;

$x = 2;

print $myvar, ” * “, $x, ” = ” , ($myvar*$x), “\n”;

$x = 10;

print $myvar, ” * “, $x, ” = ” , ($myvar*$x), “\n”;

$x = 75;

print $myvar, ” * “, $x, ” = ” , ($myvar*$x), “\n”;

$x = 24;

print $myvar, ” * “, $x, ” = ” , ($myvar*$x), “\n”;

The output of this program is:

17 * 2 = 34

17 * 10 = 170

17 * 75 = 1275

17 * 24 = 408

Several things can be noticed:

The value of $x changes throughout the program. It’s perfectly fine,

and usually even necessary to modify the value of a variable.

By using $myvar we can ensure, that assuming we wish to change

its value, we will only have to change it in one place, not in every

place it appears.

“+=” and friends

Perl provides a shortcut for writing “$myvar = $myvar + $value”, or

“$myvar = $myvar / $value” and similar operations. Here’s an example:

$x = 1;

$y = 0;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y += 1;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y += 1;

print “2^”, $y, “=”, $x, “\n”;

Page 23: Linux admin

$x *= 2; $y += 1;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y += 1;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y += 1;

print “2^”, $y, “=”, $x, “\n”;

Since the operations $x += 1 and $x -= 1 are so commonly used, they

were also assigned a separate operator. One can use $x++ and $x– to

perform them. For example, the above program could have been written

as:

$x = 1;

$y = 0;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y++;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y++;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y++;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y++;

print “2^”, $y, “=”, $x, “\n”;

$x *= 2; $y++;

print “2^”, $y, “=”, $x, “\n”;

Input in Perl

In order to receive a value from the user, perl supplies the <> operator.

When entered, this operator reads a line from the command line, and

returns it (along with the newline character).

Here’s an example:

print “Please enter your name:\n”;

$name = <>;

chomp($name);

print “Hello, “, $name, “!\n”;

Page 24: Linux admin

Notice the chomp function which strips off the trailing newline character

from the variable. You would usually want to use it, when getting input

from the user.

Here’s another example:

print “Please enter a string:”;

$string = <>;

chomp($string);

print “The string you entered contains “, length($string), ” characters.\n”;

The For Loop

The for loop enables us to iterate over a sequence of numbers and repeat

the same set of operations for each number.

For example, the following program prints all the numbers from 1 to 100:

for $i (1..100)

{

print $i, “\n”; 

}

Some explanations about the syntax:

$i is the iteration variable. It receives the value 1, then the value 2,

then 3 and so forth until it is equal to 100, afterwards the loop

terminates.

The curly brackets ({ … }) encapsulate the loop block. The loop

block is executed once for each value $i accepts. Within that block,

called the loop body, you can use $i and you’ll get its current value.

We can nest loops, so for example the following program prints the

multiplication board:

for $y (1 .. 10)

{

for $x 

(1 .. 10) 

Page 25: Linux admin

$product = $y*$x;

# Add as much whitespace as needed so the number will occupy

# exactly 4 characters.

for $whitespace (1 .. (4-length($product)))

print ” “;

}

print $product;

}

# Move to the next line 

print “\n”;

}

You may have noticed the program’s comments. In perl comments start

with the sharp sign (#) and extend to the end of the line. Writing the

multiplication boards with the labels that indicate which numbers are

being multiplied is left as an exercise to the reader.