Você está na página 1de 32

BASIC UNIX COMMANDS Ex.

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>

Tue Feb 02 10:35:15 IST 2010 [Student@MRKIT68~]$

Options of the Date command:

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

Student pts/1 2010 02 02 17: 13 (:0.0) [Student@MRKIT68~]$

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

Options of the cal command:


S.No 1 2 Options Month Year ($cal 02 2010) Year ($cal 2010) Description To display calendar of specified month of specified year. To display months of a mentioned year.

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.

************

WORKING WITH FILES AND DIRECTORIES

Ex. No: 2
Aim: To write the commands and their syntax working with files and directories. Command Description and their syntax:

Directory command: CREATING A DIRECTORY:


Description: The mkdir (make directory) command is used to create the directory. Syntax: [Student@MRKIT68~]$mkdir <directory name> Example: [Student@MRKIT68~]$mkdir ganguly <Press Enter Key> [Student@MRKIT68~]$cd ganguly <Press Enter Key>

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>

Identifying the current directory:

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

Example: [Student@MRKIT68~]$pwd <Press Enter Key>

File command: CREATING A FILE: cat command


Description: The cat command is used to create a file. Syntax: [Student@MRKIT68~]$cat > [filename] Example: [Student@MRKIT68~]$cat > Siva <Press enter Key> One Two Three <Press ctrl+d>

COPYING A FILE: cp command


Description: The cp (copy) command is used to copy the one file to another file. Syntax: cp <source file> <destination file >

Example: cp siva siva1<Press Enter Key>

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

Word counting command:


Wc command:
Description: The wc (count) command is used to count the word and lines in the files. Syntax: [Student@MRKIT68~]$ wc <filename> Example: [Student@MRKIT68~]$ wc siva <Press enter Key> 3 5 25

[Student@MRKIT68~]$

File access permissions:


The file permissions determine which users on the system have access to a file and the type of access they are allowed.

The file owner:


The file owner is the user who created the file.

The group owner:


The group is the default group to which the owner belongs. By default this is primary group specified in the /etc/passwd file for the file owner.

Other user:
The other users who are not members of the group.

Protecting files /file access permissions:

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.

********************

PIPES AND FILTERS

Ex. No: 3
Aim: To write the commands and their syntax for pipes and filters in unix.

Command Description (PIPES):


Description: A pipe is a way to connect the output of one program to the input of another program. Syntax: [Student@MRKIT68~]$ <command> <filename> | <command> Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key> Grapes Mango Apple Example: [Student@MRKIT68~]$ cat fruits | wc <Press Enter Key> 3 3 15 [Student@MRKIT68~]$

The tee command:


Description: while using pipes we have not seen any output from a command that gets piped into command. another

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

Command Description (FILTERS):


Description: When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output it is referred to as a filter. One of the most common uses of filters is to modify output. Filter commands: tail, head, cut, uniq and paste tail: This command would list the last lines in the file <file name>. Syntax: [Student@MRKIT68~]$ <filter command> <file name> <options> <number> Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key> Apple Orange Banana Grapes Example: [Student@MRKIT68~]$ tail fruits n 2 <Press Enter Key>
Banana Grapes

[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~]$

paste: This command is used to paste several files together

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:

Vi - stands for visual


Vi is a visual editor used to enter and edit text files containing data, documents or program. It displays the contents of files on the screen, and allows the user to add, insert, delete or change parts of the text. it is a full screen editor that allows the user to view and edit entire document at the same time.

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.)

Three modes of vi:


1. Command mode. 2. Insert mode. 3. Escape mode.

Starting with vi:


There are different ways to start vi editor given below: [Student@MRKIT68~]$ vi <Press Enter Key>

[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]

Saving the contents of the file (siva)

When you edit a file vi ,the original file is not disturbed, but only a copy placed in the buffer .

The following commands are used to save a file Command Purpose

:w

Save file and remains in edit mode

:x

Save file and quit from the edit mode

:wq

Save file and quits edit mode

:w file name

Save file under new file

:q

Quit without changes from edit mode

:sh

Escape the Unix shell

COMMANDS USING IN vi EDITOR Ex. No: 4

Aim: To write the command and their syntax used in vi editor.

Command Description:
Syntax: [Student@MRKIT68~]$ <vi file name> Examples: [Student@MRKIT68~]$ vi new <Press Enter Key> Core Worm seed Jewel ~ ~ ~ :wq [Student@MRKIT68~]$

sed: This command is used to editing the contents of a file.


Description: The cat command display the contents of the new file and send that display through a pipe to the sed command. The sed command changed the first occurrence of the eon each line to ZZZ. Syntax: [Student@MRKIT68~]$ <sed > <expression> <file> Demo file: [Student@MRKIT68~]$ cat new <Press Enter Key> Core Worm seed Jewel Example: [Student@MRKIT68~]$ cat new | sed e s/e/ZZZ/ <Press Enter Key> CorZZZ Worm sZZZed JZZZwel

[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~]$

awk: This command is used to manipulating the contents of a file.


Description: The awk command select each record from file containing the word good and displayed the third field. Syntax: <awk> <pattern action> <{file name}> Demo file:[Student@MRKIT68~]$ cat fruits Cherry Mango Strawberry Apple Banana order good ok good good 4 10 4 12 5

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.

MULTIPLICATION TABLE Ex. No: 5b


Aim:

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.

SWAPPING TWO NUMBERS Ex. No:5c


Aim:

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.

COMPARING TWO NUMBERS Ex. No: 5d


Aim:

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.

LARGEST OF THREE NUMBERS Ex. No: 6


Aim:

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 CHECK EVEN OR ODD NUMBER Ex. No: 7a


Aim:

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.

MENU DRIVEN SHELL PROGRAM Ex. No: 8


Aim: To create a shell program for menu driven Algorithm:

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.

LEAP YEAR OR NOT Ex. No: 9


Aim: To write a shell program to check whether the given year is leap year or not.

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.

CHECKING PRIME NUMBER Ex. No: 10


Aim: To write a shell program to check whether the given number is prime number or not. Algorithm:

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.

FIBONACCI SERIES Ex. No:11


Aim: To write a shell script to generate the Fibonacci series for the given limit. Algorithm:

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.

CALCULATING FACTORIAL OF A GIVEN NUMBER Ex. No: 12


Aim: To write a shell script to find the factorial of a given numbers.

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. ****************

FORK FUNCTION Ex. No: 13


Aim: To write the fork function using c program.

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.

SIGNAL IN UNIX Ex. No: 14


Aim:

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.

FILE HANDLING IN UNIX USING C Ex. No: 15


Aim:

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.

*******************

Você também pode gostar