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

# WRITE A SHELL PROGRAM TO CHECK A NUMBER IS EVEN OR ODD
# DATE: 28-05-2015
clear
echo “Enter the Number”
read n
echo “Number = $n”
x=` expr $n % 2 `
if [ $x -eq 0 ]
then
            echo”Number is Even.”
else
            echo”Number is Odd.”
fi
OUTPUT OF THE SHELL PROGRAM:
abhishekgupta@Abhishek:~$ sh even.sh
Enter the Number
1
Number = 1
Number is Odd.
abhishekgupta@Abhishek:~$

PERL PROGRAM TO CHECK EVEN AND ODD

#!/usr/bin/perl
# Perl program to check a Number is Even or Odd
# Date: 09-06-2015
print “Checking a Number is Even or Odd\n”;
print “Enter the Number\n”;
$number = <STDIN>;
print “Number = $number”;
if($number % 2 == 0){
print “Number is Even.\n”;
}
else{
print “Number is Odd.\n”;
}

PERL PROGRAM TO CHECK EVEN AND ODD USING SUBROUTINE:

#!/usr/bin/perl
# 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

sub even_odd{
$n = $_[0];
$flag = 0;
if($n % 2 == 0){
$flag = 1;
}
return $flag;
}

# Function call for checking Even or Odd
$f = &even_odd($number);
if($f == 1){

print “Number is Even.\n”;

}
else{

print “Number is Odd.\n”;

}

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

# !/bin/sh
# WRITE A SHELL PROGRAM TO GENERATE ARMSTRONG NUMBERS
# WITH IN A GIVEN RANGE
# DATE: 27-05-2015
clear
echo “Enter the starting value of Range.”
read i
echo “Enter the Ending value of Range.”
read j
echo “Armstrong Numbers within” $i “and” $j “are as follows:”
s=0
while [ $i -ne $j ]
do
            n=$i
            while[ $n -ne 0 ]
            do
                        x=` expr $n % 10 `
                        s=` expr $x * $x * $x + $s `
                        n=` expr $n / 10 `
            done
            if[ $i -eq $s ]
            then
                        echo$i
            fi
            s=0
            i=` expr $i + 1 `
done
OUTPUT OF THE PROGRAM:
abhishekgupta@Abhishek:~$ sh arms.sh
Enter the starting value of Range.
100
Enter the Ending value of Range.
500
Armstrong Numbers within 100 and 500 are as follows:
153
370
371
407
abhishekgupta@Abhishek:~$

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

# UNIX Shell Program to check for Prime and Non Prime
# Date: 22-04-2015
# Save the file as prime.sh
echo “Enter the Number”
read a
echo “Number: $a”
flag=0
i=2
while [ $i -lt $a ]
do
            x=` expr $a % $i `
            if[ $x -eq 0 ]
            then
                        flag=1
                        break
            fi
            i=` expr $i + 1 `
done
if [ $flag -eq 1 ]
then
            echo”Number $a is Non Prime”
else
            echo”Number $a is Prime”
fi
OUTPUT OF THE PROGRAM:
abhishekgupta@Abhishek:~$ sh prime.sh
Enter the Number
4
Number: 4
Number 4 is Non Prime
abhishekgupta@Abhishek:~$ sh prime.sh
Enter the Number
3
Number: 3
Number 3 is Prime
abhishekgupta@Abhishek:~$

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

UNIX cp command or LINUX cp command is mainly used to copy the content of a file into another exiting file. The forms of the command are written as:
cp [FILENAME1] [FILENAME2]
cp SOURCE FILES DIRECTORY
cp –t DIRECTORY SOURCE FILES
Let us take an example:
Here we will create two new files using cat command.
$ cat > test1 (Press enter)
DBMS also known as Data Base Management System or database system is a system which stores data in a correct manner in a computer system. Database is referred as the collection of data. The data base system manages all information which may be necessary and important while decision making processes which are involved in management of the company which uses it.
$
Now the file test1 has been created. Now create the second file. Here second file test2 will be empty.
$ cat > test2 (Press enter)
$
Now the two files have been created. Now we will use cp command to copy the content of file test1 into file test2.
$ cp test1 test2 (Press enter)
Here, the content of file test1 is copied to file test2. For our conformation we can see the content of file test2 as:
$ cat test2 (Press enter)
DBMS also known as Data Base Management System or database system is a system which stores data in a correct manner in a computer system. Database is referred as the collection of data. The data base system manages all information which may be necessary and important while decision making processes which are involved in management of the company which uses it.
$
If the file test2 were not empty then its content would be replaced by the content of file test1. This is explained with an example below. So to do this let us create another file say test3 having some content. Here we will replace the content of file test2 with the content of file test3.
$ cat > test3 (Press enter)
Types of Database Model are as:
Relational data model
Network data model
Hierarchical data model
$
Now replace the content of file test2 with the content of file test3.
$ cp test3 test2 (Press enter)
The content of file test2 has been changed. Now see the content of file test2.
$ cat test2 (Press enter)
Types of Database Model are as:
Relational data model
Network data model
Hierarchical data model
$
cp command can be used to copy one or more files to a directory. The general form for this operation is showed above. For example suppose sample1 and sample2 are two files and newdir is directory. This can be done in the following two ways.
$ cp sample1 sample2 newdir (Press enter)
Or we can write as:
$ cp –t newdir sample1 sample2 (Press enter)
Now the files sample1 and sample2 are copied to the directory newdir. This operation only works if the file and directory are at same location otherwise we have to specify the path.

CP COMMAND WITH OPTIONS:

CP -a COMMAND:

This option is used to copy the attributes of one file and applies to the second file. Here attributes means read, write and execute permission of a file. Suppose sample1 and sample2 are two files. We can see the current attributes of each file with the use of ls command.
$ ls –l sample1 sample2 (Press enter)
-rwxrw-r- –    1         user1              User1 4         Mar    21       10:53             sample1
-rw-rw-r- –     1         user1              User1 8         Mar    21       10:53             sample2
$
We can see here the attributes of file sample1 is -rwxrw-r- – and of file sample2 is –rw-rw-r- -.
$ cp –a sample1 sample2
Now display again the attributes of file sample1 and sample2.
$ ls –l sample1 sample2
-rwxrw-r- –   1         user1              User1 4         Mar    21       10:53             sample1
-rwxrw-r- –   1         user1              User1 8         Mar    21       10:53             sample2
$
Instead of writing –a we can also perform this operation using – -attributes-only.
$ cp – -attributes-only sample1 sample2.

CP – -archive COMMAND:

It copies the files of an archive directory and creates a new archive directory and copy the files of archive directory.
$ cp – -archive sample1.zip sample2
This command will create a new archive directory with name sample2 and it will contain all the files of the sample1.zip.

CP – -backup COMMAND:

This option is used to copy the content of first file to second file and also create a backup file of the destination file. If sample1 and sample2 are two files and sample1 is the source file and sample2 is the destination file then – -backup option will create a new file with name sample2~ which will content the previous content of file sample2.
$ cp – -backup sample1 sample2

CP –f COMMAND:

It is used to copy the first file to the second file forcibly. It can be used as:
$ cp –f sample1 sample2

CP –i COMMAND:

This option asks us before overwriting or copying. This can be done as:
$ cp –i sample1 sample2
cp: Overwrite ‘sample2’ ?
Write yes or no after the question mark. If you want to overwrite then write yes else write no.

CP –n COMMAND:

This option will stop overwriting.
$ cp –n sample1 sample2
Here the content of sample1 will not be overwritten into file sample2.

CP –s COMMAND:

This option creates the symbolic link of the file. Suppose sample1 is file and this option will create the symbol link file.
$ cp –s sample1 sample2

CP –u COMMAND:

This option will copy only when if the source file is newer than the destination file or when the destination file is missing.
$ cp –u sample1 sample2

LINUX TOUCH COMMAND

In UNIX or LINUX touch command is used to create empty files or we can say zero byte file. The general form of the command is as:
For creating a single file,
$ cat [FILENAME]
For creating multiple files,
$ cat [FILENAME1] [FILENAME2] [FILENAME3] ………
For example:
$ touch test (Press enter)
We can see test file using ls command
$ ls test (Press enter)
test
Now a file test has been created. The size of the file is zero byte. We can create multiple zero byte files with touch command at a time.
$ touch test1 test2 test3 test4 test5
Here five files test1, test2, test3, test4 and test5 have been created and each file contains nothing.
We can again use ls command to see the list of the files.
$ ls *test (Press enter)
test1    test2    test3    test4    test5
In using * is a Meta character which can be used with more than one characters to search pattern matching files.
If we want to create non-empty file we have to use either cat command or vi command.

LINUX CAT COMMAND

In UNIX operating system, cat command is an internal command which is mainly used to display the content of a file stored in the system created by any user or by system itself. The general form of cat command is as:

cat [OPTION1] [OPTION2] and so on [FILE NAME1] [FILE NAME2] and so on
Suppose test is a text file stored in the home directory and the file test contains the following lines:
Hello!
How are you?
Now to display the content of file test we use cat command in the terminal as:
$cat test
And press enter to run the cat command. The output of this command is shown below.
Hello!
How are you?
$
After execution of each command a $ prompt is returned by the terminal. 
Cat command can also be used to display the contents of more than one file at a time. Suppose sample1, sample2 and sample3 are three text files.
Now content of file sample1 is as:
Hello!
Content of file sample2 is as:
How are you?
And content of file sample3 is as:
I am fine.
Then the contents of these three files can be displayed as:
$cat sample1 sample2 sample3 (Press enter)
Hello!
How are you?
I am fine.
$
If we change the order of the files then the order of contents displayed in output will be changed. Thus if we write the cat command as then output will be different.
$cat sample1 sample3 sample2 (Press enter to run the command)
Hello!
I am fine.
How are you?
$
In this output content of first file is shown then that of third and at last the content of second file is shown.

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 command can also be used to create files. To create a file we write the command as:
cat > [FILE NAME]
Let us create a file using this cat command.
$cat > newsample (Press enter to run the command)
Hello!
I am Ram.
Who are you?
$
After finishing your content Press enter then press control +d to get out of file.

CONCATENATION AND OVERWRITING

Cat command can be used to overwrite the content of a file from content of another file. Let us create two file say test1 and test 2.
$ cat > test1(Press enter)
Hello!
I am Abhishek.
$
Now create the second file
$ cat > test2 (Press enter)
Hi!
I am Ram.
$
Now we have created the files, thus overwrite file test2 with file test1.
$ cat test1 > test2
Now the file test2 have been overwritten. To see the content of test2 write in terminal as $ cat test2 (Press enter)
Hello!
I am Abhishek.
$
Similarly we overwrite file test1 with test2.
Similarly we can overwrite a file with more than one file at a time.
Let test3 is another text file.
$ cat test3 (Press enter)
Hello!
I am karan
$
Now we can overwrite file test3 with file test1 and test2.
$ cat test1 test2 > test3
Now the content of test3 is changed by the contents of test1 and test2. We can see the content of test3 as:
$ cat > test3 (Press enter)
Hello!
I am Abhishek.
Hi!
I am Ram.
$
Now concatenating two files means adding the content of one file at the end of another file. Let us understand through an example.
$ cat test1 >> test2 (Press enter)
Here content of test1 will be added at the end of test2. Let us see the content of test2.
$ cat test2 (Press enter)
Hi!
I am Ram.
Hello!
I am Abhishek.
$
Similarly we can use multiple file at a time.
$ cat test1 test2 >> test3 (Press enter)
$ cat test3 (Press enter)
Hello!
I am karan
Hello!
I am Abhishek.
Hi!
I am Ram.
$

USE OF OPTIONS WITH CAT COMMAND

CAT –b COMMAND:

This option is used to count all lines except empty lines. Suppose test is a text file and its content is as:
Hello!
How are you?
I am fine.
And you
I am also fine.
When we use option –b with cat command output will as follows:
$ cat –b test (Press enter)
            1 Hello!
            2 How are you?
            3 I am fine.
            4 And you
            5 I am also fine.
$
Thus we can see empty lines have not been counted.

CAT –n COMMAND:

This option counts all lines of a file including empty lines too. When we use option –n with cat command output will as follows:
$ cat –n test (Press enter)
1 Hello!
2 How are you?
3 I am fine.
4
5 And you
6
7 I am also fine.
$

CAT –E COMMAND:

This option is used to place $ at the end of each line. When we use –E output is as follows:
$ cat –E test (Press enter)
Hello!$
How are you?$
I am fine.$
$
And you$
$
I am also fine.$
$

CAT –s COMMAND:

This option is used to remove repeated empty lines. Before we use this command let us modify our file test as:
Hello!
How are you?
I am fine.
And you
I am also fine.
Now we have modified the file, let us use the command:
$ cat –s test (Press enter)
Hello!
How are you?
I am fine.
And you
I am also fine.
$
As we can see from the output in the file test there are three empty lines after the second line and in output there is only one empty line after the second line.

CAT –T COMMAND:

This option is used to put ^I at the place of TAB characters. Now once again modify the file test.
Hello!
How are you?
I am fine.
And you
I am also fine.
Now test this option,
$ cat –T test (Press enter)
Hello!
^I How are you?
I am fine.
^I And you
^I I am also fine.
$

SHELL PROGRAM TO FIND SUM OF DIGITS OF A NUMBER

#! /bin/sh
# Shell program to find sum of digits of a number
# Date: 05-03-2015
echo “ENTER THE NUMBER”
read number
echo “THE NUMBER IS $number”

n=$number

sum=0
while [ $n -ne 0 ]
do
            sum=` expr $n % 10 + $sum `
            n=` expr $n / 10 `
done
echo “THE SUM OF THE NUMBERS IS $sum”
 
OUTPUT OF THE PROGRAM
ENTER THE NUMBER
12345
THE NUMBER IS 12345

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:~$