only for professional solutions

profileCoolcaliber
3.zip

3/output 2.pdf

10/28/2017 https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt

https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt 1/3

#!/usr/bin/ksh 

USAGE="$0 ‐f directory  $0 ‐d  directory  $0 ‐d ‐f directory 

‐f rename files   ‐d rename directories   " 

usage ()      {      print ‐u2 "$USAGE"      exit 1      } 

pathname ()      {      # function provided for the student      print ‐‐ "${1%/*}"      } 

basename ()      {      # function provided for the student      print ‐‐ "${1##*/}"      } 

find_dirs ()      {      # function provided for the student      find "$1" ‐depth ‐type d ‐name '* *' ‐print      } 

find_files ()      {      # function provided for the student      find "$1" ‐depth ‐type f ‐name '* *' ‐print      } 

my_rename()      {      # 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      : # remove this line when you implement the function       } 

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      : # remove this line when you implement the function  

    } 

10/28/2017 https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt

https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt 2/3

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      : # remove this line when you implement the function  

    } 

WFILE=  WDIR=  DIR= 

if [ "$#" ‐eq 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        print ‐u2 "$1 does not exist ..."        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 [ "$WDIR" ‐a "$WFILE" ]      then      fix_files "$DIR"      fix_dirs "$DIR" 

10/28/2017 https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt

https://uml.umassonline.net/bbcswebdav/pid­916184­dt­content­rid­4615303_1/courses/INFO.3120.061.FA17/student­example­p3%281%29.txt 3/3

elif [ "$WDIR" ]      then      fix_dirs "$DIR"  elif [ "$WFILE" ]      then      fix_files "$DIR"      fi 

3/Programming Assignment 3.docx

·

Programming Assignment 3

· Assignment 3

· -----------------------------------------------------------------------

· The student is to modify the script  student-example-p3  following the instructions in the script.

· The purpose of this script is to change the name of files/directories with spaces in the name to have a dash(-). So if a file/directory were named "A B", the new name would be A-B. The script will work on a directory specified on the command line and replace the spaces in the names for all files/directories underneath that directory.

· The options to the script are as follows:

· -f  the -f says to only rename files

· -d  the -d says to only rename directories

· Both -d and and -f may be specifed.

· <directory-name>  the name of the directory that you will be processing.

· 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    K ./C D/E F/G H/J    K/a b ./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

· Here is a copy of the script - it is alos as an attachment in both UNIX and Windows format.

· #!/usr/bin/ksh USAGE="$0 -f directory $0 -d  directory $0 -d -f directory -f rename files -d rename directories " usage ()     {     print -u2 "$USAGE"     exit 1     } pathname ()     {     # function provided for the student     print -- "${1%/*}"     } basename ()     {     # function provided for the student     print -- "${1##*/}"     } find_dirs ()     {     # function provided for the student     find "$1" -depth -type d -name '* *' -print     } find_files ()     {     # function provided for the student     find "$1" -depth -type f -name '* *' -print     } my_rename()     {      # 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           :  # remove this line after the function is coded     } 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           :  # remove this line after the function is coded     } 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           :  # remove this line after the function is coded     } WFILE= WDIR= DIR= if [ "$#" -eq 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         print -u2 "$1 does not exist ..."         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 [ "$WDIR" -a "$WFILE" ]     then     fix_files "$DIR"     fix_dirs "$DIR" elif [ "$WDIR" ]     then     fix_dirs "$DIR" elif [ "$WFILE" ]     then     fix_files "$DIR"     fi

· https://uml.umassonline.net/images/ci/sets/set01/document_on.gif

Outline and Example Script for Project 3 - rename files

The students are required to implement the areas in bold

Please note that his script was written for the KornShell - if you implement it with bash, you may need to change the print commands to echo commands depending on your bash implementation.

#!/usr/bin/ksh

USAGE="$0 -f directory

$0 -d directory

$0 -d -f directory

-f my_rename files

-d my_rename directories

"

usage ()

{

print -u2 "$USAGE"

exit 1

}

pathname ()

{

# function provided for the student

print -- "${1%/*}"

}

basename ()

{

# function provided for the student

print -- "${1##*/}"

}

find_dirs ()

{

# function provided for the student

find "$1" -depth -type d -name '* *' -print

}

find_files ()

{

# function provided for the student

find "$1" -depth -type f -name '* *' -print

}

my_rename()

{

# 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

: # remove this line when you add your code

}

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

: # remove this line when you add your code

}

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

: # remove this line when you add your code

}

WFILE=

WDIR=

DIR=

if [ "$#" -eq 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

print -u2 "$1 does not exist ..."

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 [ "$WDIR" -a "$WFILE" ]

then

fix_files "$DIR"

fix_dirs "$DIR"

elif [ "$WDIR" ]

then

fix_dirs "$DIR"

elif [ "$WFILE" ]

then

fix_files "$DIR"

fi

image1.gif

3/student-example-p3(1) (1)

#!/usr/bin/ksh USAGE="$0 -f directory $0 -d directory $0 -d -f directory -f rename files -d rename directories " usage () { print -u2 "$USAGE" exit 1 } pathname () { # function provided for the student print -- "${1%/*}" } basename () { # function provided for the student print -- "${1##*/}" } find_dirs () { # function provided for the student find "$1" -depth -type d -name '* *' -print } find_files () { # function provided for the student find "$1" -depth -type f -name '* *' -print } my_rename() { # 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 : # remove this line when you implement the function } 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 : # remove this line when you implement the function } 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 : # remove this line when you implement the function } WFILE= WDIR= DIR= if [ "$#" -eq 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 print -u2 "$1 does not exist ..." 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 [ "$WDIR" -a "$WFILE" ] then fix_files "$DIR" fix_dirs "$DIR" elif [ "$WDIR" ] then fix_dirs "$DIR" elif [ "$WFILE" ] then fix_files "$DIR" fi