0% found this document useful (0 votes)
4 views24 pages

Unix Shell Programming Execution Report

The document contains execution reports for Unix Shell Programming, detailing various file and directory handling commands, arithmetic and logical calculations, and file attributes checks. Each section includes program code snippets, command outputs, and explanations of the commands used. The reports demonstrate practical applications of shell scripting in a Unix environment.

Uploaded by

yashasp295
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)
4 views24 pages

Unix Shell Programming Execution Report

The document contains execution reports for Unix Shell Programming, detailing various file and directory handling commands, arithmetic and logical calculations, and file attributes checks. Each section includes program code snippets, command outputs, and explanations of the commands used. The reports demonstrate practical applications of shell scripting in a Unix environment.

Uploaded by

yashasp295
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

1 Unix Shell Programming (Execution Report)

Date:05-09-2025
Part-A/B: A
Problem Statement:1. Execution of various file/directory handling commands

Program:
[Link] (Change directory)
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ cd t0
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/t0
$ cd .
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/t0
$ cd ..
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ cd ~
vikaspatel@LAPTOP-441ITK1N MINGW64 ~
$ cd /
vikaspatel@LAPTOP-441ITK1N MINGW64 /
$

[Link] (Make directory)


vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ mkdir f0
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ mkdir f1 f2 f3
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ mkdir -p f0/f1/f2
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ ls
'$[Link]'/ SteamLibrary/ f1/ 'God of War'/ 'System Volume
Information'/ f2/ 'God of War [FitGirl Repack]'/ 'avast! sandbox'/ f3/ Programming/
f0/ t0/
vikaspatel@LAPTOP-441ITK1N MINGW64 /e
$ cd f0
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ cd f1
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ cd f2
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1/f2
$

[Link] (Present working directory)


vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1/f2
$ pwd
/e/f0/f1/f2
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1/f2
$

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 1


2 Unix Shell Programming (Execution Report)

$ ls
f1/ [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls - l total
0
drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:21 f1/
-rw-r--r-- 1 vikaspatel 197121 0 Sep 6 14:29 [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls -lt total 0
-rw-r--r-- 1 vikaspatel 197121 0 Sep 6 14:29 [Link]
drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:21 f1/
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls -la total 4
drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:29 ./
drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:21 ../
drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:21 f1/
-rw-r--r-- 1 vikaspatel 197121 0 Sep 6 14:29 [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls -li total 0
1688849860298111 drwxr-xr-x 1 vikaspatel 197121 0 Sep 6 14:21 f1/
844424930166145 -rw-r--r-- 1 vikaspatel 197121 0 Sep 6 14:29 [Link]

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ echo "Hello World"
Hello World
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ echo "this is a file" > [Link]

[Link] (Concatenate)
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls
f1/ [Link] [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ cat [Link] this is a file
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ cat [Link] [Link] this is a file
second
file new line
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ cat -n [Link]
second file
new line
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls
[Link] f1/ [Link] [Link]

[Link] (move = cut n paste)


vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ ls
[Link] f1/ [Link] [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ mv [Link] /e/f0/f1
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0
$ cd f1
Department of Information Science and Engg. NitteDU, NMIT, Bangalore 2
3 Unix Shell Programming (Execution Report)

vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1


$ ls
f2/ [Link]

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ cp [Link] [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ ls
f2/ [Link] [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ cat [Link] this is a file

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ ls
f2/ [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ rm [Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ ls f2/

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ rmdir f2
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ ls

[Link]
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ date
Sat Sep 6 15:17:58 IST 2025
vikaspatel@LAPTOP-441ITK1N MINGW64 /e/f0/f1
$ date -d "10/26/1947"
Sun Oct 26 00:00:00 IST 1947

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 3


4 Unix Shell Programming (Execution Report)

Date:09-09-2025
Part-A/B: A
Problem Statement: 2. Simple shell script for basic arithmetic and logical calculations

Program:
#!/bin/sh
a=2
b=4
c=3
val=`expr $a + $b`
echo "Sum = $val"

val=`expr $a - $b`
echo "Difference= $val"
val=`expr $a \* $b`
echo "Product = $val"

val=`expr $b / $a`
echo "quotient = $val"

val=`expr $a % $b`
echo "Modulus = $val"
if [ $a == $b ]
then
echo “a is equal to b”
else
echo “a is not equal to b”
fi
if [ $a != $b ]
then
echo “a is equal to b”
else
echo “a is not equal to b”
fi
if [ $a -lt 10 -o $b -gt 20 ]
then
echo “true”
else
echo “false”
fi

if [ $a -lt 10 -a $b -gt 20 ]
then
echo “True”
else
echo “False”
fi

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 4


5 Unix Shell Programming (Execution Report)

Output:

[LabExam@ISELAB1 ~]$chmod a+x pr2


[LabExam@ISELAB1 ~]$sh [Link]
Sum = 8
Difference = -2
Product =8
Quotient =2
Modulus =2
a is not equal to b
a is not equal to b
True
False

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 5


6 Unix Shell Programming (Execution Report)

Date:09-09-2025
Part-A/B: A
Problem Statement:3. Shell scripts to check various attributes of files and directories.

Program:
#!/bin/sh
file="[Link]"
if [ -r $file ]
then
echo "File has read permission"
else
echo "File does not have read permission"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else echo "This is a special file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "File is not a directory"

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 6


7 Unix Shell Programming (Execution Report)

fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is greater than zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
if [ -b $file ]
then
echo "File is a block file "
else
echo "File is not a block file"
fi
if [ -c $file ]
then
echo "File is a character file "
else
echo "File is not a character file"
fi

Output:

[LabExam@ISELAB1 ~]$ sh [Link]


[LabExam@ISELAB1 ~]$chmod a+x [Link]
[LabExam@ISELAB1 ~]$sh [Link]
File has read permission
File has write permission
File has execute permission
File is an ordinary file
File size is greater than zero

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 7


8 Unix Shell Programming (Execution Report)

File exists
File is not a character file is not a block file

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 8


9 Unix Shell Programming (Execution Report)

Date:16-09-2025
Part-A/B: A
Problem Statement: 4. Shell script to check and list various attributes of files.

Program:
!/bin/sh
ps
echo "ps : process status"
ps -f
echo "ps -f : full listing"
ps -l
echo "ps -l : along listing showing memory related information"
ps -u
echo "ps -u : process of user only"
ps -e
echo "ps -e : all process including user and system process"
ps -a
echo "ps -a : process of all user including processes not listed with terminal"
ps -t
echo "ps -t : processes running on terminal -l along listing showing memory related information"

Output:

[LabExam@ISELAB1 ~]$ sh [Link]

[LabExam@ISELAB1 ~]$chmod a+x [Link]


[LabExam@ISELAB1 ~]$sh [Link]

PID TTY TIME CMD


2053 pts/0 00:00:00 bash
2422 pts/0 00:00:00 [Link]
2423 pts/0 00:00:00 ps
ps = process status

UID PID PPID C STIME TTY TIME CMD


admin 2053 2043 0 10:46 pts/0 00:00:00 bash
admin 2422 2053 0 11:03 pts/0 00:00:00 /bin/sh ./[Link]
admin 2424 2422 0 11:03 pts/0 00:00:00 ps -f
ps -f=full listing

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD


0 S 1000 2053 2043 0 80 0 - 2306 wait pts/0 00:00:00 bash
0 S 1000 2422 2053 0 80 0 - 2045 wait pts/0 00:00:00 [Link]
0 R 1000 2425 2422 0 80 0 - 2868 - pts/0 00:00:00 ps
ps -l=along listing showing memory related information

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 9


10 Unix Shell Programming (Execution Report)

admin 1403 0.0 0.2 54036 9432 tty2 Ssl+ 10:46 0:00 /usr/libexec/gd
admin 1408 0.0 0.3 84184 12864 tty2 Sl+ 10:46 0:00 /usr/libexec/gn
admin 1510 3.3 3.8 1117800 135452 tty2 Sl+ 10:46 0:34 /usr/bin/gnome-
admin 1538 0.0 1.2 155672 44816 tty2 Sl+ 10:46 0:00 /usr/bin/Xwayla
admin 1571 0.3 0.2 52168 7748 tty2 Sl 10:46 0:03 ibus-daemon --x
ps -u=process of user only

PID TTY TIME CMD


1? 00:00:01 systemd
2? 00:00:00 kthreadd
3? 00:00:00 rcu_gp
4? 00:00:00 rcu_par_gp
6? 00:00:00 kworker/0:0H-kb
ps -e=all process including user and system process

PID TTY TIME CMD


868 tty1 00:00:00 gnome-session-b
907 tty1 00:00:03 gnome-shell
1136 tty1 00:00:00 Xwayland
1163 tty1 00:00:00 ibus-daemon
1166 tty1 00:00:00 ibus-dconf
ps -a=process of all user including processes not listed with terminal

PID TTY STAT TIME COMMAND


2053 pts/0 Ss 0:00 bash
2422 pts/0 S+ 0:00 /bin/sh ./[Link]
2429 pts/0 R+ 0:00 ps -t
ps -t=processes running on terminal -l along listing showing memoryb related information

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 10


11 Unix Shell Programming (Execution Report)

Date:16-09-2025
Part-A/B: A
Problem Statement: 5. Write awk script that uses all of its features.

Program:
#!/bin/awk -f
BEGIN {print “START”}
{print $1, “\t”, $3}
END {print “DONE”}

Output:

[LabExam@ISELAB1 ~]$ awk -f [Link] [Link]


And am
You can
Me you
LabExam@ISELAB1 ~]$ cat [Link]
And is am I was
You they can take
Me as you print

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 11


12 Unix Shell Programming (Execution Report)

Date:16-09-2025
Part-A/B: A
Problem Statement: 6. Shell script to display list of users currently logged in.

Program:
#!/bin/sh
echo “User logged in:”
users
echo “Current logged in date and time :”
date
echo “Currently logged in users:”
who
echo “Currently logged in username:”
whoami

Output:
[LabExam@ISELAB1 ~]$ sh [Link]

[LabExam@ISELAB1 ~]$chmod a+x [Link]


[LabExam@ISELAB1 ~]$sh [Link]
User logged in:
UNIX LAB UNIX LAB
Current logged in date and time:
Sat sep 29 12:25:30 IST 2018-11-22
Currently logged in users:
UNIXLAB TTY1 2018-09-29 11.08 (:0)
UNIXLAB pts/0 2018-09-29 12.03 (:0.0)
UNIXLAB pts/1 2018-09-29 12.28 (:0.0)
Currently logged in username:
UNIXLAB

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 12


13 Unix Shell Programming (Execution Report)

Date:14-10-2025
Part-A/B: B
Problem Statement: 1. Write a C/C++ program to implement the CAT command using general file API.

Program:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}

Output:

[UNIXLAB@localhost ~]$ gcc -0 [Link] prgm1.c


UNIXLAB@localhost ~]$cat >[Link]
hello
UNIXLAB@localhost ~]$./[Link] [Link]
hello

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 13


14 Unix Shell Programming (Execution Report)

Date:14-10-2025
Part-A/B: B
Problem Statement: 2. Write a C/C++ program to implement the Cp command using general file API's.

Program:
#include <stdio.h>
#include <stdlib.h
#include <fcntl.h>
#include <errno.h>
#define BUFF_SIZE 1024
int main(int argc, char* argv[])
{
int srcFD, destFD, nbread, nbwrite;
char *buff[BUFF_SIZE];
if(argc != 3)
{
printf("\nUsage: cpcmd source_file destination_file\n");
exit(EXIT_FAILURE);
}
srcFD = open(argv[1],O_RDONLY);
if(srcFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[1],errno);
exit(EXIT_FAILURE);
}

destFD = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |


S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(destFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[2],errno);
exit(EXIT_FAILURE);
}
while((nbread = read(srcFD,buff,BUFF_SIZE)) > 0)
{
if(write(destFD,buff,nbread) != nbread)
printf("\nError in writing data to %s\n",argv[2]);
}
if(nbread == -1)
printf("\nError in reading data from %s\n",argv[1]);

if(close(srcFD) == -1)
printf("\nError in closing file %s\n",argv[1]);

if(close(destFD) == -1)
printf("\nError in closing file %s\n",argv[2]);

exit(EXIT_SUCCESS);
}

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 14


15 Unix Shell Programming (Execution Report)

Output: gcc prgm2.c


[UNIXLAB@localhost ~]$ ./[Link] [Link] [Link]
UNIXLAB@localhost ~]$cat [Link]
hello
UNIXLAB@localhost ~]$cat [Link]
hello

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 15


16 Unix Shell Programming (Execution Report)

Date:28-10-2025
Part-A/B: B
Problem Statement: 3. Write a C/C++ program to implement the ln/rename ( ) using general file API's.

Program:
#include <stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
if(argc!=3)
{
printf(“usage: %s <src_file><dest_file>\n”,argv[0]);
Return 0;
}
if(link(argv[1],argv[2])==-1)
{
printf(“link error\n”);
return 1;
}
printf(“files linked\n”);
printf(“Inode number of linked files\n”);
char str[100];
sprint(str,”ls –i %s %s \n”,argv[1],argv[2]);
system(str);
return 0;
}

Output: gcc prgm1.c -o prgm1


[UNIXLAB@localhost ~]$ ./prgm1 [Link] [Link]
Files linked
Inode number of linked files
416529 [Link] 416529 [Link]

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 16


17 Unix Shell Programming (Execution Report)

Date:28-10-2025
Part-A/B: B
Problem Statement: 4. Write a C/C++ program to duplicate the file descriptor of a file Foo to standard
input file descriptor.

Program:
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
int main(int argc,char **argv)
{
int fd,nfd;
if(argc<2){
printf("usage:%s pathname\n",argv);
exit(1);
}
if((fd=open(argv[1],O_WRONLY))<0)
{
perror("Problem in opening the file");
exit(1);
}
if((nfd=fcntl(fd,F_DUPFD,0))==-1)
{
perror("Problem in duplicating fd");
exit(1);
}
printf("Fd %d duplicated with %d\n",fd,nfd);
close(fd);
close(nfd);
}

Output: gcc prgm.c

[UNIXLAB@localhost ~]$ ./[Link] [Link]


Fd 3 duplicated with 4
gcc prgm.c
[UNIXLAB@localhost ~]$ ./[Link] [Link]
Fd 3 duplicated with 4

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 17


18 Unix Shell Programming (Execution Report)

Date:04-11-2025
Part-A/B: B
Problem Statement: 5. Write a C/C++ program to create a file called file 1 in blocking read-write and
show how you can use fcntl api to modify its access control flags to non-blocking read-write mode.

Program:
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
int accmode,val;
if(argc!=2)
{
fprintf(stderr,"usage:%s <description>",argv[0]);
exit(1);
}
val=fcntl(atoi(argv[1]),F_GETFL,0); /* F_GETFL (void)
Return (as the function result) the file access mode and the
file status flags;*/
if(val<0)
{
perror("fcntl error for fd");
exit(1);
}
accmode=val & O_ACCMODE;

if(accmode==O_RDONLY)
printf("read only");
else if(accmode==O_WRONLY)
printf("Write only");
else if(accmode==O_RDWR)
printf("read write");
else
{
fprintf(stderr,"unknown access mode");
exit(1);
}
if(val & O_APPEND)
printf(",append");
if(val & O_NONBLOCK)
printf(",nonblocking");
if(val & O_SYNC)
printf(",synchronous write");
putchar('\n');
exit(0);

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 18


19 Unix Shell Programming (Execution Report)

Output:
gcc p22.c
[UNIXLAB@localhost ~]$ ./[Link] p22.c
Read-Write

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 19


20 Unix Shell Programming (Execution Report)

Date:28-10-2025
Part-A/B: B
Problem Statement: 6. Write C/C++ program to read and display the last 10 character’s of the input
file.

Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
int num;
long length;
printf("Enter the value of num:");
scanf("%d",&num);
fp=fopen("[Link]","r");
if(fp==NULL)
{
puts("Cannot open this file");
exit(1);
}
fseek(fp,-1,SEEK_END);
length=ftell(fp);
fseek(fp,(length-num),SEEK_SET);
do
{
ch=fgetc(fp);
putchar(ch);
}while(ch!=EOF);
fclose(fp);
return(0);
}

Output:
cat > [Link]
hello world hai beautiful girl

gcc p55.c
[UNIXLAB@localhost ~]$ ./[Link] p55.c
Enter the value of num:4
Girl

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 20


21 Unix Shell Programming (Execution Report)

Date:04-11-2025
Part-A/B: B
Problem Statement: 7. Write a C/C++ program to query and display the different attributes associated
with a file.

Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include<stdlib.h>

int main(int argc, char *argv[])


{
struct stat sb;
if(argc!=2)
{
fprintf(stderr,"usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if(stat(argv[1],&sb)==-1)
{
perror("stat");
exit(EXIT_FAILURE);
}

printf("file type: ");


switch(sb.st_mode & S_IFMT)
{
case S_IFBLK: printf("block device file\n");
break;
case S_IFCHR: printf("character device file\n");
break;
case S_IFDIR: printf("directory\n");
break;
case S_IFIFO: printf("FIFO/pipe\n");
break;
case S_IFLNK: printf("symlink\n");
break;
case S_IFREG: printf("regular file\n");
break;
case S_IFSOCK: printf("socket\n");
break;
default: printf("regular file\n");
break;
}
printf("Inode number: %ld\n", (long) sb.st_ino);
printf("Mode: %lo(octal)\n", (unsigned long) sb.st_mode);
printf("Blocks allocated: %lld\n", (long long) sb.st_blocks);
exit(EXIT_SUCCESS);
}

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 21


22 Unix Shell Programming (Execution Report)

Output: gcc prgm.c


[UNIXLAB@localhost ~]$ ./[Link] prgm.c
File type: regular
Inode number: 1067168
Mode: 100664(octal)
Blocks allocated: 8

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 22


23 Unix Shell Programming (Execution Report)

Date:11-11-2025
Part-A/B: B
Problem Statement: [Link] a C/C++ program to demonstrate masking of read/write/execute
permission of a specified input file for user group and others category.
Program:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
int main()
{
mode_t oldMask,newMask;
oldMask=umask((mode_t)0);
printf("\n Old mask = %on",(int)oldMask);
if(oldMask & S_IRGRP){
printf("\nChanging group read permission from Masked to unmasked.n");
oldMask=(oldMask ^ S_IRGRP);/* ^ Operator is binar XOR operator, copies the bit if it is set in one
operand but not in both. Exclusive or is a logical operator that outputs true only when inputs differ*/
}

newMask=(oldMask|S_IWGRP|S_IXGRP);
umask(newMask);
printf("\nNew MAsk = %onn",(int)newMask);
printf("\nThe file mode creation mask now specifies:nn");
printf("\n Group read permission UNMASKEDn");
printf("\n Group write permission MASKEDn");
printf("\n Group execute permission MASKEDn");

oldMask=umask((mode_t)0);
printf("\n Old mask = %on",(int)oldMask);

if(oldMask & S_IRUSR){


printf("\nChanging user read permission from Masked to unmasked.n");
oldMask=(oldMask ^ S_IRUSR);
}
newMask=(oldMask|S_IWUSR|S_IXUSR);
umask(newMask);
printf("\nNew MAsk = %onn",(int)newMask);
printf("\nThe file mode creation mask now specifies:nn");
printf("\n User read permission UNMASKEDn");
printf("\n User write permission MASKEDn");
printf("\n User execute permission MASKEDn");

oldMask=umask((mode_t)0);
printf("\n Old mask = %on",(int)oldMask);

if(oldMask & S_IROTH){


printf("\nChanging Other read permission from Masked to unmasked.n");
oldMask=(oldMask ^ S_IROTH);
}
newMask=(oldMask|S_IWOTH|S_IXOTH);
umask(newMask);
printf("\nNew MAsk = %onn",(int)newMask);
Department of Information Science and Engg. NitteDU, NMIT, Bangalore 23
24 Unix Shell Programming (Execution Report)

printf("\nThe file mode creation mask now specifies:nn");


printf("\n Other read permission UNMASKEDn");
printf("\n Other write permission MASKEDn");
printf("\n Other execute permission MASKEDn");
}

Output:

gcc p77.c
[UNIXLAB@localhost ~]$ ./[Link] p77.c

Old mask = 2n
New MAsk = 32nn
The file mode creation mask now specifies:nn
Group read permission UNMASKEDn
Group write permission MASKEDn
Group execute permission MASKEDn
Old mask = 32n
New MAsk = 332nn
The file mode creation mask now specifies:nn
User read permission UNMASKEDn
User write permission MASKEDn
User execute permission MASKEDn
Old mask = 332n
New MAsk = 333nn
The file mode creation mask now specifies:nn
Other read permission UNMASKEDn
Other write permission MASKEDn
Other execute permission sMASKEDnss

Department of Information Science and Engg. NitteDU, NMIT, Bangalore 24

You might also like