Home » UNIX PROGRAMS AND COMMANDS

# Category Archives: UNIX PROGRAMS AND COMMANDS

## UNIX PROGRAMS TO CHECK EVEN AND ODD

## SHELL PROGRAM TO CHECK EVEN AND ODD

**OUTPUT OF THE SHELL PROGRAM:**

## PERL PROGRAM TO CHECK EVEN AND ODD

## PERL PROGRAM TO CHECK EVEN AND ODD USING SUBROUTINE:

# Perl program to check a Number is Even or Odd using Subroutine

# Date: 09-06-2015

print “Checking a Number is Even or Odd\n”;

print “Enter the Number\n”;

$number = <STDIN>;

print “Number = $number”;

# Function definition for checking Even or Odd

# Function call for checking Even or Odd

$f = &even_odd($number);

if($f == 1){

}

else{

}

**OUTPUT OF THE PERL PROGRAM:**

abhishekgupta@Abhishek:~$ chmod +x even.pl

abhishekgupta@Abhishek:~$ ./even.pl

Checking a Number is Even or Odd

Enter the Number

4

Number = 4

Number is Even.

abhishekgupta@Abhishek:~$ ./even.pl

Checking a Number is Even or Odd

Enter the Number

7

Number = 7

Number is Odd.

abhishekgupta@Abhishek:~$

## SHELL AND PERL PROGRAM TO FIND ARMSTRONG NUMBERS

**OUTPUT OF THE PROGRAM:**

### PERL PROGRAM TO FIND ARMSTRONG NUMBERS WITHIN A RANGE

#!/usr/bin/perl

# Perl program to find Armstrong Numbers within a range

print “Armstrong Numbers within a range\n”;

print “Enter the starting value of Range\n”;

$i = <STDIN>;

print “Enter the ending value of Range\n”;

$j = <STDIN>;

print “Armstrong Numbers are as follows:\n”;

$sum = 0;

while($i != $j){

$n = $i;

while($n != 0){

$sum = ($n % 10) * ($n % 10) * ($n % 10) + $sum;

$n = $n / 10;

}

if($i == $sum){

print “$i\t”;

}

$sum = 0;

$i = $i + 1;

}

print “\n”;

**PERL PROGRAM TO FIND ARMSTRONG NUMBERS WITHIN A RANGE USING SUBROUTINE:**

#!/usr/bin/perl

# Perl program to find Armstrong Numbers within a range using Subroutine

print “Armstrong Numbers within a range\n”;

print “Enter the starting value of Range\n”;

$i = <STDIN>;

print “Enter the ending value of Range\n”;

$j = <STDIN>;

# Function definition starts here

sub Armstrong{

$x = $_[0];

$y = $_[1];

$sum = 0;

print “Armstrong Numbers are as follows:\n”;

while($x != $y){

$n = $x;

while($n != 0){

$sum = ($n % 10) * ($n % 10) * ($n % 10) + $sum;

$n = $n / 10;

}

if($x == $sum){

print “$x\t”;

}

$sum = 0;

$x = $x + 1;

}

}

# Function call

&Armstrong($i, $j);

print “\n”;

**OUTPUT OF THE PROGRAM:**

abhishekgupta@Abhishek:~$ chmod +x armstrong.pl

abhishekgupta@Abhishek:~$ ./armstrong.pl

Armstrong Number within a range

Enter the starting value of Range

2

Enter the ending value of Range

999

Armstrong Numbers are as follows:

153 370 371 407

abhishekgupta@Abhishek:~$

## SHELL PROGRAM TO CHECK PRIME AND NON PRIME

**OUTPUT OF THE PROGRAM:**

## PERL PROGRAM TO CHECK A NUMBER IS PRIME OR NON-PRIME

#!/usr/bin/perl

# Perl program to check a Number is Prime or Non-Prime

# Date: 10-06-2015

print “Checking For Prime and Non-Prime\n”;

print “Enter the Number\n”;

$number = <STDIN>;

print “Number = $number”;

$flag = 0;

if($number == 1){

print “1 is neither Prime nor Non-Prime\n”;

}

else{

for($i = 2; $i<$number; $i = $i + 1){

if($number % $i == 0){

$flag = 1;

goto PRINT;

}

}

PRINT: if($flag == 1){

print “The Number is Non-Prime\n”;

}

else{

print “The Number is Prime\n”;

}

}

**PERL PROGRAM TO CHECK A NUMBER IS PRIME OR NON-PRIME USING SUBROUTINE:**

#!/usr/bin/perl

# Perl program to check a Number is Prime or Non-Prime using Subroutine

# Date: 10-06-2015

print “Checking For Prime and Non-Prime\n”;

print “Enter the Number\n”;

# Taking input

$number = <STDIN>;

print “Number = $number”;

if($n == 1){

print “1 is neither Prime nor Non-Prime\n”;

}

else{

# Function definition

sub prime{

$flag = 0;

$n = $_[0];

for($i = 2; $i<$n; $i = $i + 1){

if($n % $i == 0){

$flag = 1;

goto RETURN;

}

}

RETURN: return $flag;

}

# Function call

$f = &prime($number);

if($f == 1){

print “The Number is Non-Prime\n”;

}

else{

print “The Number is Prime\n”;

}

}

**OUTPUT OF THE PROGRAM:**

abhishekgupta@Abhishek:~$ chmod +x prime.pl

abhishekgupta@Abhishek:~$ ./prime.pl

Checking For Prime and Non-Prime

Enter the Number

3

Number = 3

The Number is Prime

abhishekgupta@Abhishek:~$ ./prime.pl

Checking For Prime and Non-Prime

Enter the Number

6

Number = 6

The Number is Non-Prime

abhishekgupta@Abhishek:~$

### PERL PROGRAM TO FIND PRIME FACTOR OF A NUMBER

#!/usr/bin/perl

# Perl program to find Prime factor of a Number

# Date: 14-06-2015

print “Finding Prime factor of a Number\n”;

print “Enter The Number\n”;

$number = <STDIN>;

print “Number = $number”;

for($i = 2; $i < $number; $i = $i+ 1){

if($number % $i == 0){

# Function call

$x = &prime_factor($i);

if($x == 0){

print “$i is the Prime Factor of $number”;

}

$flag = 1;

}

}

if($flag == 0){

print “No prime factors.\n”;

}

# Function definition

sub prime_factor{

$n = $_[0];

$f = 0;

if($n == 2){

return $f;

}

else{

for($j = 2; $j < $n-1; $j= $j + 1){

if($n % $j == 0){

$f = 1;

goto RETURN;

}

}

}

RETURN: return $f;

}

**OUTPUT OF THE PROGRAM:**

abhishekgupta@Abhishek:~$ chmod +x prime_factor.pl

abhishekgupta@Abhishek:~$ ./prime_factor.pl

Finding Prime factor of a Number

Enter The Number

12

Number = 12

2 is the Prime Factor of 12

3 is the Prime Factor of 12

abhishekgupta@Abhishek:~$ ./prime_factor.pl

Finding Prime factor of a Number

Enter The Number

15

Number = 15

3 is the Prime Factor of 15

5 is the Prime Factor of 15

abhishekgupta@Abhishek:~$

## LINUX CP COMMAND

## CP COMMAND WITH OPTIONS:

### CP -a COMMAND:

### CP – -archive COMMAND:

### CP – -backup COMMAND:

### CP –f COMMAND:

### CP –i COMMAND:

### CP –n COMMAND:

### CP –s COMMAND:

### CP –u COMMAND:

## LINUX TOUCH COMMAND

## LINUX CAT COMMAND

**cat [OPTION1] [OPTION2] and so on [FILE NAME1] [FILE NAME2] and so on**

**After execution of each command a $ prompt is returned by the terminal.**

## PROPERTIES OF CAT COMMAND

Cat command can be used to do many operations such as creating files, concatenation of files and overwriting of files. Each operation discussed below with proper examples.

### CREATING FILES USING CAT COMMAND

**cat > [FILE NAME]**

### CONCATENATION AND OVERWRITING

## USE OF OPTIONS WITH CAT COMMAND

### CAT –b COMMAND:

### CAT –n COMMAND:

### CAT –E COMMAND:

### CAT –s COMMAND:

### CAT –T COMMAND:

## SHELL PROGRAM TO FIND SUM OF DIGITS OF A NUMBER

n=$number

**OUTPUT OF THE PROGRAM**

THE SUM OF THE NUMBERS IS 15

## PERL PROGRAM TO FIND SUM OF DIGITS OF A NUMBER

#!/usr/bin/perl

# Perl program to find sum of digits of a Number

# Date: 10-06-2015

print “Sum of digits of a Number\n”;

print “Enter the Number\n”;

$number = <STDIN>;

print “Number = $number”;

$sum = 0;

while($number != 0){

$sum = $number % 10 + $sum;

$number = $number / 10;

}

print “Sum of digits of the Number is $sum\n”;

**PERL PROGRAM TO FIND SUM OF DIGITS OF A NUMBER USING SUBROUTINE:**

#!/usr/bin/perl

# Perl program to find sum of digits of a Number using Subroutine

# Date: 10-06-2015

print “Sum of digits of a Number\n”;

print “Enter the Number\n”;

# Input is taken below

$number = <STDIN>;

print “Number = $number”;

# Function definition

sub sum_digits{

$sum = 0;

$n = $_[0];

while($n != 0){

$sum = $n % 10 + $sum;

$n = $n / 10;

}

return $sum;

}

# Function call

$s = &sum_digits($number);

print “Sum of digits of the Number is $s\n”;

**OUTPUT OF THE PROGRAM:**

abhishekgupta@Abhishek:~$ chmod +x sumdigits.pl

abhishekgupta@Abhishek:~$ ./sumdigits.pl

Sum of digits of a Number

Enter the Number

12345

Number = 12345

Sum of digits of the Number is 15

abhishekgupta@Abhishek:~$ ./sumdigits.pl

Sum of digits of a Number

Enter the Number

123456789

Number = 123456789

Sum of digits of the Number is 45

abhishekgupta@Abhishek:~$