Hello. I am having a bit of trouble with my program. We only need to insert a fe
ID: 3702124 • Letter: H
Question
Hello. I am having a bit of trouble with my program. We only need to insert a few functions into a partially completed Bash Shell Scripting Program. It's stuck not accepting my arguments. Either it doesn't think my input is a directory, or it doesn't think my input exists at all. Below are the instructions, and below that is the program with my code inserted.
INSTRUCTIONS:
The student is to modify the script student-example-p3 following the instructions in the script.
For example:
Given the following
$ ls
C D/ dirs.tar fix-spaces* And the directory "C D" contained the following structure ./C D
./C D/E F
./C D/E F/G H
./C D/E F/G H/J
./C D/E F/G H/J
./C D/E F/G H/a b
./C D/E F/a b
./C D/a b
After the script was run,
fix-space -f -d "C D"
the directory structure would be as follows:
C-D
C-D/E-F
C-D/E-F/G-H
C-D/E-F/G-H/a-b
C-D/E-F/G-H/J----K
C-D/E-F/G-H/J----K/a-b
C-D/E-F/a-b
C-D/a-b
K K/a b
usage ()
{print -u2 "$USAGE" exit 1
}
pathname ()
{# function provided for the student
# if you change this to echo, remove the -- print -- "${1%/*}"
}
basename ()
find_dirs ()
{# function provided for the student
find "$1" -depth -type d -name '* *' -print }
{# function provided for the student
find "$1" -depth -type f -name '* *' -print }
my_rename()
{# the student must implement this function to my_rename
errors
fix_dirs ()
{# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the directory from having spaces to # changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
fix_files ()
{# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the file from having spaces to
# changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
usage fi
case $1 in
usage
;; *)
a# message and exit #
then
MY CODE:
#!/bin/bash
###############################################################################
#
# Description: this script will change the name of
# files/directories with spaces in the name to be dashes instead
# of spaces.
#
# The options to the script are as follows:
# -f rename files
# -d rename directories
# Both -d and and -f may be specifed.
# <directory-name> the name of the directory that you will be
# processing.
###############################################################################
# USAGE INFORMATION
USAGE="$0 -f directory
$0 -d directory
$0 -d -f directory
-f rename files
-d rename directories
"
# FUNCTIONS
###############################################################################
usage ()
{
echo "$USAGE"
exit 1
}
pathname ()
{
# function provided for the student
echo "${1%/*}"
}
basename ()
{
# function provided for the student
echo "${1##*/}"
}
find_dirs ()
{
# function provided for the student
find "$1" -depth -type d -name '* *' | cat
}
find_files ()
{
# function provided for the student
find "$1" -depth -type f -name '* *' | cat
}
###############################################################################
###############################################################################
# *** MY CODE ***
# The student must implement this function to my_rename $1 to $2
# The following error checking must happen:
# 1. check if the directory where $1 resided is writeable, if not then report an error
# 2. check if "$2" exists -if it does report and error and don't do the mv command
# 3. check the status of the mv command and report any errors
###############################################################################
my_rename(){
# Take the directories from the positional arguments
FROM="$1"
TO="$2"
# If the directory is NOT writable, output error and exit.
if [ ! -w "${FROM}" ]
then
echo "Directory or File ${FROM} is NOT WRITABLE!";
exit 1
fi
# If the target directory already exists, output error and exit.
if [ -d "$TO" ]
then
echo "Target File or Directory $TO ALREADY EXISTS!";
exit 1
fi
# Otherwise, rename directory.
mv "$FROM" "$TO"
# Set exist status equal to status. If exists, output error.
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "Error moving the file or directory $FROM to $TO";
fi
}
###############################################################################
# *** MY CODE ***
# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the directory from having spaces to # changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
###############################################################################
fix_dirs (){
# Find directories and read all
find_dirs "$1" | while read -r oldDirectory; do
# Set variable to new directory name.
newDirectory=$(echo "$oldDirectory" | tr " " "-")
# Call rename function
my_rename "${oldDirectory}" "${newDirectory}"
done
}
###############################################################################
# *** MY CODE ***
# # The student must implement this function
# # to actually call the my_rename funtion to
# # change the name of the file from having spaces to
# # changing all of the spaces to -'s
# # if the name were "a b", the new name would be a-b
# # if the name were "a b" the new name would be a----b
# : # remove this line when you implement the function
###############################################################################
fix_files (){
# Find file and read all
find_files "$1" | while read -r oldFile; do
# Set variable to new file name
newFile=$(echo "$oldFile" | tr " " "-")
# Call rename function
my_rename "${oldFile}" "${newFile}"
done
}
###############################################################################
###############################################################################
# BEGINNING OF MAIN PROGRAM
##############################################################################
while [ $# -gt 0 ]
do
case $1 in
-d)
WDIR=1
;;
-f)
WFILE=1
;;
-*)
usage
;;
*)
if [ -d "$1" ]
then
DIR="$1"
else
echo "$1 does not exist ..." >&2
exit 1
fi
;;
esac
shift
done
WFILE=
WDIR=
DIR=
# Check if
# If directory not properly specified, output error
if [ "$#" == "0" ]
then
usage
fi
###############################################################################
# *** MY CODE ***
# The student must implement the following:
# - if the directory was not specified, the script should
# print a message and exit
# - if the Directory specified is the current directory, the script
# print a error message and exit
# - if the directory specified is . or .. the script should print
# an error message and exit
# - if both -f and -d are not specified, the script should print
# a message and exit
###############################################################################
if [ ! -d "$DIR" ]
then
echo "Error: Directory not specified!"
exit 1
elif [ "$DIR" == "$PWD" ]
then
echo "Error: Directory specified is the current directory!"
exit 1
# Check if . or .. exist in directory path
elif [ ! "$(echo "$DIR" | grep -c ".")" -eq "0" ] && [ ! "$(echo "$DIR" | grep -c ".")" -eq "0" ]
then
echo "Error: Please remove any . or .. in directory path!"
exit 1
elif [ "$WFILE" -eq 0 ] && [ "$WDIR" -eq 0 ]
then
echo "Error: Flags not specified!"
usage
fi
###############################################################################
###############################################################################
if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi
Explanation / Answer
I have marked the corrections I made with ##
#!/bin/bash
###############################################################################
#
# Description: this script will change the name of
# files/directories with spaces in the name to be dashes instead
# of spaces.
#
# The options to the script are as follows:
# -f rename files
# -d rename directories
# Both -d and and -f may be specifed.
# <directory-name> the name of the directory that you will be
# processing.
###############################################################################
# USAGE INFORMATION
USAGE="$0 -f directory
$0 -d directory
$0 -d -f directory
-f rename files
-d rename directories
"
# FUNCTIONS
###############################################################################
usage ()
{
echo "$USAGE"
exit 1
}
pathname ()
{
# function provided for the student
echo "${1%/*}"
}
basename ()
{
# function provided for the student
echo "${1##*/}"
}
find_dirs ()
{
# function provided for the student
find "$1" -depth -type d -name '* *' | cat
}
find_files ()
{
# function provided for the student
find "$1" -depth -type f -name '* *' | cat
}
###############################################################################
###############################################################################
# *** MY CODE ***
# The student must implement this function to my_rename $1 to $2
# The following error checking must happen:
# 1. check if the directory where $1 resided is writeable, if not then report an error
# 2. check if "$2" exists -if it does report and error and don't do the mv command
# 3. check the status of the mv command and report any errors
###############################################################################
my_rename(){
# Take the directories from the positional arguments
FROM="$1"
TO="$2"
# If the directory is NOT writable, output error and exit.
if [ ! -w "${FROM}" ]
then
echo "Directory or File ${FROM} is NOT WRITABLE!";
exit 1
fi
# If the target directory already exists, output error and exit.
if [ -d "$TO" ]
then
echo "Target File or Directory $TO ALREADY EXISTS!";
exit 1
fi
# Otherwise, rename directory.
mv "$FROM" "$TO"
# Set exist status equal to status. If exists, output error.
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "Error moving the file or directory $FROM to $TO";
fi
}
###############################################################################
# *** MY CODE ***
# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the directory from having spaces to # changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
###############################################################################
fix_dirs (){
# Find directories and read all
find_dirs "$1" | while read -r oldDirectory; do
# Set variable to new directory name.
newDirectory=$(echo "$oldDirectory" | tr " " "-")
# Call rename function
my_rename "${oldDirectory}" "${newDirectory}"
done
}
###############################################################################
# *** MY CODE ***
# # The student must implement this function
# # to actually call the my_rename funtion to
# # change the name of the file from having spaces to
# # changing all of the spaces to -'s
# # if the name were "a b", the new name would be a-b
# # if the name were "a b" the new name would be a----b
# : # remove this line when you implement the function
###############################################################################
fix_files (){
# Find file and read all
find_files "$1" | while read -r filePath; do
##separated dir and file names so that tr is used on filename only
path=`dirname "$filePath"`
oldFile=`basename "$filePath"`
# Set variable to new file name
newFile=$(echo "$oldFile" | tr " " "-")
# Call rename function
my_rename "$path/${oldFile}" "$path/${newFile}"
done
}
###############################################################################
###############################################################################
# BEGINNING OF MAIN PROGRAM
##############################################################################
# Check if
# If directory not properly specified, output error
if [ "$#" == "0" ]
then
usage
fi
while [ $# -gt 0 ]
do
case $1 in
-d)
WDIR=1
;;
-f)
WFILE=1
;;
-*)
usage
;;
*)
if [ -d "$1" ]
then
DIR="$1"
else
echo "$1 does not exist ..." >&2
exit 1
fi
;;
esac
shift
done
##REMOVED
#WFILE=
#WDIR=
#DIR=
###############################################################################
# *** MY CODE ***
# The student must implement the following:
# - if the directory was not specified, the script should
# print a message and exit
# - if the Directory specified is the current directory, the script
# print a error message and exit
# - if the directory specified is . or .. the script should print
# an error message and exit
# - if both -f and -d are not specified, the script should print
# a message and exit
###############################################################################
if [ ! -d "$DIR" ]
then
echo "Error: Directory not specified!"
exit 1
elif [ "$DIR" == "$PWD" ]
then
echo "Error: Directory specified is the current directory!"
exit 1
# Check if . or .. exist in directory path
##Use . instead of . in grep
##Use 0 instead of "0"
elif [ ! "$(echo "$DIR" | grep -c ".")" -eq 0 ] && [ ! "$(echo "$DIR" | grep -c ".")" -eq 0 ]
then
echo "Error: Please remove any . or .. in directory path!"
exit 1
##USE -z for empty string instead -eq 0
elif [ -z "$WFILE" ] && [ -z "$WDIR" ]
then
echo "Error: Flags not specified!"
usage
fi
###############################################################################
###############################################################################
if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.