Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions: Your assignment is to write a shell script that swaps the contents

ID: 3745755 • Letter: I

Question

Instructions: Your assignment is to write a shell script that swaps the contents of two files. The shell script
should be called swap.sh.
The initial requirements described in this paragraph will only be stated in this assignment, but will be
required in all of the other shell script assignments this semester. In all of your shell script assignments, be
sure to (1) put the comment at the beginning of the script to indicate to the system that the Bourne shell is
to be used, (2) put comments after that line to identify yourself, the assignment and to describe the general
purpose of the script, (3) put an appropriate comment before each block of commands in the script, (4) exit
with a status of zero when the script was able to correctly accomplish its task, and (5) print an appropriate
error message and exit with a nonzero status when the script was not able to accomplish its task.
The swap.sh script should take two arguments as input that indicate the two files to be swapped. You
should check that the proper number of arguments have been passed and that the filenames passed on the
command line have the proper permissions set. You should also check that any temporary files used during
the execution of the script do not exist. If any of these checks fail, then print an appropriate error message
and exit the script with a status of one. If all of the checks pass, then swap the two specified files and print
a message indicating that the two files have been swapped.
Example Session:
% swap tmp1 tmp2
The files tmp1 and tmp2 have been successfully swapped.

Explanation / Answer

Here is the program as asked. Please replace the "About youself" comment in the 2nd line

swap.sh

#!/usr/bin/env bash
# Describe about yourself here
# Purpose of this script is to swap contents of two files
# Files to be swapped are passed as arguments to the script on the terminal. 1st argument being 1st file and 2nd argument being 2nd file.
# Please note that execution permissions need to given to the script before it can be executed.


# check if the number of parameters passed are exactly 2
if [ "$#" -ne 2 ]; then
echo "Exactly 2 parameters are required."
exit 1
fi

# check if the file 1 exists.
if [ ! -e "$1" ]; then
echo "File $1 doesn't exist."
exit 1
fi

# check if the file 2 exists.
if [ ! -e "$2" ]; then
echo "File $2 doesn't exist."
exit 1
fi

# check if the file 1 has write permissions.
if [ ! -w "$1" ]; then
echo "File $1 doesn't have write permission."
exit 1
fi

# check if the file 2 has write permissions.
if [ ! -w "$1" ]; then
echo "File $2 doesn't have write permission."
exit 1
fi


TMPFILE=tmp.$$ # create a temporary file first.
mv "$1" $TMPFILE # move the first file to the temporary file.
mv "$2" "$1" # move the second file to the first file.
mv $TMPFILE "$2" # move the temporary file to the 2nd file
echo "The files $1 and $2 have been successfully swapped."
exit 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote