0% found this document useful (0 votes)
28 views12 pages

Final Linux

The document contains code snippets and outputs from various Bash and C programming scripts and programs. Program 1a takes a file name and line numbers as input and outputs selected lines. Program 1b removes words from files. Program 2a lists files with read, write and execute permissions. Program 2b checks if inputs are files or directories. Later programs count characters, copy files, read and write files using file descriptors.

Uploaded by

MAYURI JP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

Final Linux

The document contains code snippets and outputs from various Bash and C programming scripts and programs. Program 1a takes a file name and line numbers as input and outputs selected lines. Program 1b removes words from files. Program 2a lists files with read, write and execute permissions. Program 2b checks if inputs are files or directories. Later programs count characters, copy files, read and write files using file descriptors.

Uploaded by

MAYURI JP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Program 1a: $ vi pro1b.

sh
$ cat>>pranesh.txt For x in $*
123 do
456 echo "enter the word to be deleted"
789 read fword
135 sed -e '/'Sfword' /d' $x>output.txt
246 done
798 cat output.txt
~
$ vi prog1a.sh ~
echo “enter the file name” :wq
read fname
echo "enter the starting line number" Output:
read s $ sh pro1b.sh hartsh.txt
echo “enter the ending line number” enter the word to be deleted
read n 789
sed -n $s,Sn\p $fname | cat>newline 123
cat newline 456
~ 135
~ 246
:wq 978

Output: Program 2a:


$ sh prog1a.sh $mkdir gokul
enter the file name $ cd gokul
pranesh.txt /gokul$ cat>>arjun.txt
enter the starting line number messi
2 ronaldo
enter the ending line number neymar
4 Paul
456 Pele
789
135 /gokul$ cat>>shiva.txt
kohli
Program 1b: pranesh
$ cat>>harish.txt dhoni
123 sachin
456 rohit Rahul
789 /gokul$ cat>>mathan.txt
135 Saran
246 Gokul
978 kavin
suhas
Rohith
/gokul$ vi pro2a.sh -rw-rw-r-- 1 pranesh pranesh 74 Oct 26
/gokul$ ls -l 21:40 pro8.sh
total 24 -rw-rw-r-- 1 pranesh pranesh 213 Oct 26
-rw-rw-r-- 1 pranesh pranesh 32 Oct 26 21:46 pro9.sh
21:33 arjun.txt -rw-rw-r-- 1 pranesh pranesh 31 Oct 26
-rw-rw-r-- 1 pranesh pranesh 31 Oct 26 21:34 shiva.txt
21:37 mathan.txt
-rw-rw-r—1 pranesh pranesh 11 oct 26 Program 2b:
21:39 pro7. Sh $ vi pro2b.sh
-rw-rw-r-1 pranesh pranesh 74 oct 26 for x in S*
21:40 prog8. Sh do
-rw-rw-r-- 1 pranesh pranesh 213 Oct 26 if [ -f $x ]
21:46 pro9.sh then
-rw-rw-r-- 1 pranesh pranesh 31 Oct 26 echo “$x is a file”
21:34 shtva.txt echo “no of lines in the file are”
wc -l $x
echo "enter the list of file name in curent elif [ -d $x ]
directory" then
echo "which have read, write and execute echo ”$x is a directory”.
permission" else
for file in * echo "enter valid filename or directory
do name"
if [ -f $file] fi
then done
if [ -r $file -a - w $file -a -x $file ] ~
then ~
ls -l $file :wq
fi
fi Output:
done $ sh pro2b.sh harish.txt
~ harish.txt is a file
~ no of lines in the file are
:wq 6 harish.txt

Output: Program 3a:


/gokul$ chmod +x arjun.txt $ cat bca.txt
/gokul$ ls -l Elon musk
total 24 Satya Nadella
-rwxrwxr-x 1 pranesh pranesh 32 Oct 26 Mark Zuckerberg
Jamie Dimon
21:33 arjun.txt
Abdul kalam
-rw-rw-r-- 1 pranesh pranesh 31 Oct 26
bcdfghjkl
21:37 mathan.txt
-rw-rw-r-- 1 pranesh pranesh 11 Oct 26
21:39 pro7.sh
$ vi 3a_bash.sh Program 4a:
file1=$1 $ cat bca.txt
shift 1 Elon Musk
exec<$file1 while read li Satya Nadella
do Mark Zuckerberg
echo "$li:" Jante Dimon
grep -c "$li" s@
Abdul kalam
done
bcdfghjkl
~
~
:wq $ vi 4a_bash.sh
Output: for name in $1
$ sh 3a bash.sh bca.txt do
Elon musk: sed -e 's/\(.\)/\n/g' grep -v -e "a" -e "e" -e
0 "L" -e "o" -e "u" $name | wc -l
Program 3b: done
$ cat bca.txt ~
Elon Musk ~
Satya Nadella :wq
Mark Zuckerberg
Jamie Dimon Output:
Abdul kalam $ sh 4a bash.sh bca.txt
Bcdfghjkl 1

$ vi 3b_bash.sh Program 4b:


echo "the directorys are:" $ cat bca.txt
for file in * Elon Musk
do Satya Nadella
if [ -d $file] Mark Zuckerberg
then Janie Dimon
echo "Sfile" Abdul kalam
fi bcdfghjkl
done
~ $ vi 4b_bash.sh
~ BEGIN(print "record. \t characters \t
:wq words"}
Output: #BODY section
$ mkdir IBCA IIBCA IIIBCA IMCA IIMCA {
$ sh 3b_bash.sh len=length(50)
the directorys are: total_len len
IBCA print(NR, "\t",len, "\t", NF,$0)
IIBCA words =NF
IIIBCA }
IMCA END{
IIMCA print("In total")
print("characters :\t" total len) printf("%s contains: \n",src);
print("lines :\t" NR) while((ch=getc(fp1))!=EOF)
} {
~ fputc(ch, fp2);
~ printf("%c",ch);
:wq }
printf("file is copied.");
Output: fclose(fp1);
vi 4b_bash.awk fclose(fp2);
awk -f 4b_bash.awk bca.txt return 1;
record. characters words }
1: 9: 2 Elon Musk ~
2: 14: 2 Satya Nadella ~
3: 15: 2 Mark Zuckerberg :wq
4: 11: 2 Jante Dinon
5: 11: 2 Abdul kalan Output:
6: 9: 1 bcdfghjkl $ gcc -o 5a_bash.out 5a_bash.c
$ ./5 bash.out
total enter the source file nane:bca.txt
characters: 9 enter the destination file name:nit.txt
lines: 6 bca.txt contains: Elon musk
Satya Nadella
Program 5a: Mark Zuckerberg
$ vi 5a_bash.c Jante Dimon
#include<stdio.h> Abdul kalam
#include<stdlib.h> bcdfghjkl
int main() file is copied.
{
char src[10],dest[10],ch; Program 5b:
FILE *fp1, *fp2; $ cat bca.txt
printf("enter the source file name:"); Elon musk
scanf("%s",src); Satya Nadella
if((fp1=fopen(src, "r"))==0) Mark Zuckerberg
{ Jante Dimon
printf("error in opening src file"); Abdul kalam
exit(0); bcdfghjkl
}
printf("enter the destination file name:"); $ vi 5b_bash.sh
scanf("%s", dest); #include<stdio.h>
if((fp2=fopen(dest, "w"))==0) #include<fcntl.h>
{ #include<unistd.h>
printf("error in destination file"); int main(int argc, char **argv)
exit(1); {
} char ch[2];
int fd; Output:
fd=open(argv[1],0_RDONLY); $ gcc -o 6a bash.out 6a bash.c
while((read(fd,ch,1))==1) $ ./6a_bash.out
{ 1a_bash.sh
write(1,ch,1); IBCA
} 111BCA
close(fd); 6a bash.out
return 1; 9b_bash.out
} 5a bash.c
~ IIMCA
~ nit.sh
:wq 12 bash.out
Output: 2a_bash.sh
$ gcc 5b_bash.c -o 5b_bash
$ ./5b_bash bca.txt Program 6b :
Elon Musk $ vi 6b_bash.c
Satya Nadella #include <stdio.h>
Mark Zuckerberg #include <unistd.h>
Jante Dinon #include <sys/types.h>
Abdul kalam #include <sys/wait.h>
bcdfghjkl #include <stdlib.h>
int main(){
Program 6a: int pid;
$ vi 6a_bash.c pid = fork();
#include <stdio.h> if ( pid < 0 )
#include <dirent.h> {
int main(void) Printf(“\nFork failed\n”);
{ exit (-1);
struct dirent *de; }
DIR *dr = opendir("."); else if ( pid == 0)
if (dr == NULL) {
{ execlp ( “/bins/ls”,”ls “,”-l”, NULL );
printf("Could not open current directory" }
); else
return 0; {
} wait (NULL);
while ((de = readdir(dr)) != NULL) printf(“\nchild completed\n”);
printf("%s\n", de->d_name); exit (0);
closedir(dr); }
return 0; }
} ~
~ ~
~ :wq
:wq
Output: ~
$ gcc -o 6b_bash.out 6b_bash.c ~
$ ./6b_bash.0ut :wq
total 368
-rwxr----- 1 nitheesh nitheesh 930 oct Output:
25 17:25 18a1_bash.c $ cd IBCA
-rwxr----- 1 nitheesh nitheesh 438 oct $ cat>>sample.txt
25 17:25 10a2_bash.c Nitheesh
-rwxr----- 1 nitheesh nitheesh 1345 oct Nagarjuna
25 17:25 10b_bash.c Pranesh
-rwxr----- 1 nitheesh nitheesh 985 oct Pooja
25 17:25 12_bash.c Naren
-rwxr----- 1 nitheesh nitheesh 481 oct Velan
25 17:25 12_bash.out
-rwxrwxr-x 1 nitheesh nitheesh 1650 oct $ cd
28 10:15 1a_bash.sh $ cd Documents:
-rwxr----- 1 nitheesh nitheesh 525 oct $ cd ubuntu
28 15:27 5a_bash.c $ gcc -o 7a bash.out 7a_bash.c
-rwxrrwxr-x 1 nitheesh nitheesh 1645 oct $ ./7a_bash.out IBCA
27 15:17 5a_bash.out 918508 sample.txt
-rwxrrwxr-x 1 nitheesh nitheesh 1628 oct
28 15;35 5b_bash
-rwxr----- 1 nitheesh nitheesh 263 oct Program 7b:
28 15:33 5b_bash.c $ vi 7b_bash.c
#include<stdlib.h>
Program 7a: #include<stdio.h>
$ vi 7a_bash.c #include<string.h>
#include<stdlib.h> int main(int argc, char *argv[])
#include<stdio.h> {
#include<string.h> char d[50];
int main(int argc, char *argv[]) if(argc ==2)
{ {
char d[50]; bzero(d,sizeof(d));
if(argc==2) strcat(d, "ls ");
{ strcat(d, “>”);
bzero(d,sizeof(d)); strcat(d, argv[1]);
strcat(d,argv[1]); system(d);
strcat(d, "ls "); }
strcat(d,"-i "); else
system(d); printf("\nInvalid No. of inputs");
} }
else ~
printf("\nInvalid No. of inputs:”); ~
} :wq
Output:
$ gcc -o 7b_bash.out 7b_bash.c
$ ./7b_bash.out prg.txt
$ cat prg.txt
7b_bash.c
7b_bash.out
prd.txt
prg.txt
Program 8a: child_pid = fork () ;
$ vi 8a_bash.c if (child_pid > 0)
#include<stdio.h> {
#include<sys/wait.h> Sleep (10) ;
int main(void) }
{ else
int pid; {
int status; Exit (0) ;
printf("Hello World!\n"); }
pid=fork(); return 0;
if(pid == -1) }
{ ~
perror("bad fork"); ~
exit(1); :wq
}
if(pid == 0) Output:
printf("I am the child process.\n"); $ gcc -o 8b_bash.out 8b_bash.c
else $ ./8b_bash.out
{ $ ps aux |grep “defunct “
wait(&status); nitheesh 4987 0.0 0.0 17636 912 pts/0 $+
printf("I am the parent process.\n"); 11:47 0.00 grep --color = auto defunct
}
} Program 9a:
~ $ vi 9a_bash.c
~ #include<stdio.h>
wq int main()
{
Output: Int pid;
$./8a.out Printf(“ I’am the original process with PID
Hello World! %d and PPID %d.\n”,getpid(),getppid());
I am the child process. Pid=fork();
I am the parent process. If(pid!=0)
{
Program 8b: Printf(“ I’am the parent with PID %d and
$ vi 8b_bash.c PPID %d.\n”, getpid(),getppod());
#include <stdlib.h> Printf(“My child’s PID is %d\n”,pid);
#include <sys/types.h> }
#include <unistd.h> Else
int main () {
{ Sleep(4);
int pid_t, child_pid;
Printf(“I’m the child with PID %d and PPID {
%d.\n”, getpid(),getppid()); printf("parent reading from pipe \n");
} while(read(pfds[0],buf,80))
Printf(“PID %d termination.\n”,getpid()); printf("%s\n",buf);
} }
~ exit(1);
~ }
:wq ~
~
Output: :wq
$.\a.out
I’am the original process with PID 5271 and Output:
PPID 2462 $ ./a.out
I’am the parent with PID 5271 and PPID parent reading from pipe
2462 total 60
My child’s process PID 5272 -rw-rw-r-- 1 nitheesh nitheesh 0 Oct 28
PID 5271 terminates. 16:41 4b_bash.awk
$ I’m the child with PID 5272 and PPID 1471 -rw-rw-r-i^a
And PID 5272 terminates. - 1 nitheesh nitheesh 278 Nov 9 12:16
8a.c
Program 9b: -rwxrwxr-x 1 nitheesh nitheesh 163i^a
$ vi 9b_bash.c 12 Nov 9 12:17 8a.out
#include <stdio.h> -rw-rw-r-- 1 nitheesh nitheesh 0 Oct 31
#include <unistd.h> 19:08 9a_prg.i^a
#include <sys/types.h> C
#include <stdlib.h> -rwxrwxr-x 1 nitheesh nitheesh 16472 Nov
int main() 9 12:25 a.out drwxr-xr-x 2 root i^a
{ root 4096 Aug 30 20:02 IIBCA
int pfds[2]; -rw-rw-r-- 1 nitheesh nitheesh 342 Nov 9
char buf[30]; 12i^a
if(pipe(pfds)==-1) :24 lin9b.c
{ drwxrwxr-x 2 nitheesh nitheesh 4096 Nov 9
perror("pipe failed"); 12:23 nitheesh
exit(1); drwxrwxr-i^a
if( ! fork()) x 3 nitheesh nitheesh 4096 Sep 25 21:25
{ psgcas
close(1); -rw-r--r-- 1 root root i^a
dup(pfds[1]); 42 Aug 31 10:15 sample.text
system ("ls -l"); p 25 21:25 psgcas
} -rw-r--r-- 1 root root i^a
else
Program 10a: void server (int rfd, int wfd)
$ vi 10a_bash.c {
#include<stdio.h> char fname [2000];
#include<stdlib.h> char buff[2000];
#include<sys/types.h> n=read(rfd, fname, 2000);
#include<sys/stat.h> fname[n]=’\0’;
#include<string.h> int fd=open(fname, O_RDONLY);
#include<fcntl.h> sleep(10);
void server (int, int); if(fd<0)
void client(int, int); write(wfd, “can’t open”,9);
int main() else
{ n=read(fd, buff, 2000);
int p1[2],p2[2],pid; write(wfd, buff,n);
pipe(p1); }
pipe(p2): ~
pid=fork(); ~
if(pid==0) :wq
{
close(p1[1]); Output:
close(p2[0]); $ cat>>IIBCA.txt
client(p1[1],p2[0]); Linux Programming
wait(); Operating System
return 0; RDBMSS
} Accountancy
void client(int wfd, int rfd) DCN
{ $./a.out
int i,j,n; ENTER THE FILE NAME :IIBCA.txt
char fname [2000]; CLIENT SENDING THE REQUEST…..PLEASE
char buff[2000]; WAIT
printf(“ENTER THE FILE NAME :”); THE RESULTS OF CLIENTS ARE……
scanf(“%s”, fname); Linux Programming
printf(“CLIENT SENDING THE Operating System
REQUEST…..PLEASE WAIT\n”); RDBMSS
sleep(10); Accountancy
write(wfd, fname, 2000); DCN
n=read(rfd, buff, 2000);
buff[n]=’\0’; Program 10b:
printf(“THE RESULTS OF CLIENTS $ vi 10b_bash.c
ARE……\n”); #include <stdio.h>
write(1,buff,n); #include <stdlib.h>
} #include <limits.h>
#include <unistd.h> if(write(fd, buf, strsize) != strsize)
#include <string.h> {
#include <sys/types.h> printf(“Parent write to FIFO failed \n”);
#include <sys/stat.h> exit(1);
#include <sys/wait.h> }
#include <fcntl.h> }
#include <limits.h> }
#include <errno.h> ~
# define BUFSIZE 100 ~
void main(int argc, char *argv[]) :wq
{
mode_t fifo_node = S_IRUSR | S_IWUSR; Output:
int fd, status, child; $ cat>>IIBCA.txt
char buf[BUFSIZE]; Linux Programming
unsigned strsize; Operating System
if (argc !=2) RDBMSS
{ Accountancy
printf (“\n Usage: %s pipename\n”, argv[0]); DCN
exit(1); $ ./a.out IIBCA.txt
} Parent 2921 is about to open FIFO IIBCA.txt
if ((mkfifo(argv[1],fifo_mode) == -1) && Child 2922 is about to open FIFO IIBCA.txt
(errno != EEXIST)) Child is about to read
perror(“Pipe”); Child 2922 received: This was written by
exit(1); parent 2921
}
if (( child fork()) == -1) Program 11:
{ $ vi 11_bash.c
perror(“Fork”); #include <stdio.h>
exit(1); #include <sys/tpc.h>
} #include <sys/msg.h>
else if (child == 0) #include <stdlib.h>
printf(“\nChild %ld is about to open FIFO struct mesg_buffer
%s\n”, (long)getpid(), argv[1]); {
if ((fd = open(argv[1], O_WRONGLY)) = -1) long mesg_type:
{ char mesg_text[100];
perror(“Child cannot open FIFO”); }
exit(1); Message1,message2, message3;
} int main()
sprintf(buf, “This was written by parent (
%ld\n”, (long)getpid()); key_t key1,key2,key3;
strsize = strlen(buf) + 1; int msgid1,msgid2,msgid3;
key1= ftok("progfile1", 65); Program 12:
key2= ftok("progfile2", 65); $ vi 12_bash.c
key3= ftok("progfile3", 65); #define _POSIX_SOURCE
msgid1= msgget(key1, 0666|IPC_CREAT); #include <unistd.h>
message1.mesg_type = 3; #include <signal.h>
printf("Write Data : "); #include <stdio.h>
scanf("%s",message1.mesg_text); #include <time.h>
msgid2= msgget(key2, 0666 | IPC_CREAT); void catcher (int signum) {
message2.mesg_type = 2; puts(“inside catcher...”);
printf("write Data: "); }
scanf("%s",message2.mesg_text); void timestamp() {
msgid3= msgget(key3, 0666 | IPC_CREAT); time_ t t;
message3.mesg_type= 1; time(&t);
printf("Write Data : "); printf (“the time is %s”, ctime (&t));
scanf("%s",message3.mesg_text); }
msgsnd(msgid1, &message1, int main() {
sizeof(message1), 0); struct sigaction sigact;
msgsnd(msgid2, &message2, sigemptyset (&sigact.sa_mask);
sizeof(message2), 0); sigact.sa_flags = 0;
msgsnd (msgid3, &message3, sigact.sa_handler = catcher;
sizeof(message3), 0); sigaction(SIGALRM, &sigact, NULL);
printf("Data send is: %s\n", alarm(5);
message1.mesg_text); printf(“before pause....”);
printf("Data send is: %s\n", timestamp();
message2.mesg_text); pause();
printf("Data send is: %s\n", printf(“after pause....”);
message3.mesg_text); timestamp();
return 0; }
} ~
~ ~
~ :wq
:wq
Output: Output:
$ gcc -o 11_bash.out 11_bash.c $ gcc 12_bash.c
$./11_bash.out $ ./a.out
write Data : Apple before pause.... the time is Mon Nov 1
Write Data: Banana 00:08:28 2021
write Data : Cat inside catcher....
Data send is : Apple after pause.....the time is Mon Nov 1
Data send is : Banana 00:08:33 2021
Data send is : Cat

You might also like