Escolar Documentos
Profissional Documentos
Cultura Documentos
No: 1
Aim: To write the basic commands and syntax by using Unix. Command Description:
Command: date
Description: Date command is used to display the current date and time. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ date <Press Enter Key>
Options of the date command can be used to format the date and time before displaying them. The options are specified within double quote and within the, they must begin with a + symbol. Example: [Student@MRKIT68~]$ date +%a <Press Enter Key> S.No 1 2 3 4 5 6 7 8 9 10 Option %r %a %h %m %d %y %T %H %M %S Description Display time in AM/PM notation Display weekdays as (Sun to Sat) Display month (Jan to Dec) Month of year (in digits) Day of month (in digits) Year (last two digits) Time as HH:MM:SS Hour (00 to 23) Minute(00 to 59) Second(00 to 59)
Command: who
Description: The who command is used to display the names of all users who are currently logged in. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ who <Press Enter Key> Student: 0 2010 02 02 17: 13
Command: who am i
Description: The who am i command is used to display the name of current users. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ who am i <Press Enter Key> Student pts/1 2010 02 02 17: 13 (:0.0) [Student@MRKIT68~]$
Command: cal
Description: The cal command is used to display the calendar with year and month at present. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ cal <Press Enter Key> February 2010 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 9 10 11 12 13
7 8
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [Student@MRKIT68~]$
Command: ls
Description: The ls command is used to list the contents of a directory. Syntax: [Student@MRKIT68~]$ <command> <option> Example: [Student@MRKIT68~]$ ls <Press Enter Key> Desktop fruits tea sat.sh fa.c
Commands: bc Description: The bc command is used for arithmetic and binary calculation.
Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ bc <Press Enter Key> bc 1.06 Copyright 1991 1994, 1997, 1998, 2000 free Software Foundation, This is free software with ABSOLUTELY NO WARRANTY. For details type warranty. 8+4 12 4*7 28 <Press ctrl + D> [Student@MRKIT68~]$ Inc.
Command: man
Description: To display the manual for various commands. Syntax: [Student@MRKIT68~]$ man <command> Example: [Student@MRKIT68~]$ man cal <Press Enter Key> The above example have list out how to use the calendar command.
Command: history
Description: The history command is used to display the history of commands what you have typed. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ history <Press Enter Key> 1 date 2 who 3 who am i 4 cal 5 history [Student@MRKIT68~]$
Command: clear
Description: The clear command is used to clear the screen. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ clear <Press Enter Key>
Result: Thus the basic command in Unix has been executed and output was verified.
************
Ex. No: 2
Aim: To write the commands and their syntax working with files and directories. Command Description and their syntax:
MOVING A DIRECTORY:
Description: The mv (move) command is used to move the one directory to another directory. Syntax: [Student@MRKIT68~]$ mv <source_ dir> <destination_ dir> Example: [Student@MRKIT68~]$ mv ganguly ganguly1 <Press enter Key>
REMOVING A DIRECTORY:
Description: The rmdir (remove directory) command removes the specified directory. Syntax: [Student@MRKIT68~]$rmdir <directory name> Example: [Student@MRKIT68~]$rmdir ganguly <Press Enter Key>
CHANGING A DIRECTORY:
Description: The cd command is used to change the current working directory to another directory name. Syntax: [Student@MRKIT68~]$cd <directory name> Example: [Student@MRKIT68~]$cd ganguly <Press Enter Key>
pwd command:
Description: The pwd (present working directory)command is used to display the full path name of the current directory. Syntax: [Student@MRKIT68~]$pwd
MOVING A FILE:
Description: The mv (move) command is used to move the one file to another file. Syntax: [Student@MRKIT68~]$mv <source file> <destination file.> Example: [Student@MRKIT68~]$mv ganguly ganguly1 <Press enter Key>
REMOVING A FILE:
Description: The rm (remove) command is used removes the specified file. Syntax: [Student@MRKIT68~]$ rm <file name> Example: [Student@MRKIT68~]$rm siva <Press enter Key>
SORTING A COMMAND
Description: The sort command is used to sorts, merges, or compares all the lines from the given file. Syntax: [Student@MRKIT68~]$ sort filename Example: [Student@MRKIT68~]$ sort siva <Press enter Key> Example1: [Student@MRKIT68~]$ sort siva siva1 <Press enter Key>
COMPARING A FILE:
Description: The cmp (compare) command is used to compare the two files. Syntax: [Student@MRKIT68~]$ cmp <file1> <file2> Example: [Student@MRKIT68~]$ cmp siva siva1
[Student@MRKIT68~]$
Other user:
The other users who are not members of the group.
Permission associated with files can be changed only by the owner of the file .using the chmod, access permission is associated with a file or directory can be changed access permission can be set using the chmod command.
Syntax: [Student@MRKIT68~]$ chmod <access _permission> <filename /directory _ name> Example: [Student@MRKIT68~]$ chmod 777 siva <Press enter Key>
Result:
Thus the files and directory commands has been executed and output was verified.
********************
Ex. No: 3
Aim: To write the commands and their syntax for pipes and filters in unix.
Where tee command copies the incoming data on input to output. In the mean time it saves a copy in the specified file. Syntax: [Student@MRKIT68~]$ <command> | tee <file name> | command Example: [Student@MRKIT68~]$ date | tee fruits | wc <Press Enter Key> 1 6 29
[Student@MRKIT68~]$
head: This command would display the first lines of file <file name>.
Syntax: [Student@MRKIT68~]$ <filter command> < -number> <file name> Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key> Apple Orange Banana Grapes Example: [Student@MRKIT68~]$ head fruits -2 fruits <Press Enter Key> Apple Orange [Student@MRKIT68~]$
cut : This command would cut the content of file according to the options in the command
Syntax: [Student@MRKIT68~]$ <cut command> <options> <file name> Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key> Apple Orange Banana Grapes Example: [Student@MRKIT68~]$ cut c1 fruits <Press Enter Key> A O B G [Student@MRKIT68~]$
uniq: This command prints those lines which are unique and remove the duplicate lines.
Syntax: [Student@MRKIT68~]$ <uniq command> <filename> Demo file: [Student@MRKIT68~]$ cat hello <Press Enter Key> Arun Suresh Sathya Sathya Ramesh Example: [Student@MRKIT68~]$ uniq hello Arun Suresh Sathya Ramesh [Student@MRKIT68~]$
Syntax: [Student@MRKIT68~]$ <paste command> <file name1> <file name2> Demo file: [Student@MRKIT68~]$ cat veg Onion Brinjal Demo file: [Student@MRKIT68~]$ cat dept CSE CIVIL Example: [Student@MRKIT68~]$ paste veg dept Onion Brinjal CSE CIVIL
[Student@MRKIT68~]$
Result:
Thus the commands used in pipe and filter has been executed and output was verified
*************
VI EDITOR:
Features of vi
vi is very easy to learn and more powerful exciting features vi is great speed which is missing in other editor vi is case sensitive
Example: M & m (the upper case M& lower case m are completely different.)
[Student@MRKIT68~]$ vi <file name> <Press Enter Key> Example: [Student@MRKIT68~]$ vi Siva <Press Enter Key> (Vi editor screen will be appeared as shown below) ~ ~ ~ ~ ~ ~ ~ ~ Siva [new file]
When you edit a file vi ,the original file is not disturbed, but only a copy placed in the buffer .
:w
:x
:wq
:w file name
:q
:sh
Command Description:
Syntax: [Student@MRKIT68~]$ <vi file name> Examples: [Student@MRKIT68~]$ vi new <Press Enter Key> Core Worm seed Jewel ~ ~ ~ :wq [Student@MRKIT68~]$
[Student@MRKIT68~]$
grep: This command is used to find the matching pattern in the contents of file
Syntax: <grep> <pattern to find> <file name> Demo file:[Student@MRKIT68~]$ cat birds <Press Enter Key>
Pigeon Bulbul Parrot Duck Example:[Student@MRKIT68~]$ grep Bulbul birds <Press Enter Key> Bulbul [Student@MRKIT68~]$
Example:[Student@MRKIT68~]$ awk /good/ {print $3} fruits <Press Enter Key> 10 12 5 [Student@MRKIT68~]$
********************
SHELL PROGRAMS
FINDING SUM OF TWO NUMBERS Ex. No: 5a
Aim: To write a shell program to find the sum of two numbers. Algorithm: Step 1: Start the program. Step 2: Get input values a b. Step 3: Use the expression, a+b Step 4: Display the result. Step 5: Stop the program.
Shell Program:
clear echo Enter the values of A and B: read a b echo The Sum is: expr $a + $b
Output:
[Student@MRKIT68~]$sh sum.sh Enter the values of A and B: 45 23 The Sum is: 68 [Student@MRKIT68~]$
Result:
Thus the shell program to calculate the sum of two numbers has been executed and output was verified.
To write a shell script to display the multiplication table of a given number. Algorithm: Step1: Start the program. Step2: Input number n. Step3: If i<10 then m=n*i. Step4: Print the value of i, n and m in multiplication table format. Step5: Increment the value of i. Step6: Stop the program.
Shell program:
echo Enter the number read n for i in 1 2 3 4 5 6 7 8 9 10 do m=$(( $n * $i )) echo $i * $n =$m done
Output:
[Student@MRKIT68~]$sh mult.sh Enter the number 10 1*10=10 2*10=20 3*10=30 4*10=40 5*10=50 6*10=60 7*10=70 8*10=80 9*10=90 10*10=100 [Student@MRKIT68~]$
Result:
Thus the shell program for displaying the multiplication table has been executed and output was verified.
To write a shell script to interchange two numbers. Algorithm: Step 1: Start the program. Step 2: Input number a and b. Step 3: Do the following assignments C=a a=b b=c. Step 4: Print a and b values. Step 5: Stop the program.
Shell program:
echo Enter a read a echo Enter b read b c=$a a=$b b=$c echo After Swapping echo Number a is $a echo Number b is $b
Output:
[Student@MRKIT68~]$sh swap.sh Swapping using temporary variable Enter a 25 Enter b 50 After swapping Number a is 50 Number b is 25 [Student@MRKIT68~]$
Result:
Thus the Shell program for swapping two numbers has been executed and output was verified.
To write a shell program to compare two numbers. Algorithm: Step 1: Start the program. Step 2: Input two numbers a and b. Step 3: Check a and b are equal. Step 4: If a and b are equal then print numbers are equal. Otherwise numbers are not equal. Step 5: Stop the program.
Shell program:
echo Enter two numbers read a b if test $a eq $b then echo Numbers are equal else echo numbers are not equal fi
Output:
[Student@MRKIT68~]$sh comp.sh Enter two numbers 34 54 Numbers are not equal. [Student@MRKIT68~]$
Result:
Thus the shell program to compare the two numbers has been executed and output was verified.
To create a shell program to find the largest of three numbers. Algorithm: Step 1: Start the program. Step 2: Get input values a, b,c. Step 3: Use the if condition. Step 4: if [a > b] , & [a > c] Print a is greater (or) Print c is greater. Step 5: if[b > c] Print b is greater (or) Print c is greater. Step 6: Close if loop as fi Step 7: Display the output and stop the program.
Shell Program:
echo Enter any three numbers read a read b read c if [ $a gt $b a $a gt $c ] then echo The largest number is $a elif [ $b gt $c] then echo The largest number is $b else echo The largest number is $c fi
Output:
[Student@MRKIT68~]$ sh big.sh Enter any three numbers 32 13 33 The largest number is 33 [Student@MRKIT68~]$
Result:
Thus the shell program to find the largest of three numbers has been executed and output was verified.
To write a shell program to check the given number is even or odd Algorithm: Step 1: Start the program. Step 2: Enter the number. Step 3: Find c by using expression. Step 4: If c is equal to zero then number is even . Step 5: else number is odd. Step 6: Display the result. Step 7: Stop the program.
Shell program:
echo Enter a number read n c=$(( $n % 2 )) if [ $c eq 0 ] then echo This is an even number. else echo This is an odd number. fi
Output:
[Student@MRKIT68~]$ sh eo.sh Enter a number. 6 This is an even number. [Student@MRKIT68~]$
Result:
Thus the shell program to check the number is even or odd has been executed and output was verified. CHECKING POSITIVE OR NEGATIVE NUMBER
Ex. No: 7b
Aim: To write a shell program to check the given number is positive or negative number.
Algorithm: Step 1: Start the program. Step 2: Input number n. Step 3: Is n greater than zero. If yes print positive number and go to step 5. Step 4: If no print negative number. Step 5: Stop the program.
Shell Program:
echo Enter a number read n if test $n gt 0 then echo Positive number elif test $n lt 0 then echo Negative number else echo zero fi
Output:
[Student@MRKIT68~]$ sh pn.sh Enter a number 10 Positive number. [Student@MRKIT68~]$
Result:
Thus the shell program to check positive or negative number has been executed and output was verified.
Step 1: Start the program. Step 2: Using case structure enter the menu one by one. Step 3: If the choice 1 is selected, todays date will be displayed. Step 4: If the choice 2 is selected, present working directory will be displayed. Step 5: If the choice 3 is selected, list the users logged will be displayed. Step 6: If the choice 4 is selected, current month is displayed. Step 7: If the choice 5 is selected, we can exit from the program. Stop 8: Stop the program.
Shell program:
while true do echo Menu echo 1. Todays date echo 2. Present working directory echo 3. List of users echo 4. Display current month echo 5. Exit echo Enter your choice: read choice case $choice in 1) date;; 2) pwd;; 3) ls;; 4) cal;; 5) exit;; 6) echo Invalid choice esac done
Output:
[Student@MRKIT68~]$ sh menu.sh Menu
1. Todays date 2. Present working directory 3. List of users 4. Display current month 5. Exit Enter your choice: 2 /home/student
Menu
1. Todays date 2. Present working directory 3. List of users 4. Display current month 5. Exit [Student@MRKIT68~]$
Result:
Thus the shell program of menu driven program has been executed and output was verified.
Algorithm: Step 1: Start the program. Step 2: Enter the year. Step 3: Check the condition, then print leap year or not. Step 4: Display the result Step 5: Stop the program.
Shell program:
echo Enter the year read year a =$(( $year % 4 )) if [ $a eq 0 ] then echo $year is a leap year else echo $year is not a leap year fi
Output:
[Student@MRKIT68~]$sh leap.sh Enter the year 2012 2012 is a leap year. [Student@MRKIT68~]$
Result:
Thus the shell program to check whether the year is leap year or not has been executed and output was verified.
Step 1: Start the program. Step 2: Input number n. Step 3: Assign i = 2, j = 0. Step 4: Is i < n then r = n % i. Otherwise go to step 8. Step 5: Is r = 0 then increment I and j value by 1. Otherwise go to step 6. Step 6: Increment i value one by one. Step 7: Repeat step 4 to 6. Step 8: Is j = 0 then print number is prime and go to step 10. Step 9: Is j! = 0 then print number is not a prime number. Step 10: Stop the program.
Shell Program:
echo Enter a number read n i=2 j=0 while test $i lt $n do r=$(( $n % $i )) if test $r eq 0 then j=$(( $j + 1 )) fi i=$(( $i +1 )) done if test $j eq 0 then echo Number is prime else echo Number is not prime fi
Output:
[Student@MRKIT68~]$sh prime.sh Enter a number 10 Number is not Prime. [Student@MRKIT68~]$ Result: Thus the shell program to check the given number is prime number or not.
Step 1: Start the program. Step 2: Input number n. Step 3: Assign a=01,b=1 and print a,b values. Step 4: Assign i=2. Step 5: If i<n then go to step 6 otherwise go to step 9. Step 6: Calculate c = a + b, i= i +1,assign a = b, b = c. Step 7: Print the value of c. Step 8: Repeat steps 5 to 7. Step 9: Stop the program.
Shell program:
echo Program to generate Fibonacci series echo Enter the range to be displayed read n a=0 b=1 echo fibonacci series echo $a echo $b i=2 while test $i -lt $n do c=$(( $a + $b )) echo $c i=$(( $i + 1 )) echo $c i=expr $i + 1 a=$b b=$c done
Output:
[Student@MRKIT68~]$sh fibo.sh Program to generate Fibonacci series Enter the range to be displayed 10 Fibonacci series 0 1 1 2 3 [Student@MRKIT68~]$
Result:
Thus the shell program to print the Fibonacci series for the given range has been executed and output was verified.
Algorithm: Step 1: Start the program. Step 2: Input number n. Step 3: Initialize the value of i and factorial f to 1. Step 4: Is the value of i is less than the number find the value of factorial by multiplying i and f. Step 5: Increment the value of i by 1. Step 6: Print the result. Step 7: Stop the program.
Shell program:
Echo Enter a number read n i=1 f=1 if test $n -eq 0 then echo factorial is 1 else while test $i le $n do f=$(( $f * $i )) i=$(( $i + 1 )) done echo Factorial is $f fi
Output
[Student@MRKIT68~]$sh fact.sh Enter a number 5 Factorial is 120 [Student@MRKIT68~]$
Result:
Thus the Shell program to find the factorial of the given number has been executed and output was verified. ****************
Algorithm: Step 1: Declare the input. Step 2: Use if loop. Step 3: Declare condition. Step 4: Execute the output.
Program:
#include <stdio.h> main() { int I; int pid; pid=fork(); if(pid==0) { for(i=0;i<=5;i++) { printf(\n I am in child %d,i) sleep(2); } } else { for(i=0;i<10;i++) { printf(\n I am in parent %d,i); sleep(1); } } }
Output:
[Student@MRKIT68~]$ cc fork.c
[Student@MRKIT68~]$ ./a.out
I am in parent0 I am in child0 I am in parent1 I am in child1 I am in parent2 I am in parent3 I am in child2 I am in parent4 I am in parent5 I am in child3 I am in parent6 I am in parent7 I am in child4 I am in parent8 I am in parent9 I am in child5
[Student@MRKIT68~]$
Result:
Thus the C program in Unix has been executed and output was verified.
To write the signal in Unix using C program. Algorithm: Step 1: Declare the integer. Step 2: Use for(i=1;i<=10;i++) Step 3: Close the loop. Step 4: Execute the output.
Program:
#include<stdio.h> #include<signal.h> main() { void change(); int i; signal(SIGINT,change); for(i=1;i<=10;i++) { printf(\n unix signal program); sleep(1); } } void change() { printf(\n Test the signal); } }
Output:
[Student@MRKIT68~]$ cc signal.c [Student@MRKIT68~]$ ./a.out unix signal program unix signal program unix signal program Test the Signal unix signal program unix signal program unix signal program unix signal program Test the Signal unix signal program unix signal program unix signal program [Student@MRKIT68~]$
Result:
Thus the C program in Unix has been executed and output was verified.
To write the C program for File Handling in Unix using C Algorithm: Step 1: Declare the integer fd. Step 2: Use if command. Step 3: Declare the conditions. Step 4: Close if command.
Program:
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> main() { int fd; char fname[20],buf1[100],buf2[]=hai how are you; printf(\n enter input file name:) scanf(%s,fname); if((fd=creat(fname,S_IRWXU))<0) { printf(\n error in file creation); exit(0); } else { write(fd,buf2,20); printf(\n content is written); close(fd); fd=open(fname,O_RDONLY); read(fd,buf1,20); printf(\n the file content is :\n %s,buf1); printf(\n); close(fd); } }
Output:
[Student@MRKIT68~]$ cc file1.c
[Student@MRKIT68~]$ ./a.out enter input file name:first.out content is written the file content is : Hai how are you [Student@MRKIT68~]$ [Student@MRKIT68~]$ vi first.out Hai how are you^@^A^@^@^@
Result:
Thus the c program in Unix has been executed and output was verified.
*******************