0% found this document useful (0 votes)
174 views

Shell Script

The document describes a shell script that implements a file operations program using case statements and command line arguments to copy, move, delete, and change permissions of files. The valid usage is passing the operation (c, m, d, p) and the filename as arguments. It then provides the shell script code to implement these operations. It also asks to write a C program to perform the same file operations using related system calls instead of shell commands. The response provides a C program that implements the same file operations - copy, move, delete, and change permissions. It uses a switch case on the operation argument to call functions for each operation. These functions use related system calls like open, read, write, rename, unlink

Uploaded by

piyush singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Shell Script

The document describes a shell script that implements a file operations program using case statements and command line arguments to copy, move, delete, and change permissions of files. The valid usage is passing the operation (c, m, d, p) and the filename as arguments. It then provides the shell script code to implement these operations. It also asks to write a C program to perform the same file operations using related system calls instead of shell commands. The response provides a C program that implements the same file operations - copy, move, delete, and change permissions. It uses a switch case on the operation argument to call functions for each operation. These functions use related system calls like open, read, write, rename, unlink

Uploaded by

piyush singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1.

Implement the shell program using case and command line arguments to perform the
following:

file copy
file move
file delete
file permission change
Usage should be below::
$ fileops.sh c fname
would copy fname to fname.cpy
$ fileops.sh m fname
would move fname to old.fname
$ fileops.sh d fname
would delete fname from the directory
$ fileops.sh p fname
would change the permission of the file to readonly for group and others
$fileops.sh any other arguments .. more than 2 or not c or m or d and fname then it should exit

SOLUTION:
#! /bin/bash

if [ $# != 2 ]
then
echo "Invalid choice."
exit

else
opt=$1
fname=$2
case $opt in
c)
cp $fname $fname.cpy
if [ -e $fname ]
then
echo
echo "$fname copied to $fname.cpy"
fi
;;

m)
mv $fname old.$fname
if [ -e old.$fname ]
then
echo "$fname moved to old.$fname"
fi
;;

d)
rm $fname
if [ ! -e $fname ]
then
echo "$fname removed"
fi
;;

p)
chmod go=r $fname
if [ -e $fname ]
then
echo "For file $fname Group & others permission changed to Read only."
fi
;;

*)echo "Invalid choice."


exit;;

esac
fi

Screenshots of Program :
Q2. Also use any related system calls and write C program for performing the same operations
$fileops.exe c fname
file copy, move and delete instead of system command

Solution:

#include <stdio.h>
#include <unistd.h>
#include<fcntl.h>
#include<unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

void copy();
void move();
void delete();
void change_permission();

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


{
if( argc == 3 )
{
char command = argv[1][0];
char *fname = argv[2];

switch (command)
{
case 'c':
copy(fname);
break;

case 'm':
move(fname);
break;

case 'd':
delete(fname);
break;

case 'p':
change_permission(fname);
break;

default :
printf("Invalid Option [c copy] | [m move] | [d delete] | [p
permission]\n");
exit(1);
}
}
else
{
printf("Invalid Arguments\n");
exit(1);
}
return 0;
}

void copy(char *fname)


{
char *extention = ".cpy";
char *newfname = malloc(strlen(fname) + strlen(extention) + 1); // +1 for the
null-terminator

strcpy(newfname, fname);
strcat(newfname, extention);

char buffer[10000]; //Character buffer


ssize_t read_in, write_out; //Number of bytes returned by single read and
write operation

int sourcefile = open(fname, O_RDONLY); //Source file open in read only


mode

if(sourcefile < 0 )
{
printf("Source file not Exist\n"); //Source file not found
}
else
{
// Destination file open in write mode and if not found then create
int destfile = open(newfname, O_WRONLY | O_CREAT);
while((read_in = read(sourcefile, &buffer, 10000)) > 0)
{
write_out = write(destfile, &buffer, read_in);
}
printf("File Successfully copied to %s\n", newfname);
close(destfile);
}
close(sourcefile);
}
void move(char *fname)
{
int result;
char *prefix = "old.";
char *newfname = malloc(strlen(fname) + strlen(prefix) + 1);

strcpy(newfname, prefix);
strcat(newfname, fname);

result = rename(fname, newfname);

if(result == 0)
{
printf("%s Moved to %s\n", fname, newfname);
}
else
{
printf("Can't move, File not Found!...\n");
}
}

void delete(char *fname)


{
int result;

result = unlink(fname);
if(result == 0)
{
printf("%s Deleted Successfuly!\n", fname);
}
else
{
printf("Can't Delete, File not Found!...\n");
}
}

void change_permission(char *fname)


{
int result;

result = chmod(fname, 00644);

if(result == 0)
{
printf("%s, File permission successfuly changed to readly for Group and
Others!\n", fname);
}
else
{
printf("Can't change file permission, File not Found!...\n");
}
}
Screenshots of output:

You might also like