Linux Lab V MCA

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

1.

Write a program to implement any 10 linux commands

#!/bin/bash echo "Welcome to Linux" ls -l pwd cat demo mkdir l1 echo "Directory Created" cp demo demo1 echo "successfully copied" mv demo dd echo "renamed to new file name" rm demo1 echo "file deleted" rmdir l1 echo "Directory removed" date 2. Write a Linux program to copy a file. #!/bin/bash echo "Enter source file" read src echo "Enter destination" read dest if [ -s $src ] then cp $src $dest echo "File copied" else echo "Source file not found" fi

3. Write a Linux program to create 5 child processes and kill these processes in both the orders (Ascending & Descending) #include<stdio.h> #include<signal.h> main() { int i, pid[5],par=1,killord; for(i=0;i<5 && i>0;i++) { pid[i]=fork(); par=pid[i]; } if(par==0) { pause(); } else { printf("Five processes created are : \n"); for(i=0;i<5;i++) { printf("Child processes %d with pid->%d\n",i,pid[i]); } while(1) { printf("In what order do you want to kill the process?\n"); printf("1. Ascending order\n"); printf("2. Descending order\n"); printf("Enter your choice:"); scanf("%d",&killord); if(killord==1) {

for(i=0;i<5;i++) { printf("Killing child processes with pid[%d] = %d\n",i, pid[i]); sleep(2); kill(pid[i],SIGTERM); } break; } else if (killord==2) { for(i=4;i>=0;i--) { printf("Killing child processes with pid %d\n",pid[i]); sleep(2); kill(pid[i],SIGTERM); } break; } else { printf("Invalid choice!\n"); } } } } 4. Write a Linux program to store data in a file and compress it. #!/bin/bash echo "Enter file name" read fname cat > $fname echo "file created successfully" gzip $fname echo "file compressed" ls -s $fname.gz

5. Write a Linux program to control the process and assign security. #!/bin/bash clear ans='y' while [ $ans = 'y' ] do echo "PROGRAM TO CONTROL THE FILE PERMISSIONS OF A GIVEN FILE" echo -e "Enter the file name: \c" read fname if [ -f $fname ] then echo "Current file attributes" ls -l $fname echo "New File Permission" echo "1)SYMBOLIC MODE" echo "2)ABSOLUTE MODE" echo -e "Enter the option: \c" read opt case $opt in 1) echo "Enter the permission for user" read us echo "Enter the permission for group" read gr echo "Enter the permission for others" read ot chmod u=$us,g=$gr,o=$ot $fname ;; 2) echo "permission: (7 rwx,6 rw,5rx,4r,3 wx,1x)" echo -e "Enter the permission: \c" read per chmod $per $fname ;; *) echo "Invalid choice" ;;

esac if [ $? -eq 0 ] then echo "Updated successfully" echo "Current file attributes" ls -l $fname fi echo "Any more file? (y/n)" read ans else echo "The file is not exists" fi done 6. Write a shell program to find whether a given number is prime number or not.

#prime echo "Enter the number" read num i=2 while [ $i -lt $num ] do if [ $( expr $num % $i ) -eq 0 ] then echo "Not a prime number" exit else i=$( expr $i + 1 ) fi done echo "Prime Number"

7. Write a Linux program for sending and handling mail. #!/bin/bash echo "Enter the user name" read uname mail $uname echo "do u want to send more mails (1/0)" read ch while [ $ch -eq 1 ] do echo " enter user name " read name mail $name echo "wanna send more (1/0)" read ch done 8. Write a Linux program to create two directories and store 10 files in one directory. Using file related commands transfer all files to other directory. #!/bin/bash echo "Enter the names of two directories" read dir1 read dir2 mkdir $dir1 $dir2 i=1 cd $dir1 while [ $i -le 2 ] do echo "Enter the names of files" read f1 touch $f1 mv $f1 /root/$dir2 i=$( expr $i + 1 ) done

9. Write a program to demonstrate IPC using pipes. #include<stdio.h> #include<string.h> #include<sys/types.h> int size; int main() { char msg[80]; char buf[80]=""; int p[2],pid,i; pipe(p); pid=fork(); if (pid>0) { // printf("Parent request : Enter the message:"); size=read(0,msg,80); write(p[1],msg,size); wait((char *)0); exit(0); } else { read(p[0],buf,80); printf("Response from child: Message read from the pipe is "); printf("%s\n",buf); exit(0); } }

10. Write a program to implement date command using EXEC, FOR, WAIT, EXIT command. #!/bin/bash echo "Demonstration of FOR command:"

echo "Enter the number " read num i=1 prod=1 for i in 1 2 3 4 5 6 7 8 9 10 do prod=$( expr $i '*' $num ) echo $num "*" $i "=" $prod done echo "Demonstration of EXIT Command" echo "Enter the file name" read fname if [ -s $fname ] then echo "File found successfully ..Exiting .... " exit else echo "File not found" fi #jobs #echo "Demonstration of WAIT command " #wait echo "Demonstration of EXEC Command " echo "This will terminate the session " exec ls

11. Write a program to demonstrate any four signals. #include<stdio.h> #include<signal.h> #include<time.h> #include<sys/types.h> #include<curses.h> #define TIMER 1

void showtime() { long t; struct tm *tp; time(&t); tp=localtime(&t); printf("Time: %d:%d:%d\n",tp->tm_hour,tp->tm_min,tp->tm_sec); interrupted(); alarm(TIMER); } void interrupted() { printf("Interrupted\n"); } int main() { signal(SIGALRM,showtime); signal(SIGQUIT,interrupted); alarm(TIMER); for(;;); }

12. Write a program to display a given number in terms of words (e.g. 123 as ONE, TWO, THREE) using switch command. #!/bin/bash echo "Enter any number" read num sum=0 while [ $num -gt 0 ] do rem=$( expr $num % 10 ) sum=$( expr $sum '*' 10 + $rem ) num=$( expr $num / 10 ) done

while [ $sum -ne 0 ] do a=$( expr $sum % 10 ) sum=$( expr $sum / 10 ) case $a in 0) echo "Zero ";; 1) echo " One";; 2) echo " Two";; 3) echo " Three";; 4) echo " Four";; 5) echo " Five";; 6) echo " Six";; 7) echo " Seven";; 8) echo " Eight";; 9) echo " Nine";; *) exit;; esac done 13. Write a program to implement the following commands. 1) write 2) read 3) wall 4) grep 5) chmod #!/bin/bash echo "Enter the user name" read uname write $uname echo "Enter message for all users" read mesg wall $mesg echo "Enter file name & its contents" read fname cat > $fname echo "enter the word to search" read pat grep $pat $fname chmod u+wx $fname ls -l

You might also like