Bourne Shell Script $ hw2 file1.c cprogs file2 memos The above command would do
ID: 3861099 • Letter: B
Question
Bourne Shell Script
$ hw2 file1.c cprogs file2 memos
The above command would do the following, if both subdirectories exist:
Move file1.c to immediate subdirectory cprogs
Move file2 to immediate subdirectory memos
Implementation: You must implement this script using a while loop that contains shift commands to access the filename and subdirectory arguments (NOTE: $# will change every time you use the shift command).
The script should first verify that an EVEN number of arguments (2 or more) were entered. If not, use a HERE document to display an error message and the correct argument format, and then exit the script with exit code 1.
Then, before moving each file, the script should verify that the both the named file and the named subdirectory exist.
- If the file does not exist, display an error message which includes the name of the file that does not exist.
- If the subdirectory does not exist, display a message stating which subdirectory does not exist and saying which file was not moved.
- If both the file and the subdirectory exist, move the file and display a message confirming that the file was moved and to where.
Shift and loop until all file/subdirectory pairs have been processed
Explanation / Answer
NOTE: I have completed the code for your assignment and shared the code, output screenshot. Please check and let me know if you face any issues. I will revert back within 24 hours.
Code:
#!/bin/bash
showUsage()
{
cat<<END
===========================================================
Usage: $0 <file1> <directory1> <file2> <directory2> ......
Example: $0 file dir1 file dir2....
NOTE: Only even number of arguments expected. Like 2, 4, 6...
===========================================================
END
exit 1
}
# validating if there are atleast 2 arguments
if [ $# -le 1 ]
then
echo "Atleast 2 arguments are expected as input"
showUsage
fi
# Checking if even number of arguments are passed
arg_result=`expr $# % 2`
if [ $arg_result -ne 0 ]
then
echo "Number of arguments you have entered is $#"
echo "Please enter only even number of arguments"
showUsage
fi
# Iterating through each arguments passed in command line
while [ $# -ne 0 ]
do
filename=$1
# checking if the given file is a file and exists
if [ -f "$filename" ]
then
# no-op command
:
else
echo "$filename does not exist or not a regular file"
showUsage
fi
# moving to next argument
shift
subDir=$1
# checking if the given file is a directory and exists
if [ -d "$subDir" ]
then
# no-op command
:
else
echo "$subDir does not exist or not a directory"
showUsage
fi
# moving to next argument
shift
# moving the filename to directory
mv -f $filename $subDir
echo "$filename is moved to directory $subDir..."
done
Execution output screenshot:
https://pasteboard.co/GCYKyTp.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.