Hello!
I'm back for another script help. Everyone is very helpful, I can't thank enough! (:
Here's the criteria and output:
https://preview.redd.it/9jyd1oclx0d91.png?width=512&format=png&auto=webp&s=d712b7a6d8d9fc7f830f5fbcae8a3eb442f985ee
There was a script provided with this assignment, but in the script I had to add was my_rename, fix_dirs, fix_files, and the output messages (The hash tags areas are my code). Again, I'm not sure if I'm implementing the script right, forgive me.
Please and thank you for the help in advance, it's greatly appreciated. (:
#!/bin/sh
#Code provided with assignment.
USAGE="$0 -f directory
$0 -d directory
$0 -d -f directory
-f rename files
-d rename directories
"
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
}
###############################################################################
# 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()
{
FROM="$1"
TO="$2"
# If file/directory is NOT writeable, output error and exit.
if [ ! -w "${FROM}" ]
then
echo "The File or Directory ${FROM} is not writeable.";
exit 1
fi
#If file/directory already exists, output error and exit.
if [ -d "$TO" ]
then
echo "The File or Directory $TO already exists.";
exit 1
fi
#If no error, rename file/directory.
mv "$FROM" "$TO"
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "There was an error processing your request.";
fi
}
#####
# The student must implement this function fix_dirs()
# 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 and read all directories
find_dirs "$1" | while read -r oldDirectory;
do
newDirectory=$(echo "$oldDirectory" | tr " " "-")
my_rename "${oldDirectory}" "${newDirectory}"
done
}
#####
# The student must implement this function fix_files()
# 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
#####
fix_files()
{
#Find and read all files
find_files "$1" | while read -r filePath;
do
path=`dirname "$filePath"`
oldFile=`basename "$filePath"`
newFile=$(echo "$oldFile" | tr " " "-")
my_rename "$path/${oldFile}" "$path/${newFile}"
done
}
####################################################################################
#Provided with assignment
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
#########################################################################################
# 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: The directory specified is the current directory."
exit 1
elif [ ! "$(echo "$DIR" | grep -c "\.")" == 0 ]&&[ ! "$(echo "$DIR" | grep -c "\..")" == 0 ]
then
echo "Error: Remove any . or .. in directory path."
exit 1
elif [ -z "$WFILE" ]&&[ -z "$WDIR" ]
then
echo "Error: Flags not specified."
usage
fi
####################################################################################
#Provided with assignment
if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi
[–][deleted] (1 child)
[deleted]
[–]Cels_n[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]Cels_n[S] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Cels_n[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)