Advertisement
Guest User

Modify

a guest
Nov 26th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.94 KB | None | 0 0
  1. #!/bin/sh
  2. # Write a script modify with the following syntax:
  3.  
  4. #   modify [-r] [-l|-u] <dir/file names...>
  5. #   modify [-r] <sed pattern> <dir/file names...>
  6. #   modify [-h]
  7.  
  8. #   modify_examples
  9.  
  10. # which will modify file names. The script is dedicated to lowerizing (-l)
  11. # file names, uppercasing (-u) file names or internally calling sed
  12. # command with the given sed pattern which will operate on file names.
  13. # Changes may be done either with recursion (-r) or without it. Write a
  14. # second script, named modify_examples, which will lead the tester of the
  15. # modify script through the typical, uncommon and even incorrect scenarios
  16. # of usage of the modify script.
  17.  
  18. #VARIABLES
  19. RECURSIVE_MODE=0
  20. LOWERCASE=0
  21. UPPERCASE=0
  22. SED_MODE=0
  23. MODE=0  #lowercasing:1 uppercasing:2 sed_pattern:3
  24. SED_PATTERN=""
  25. SCRIPTNAME=`basename $0`
  26. #echo "My name is $SCRIPTNAME\n"
  27.  
  28. printHelp(){
  29.     echo "Usage:\n\t$0 [-r] [-l|-u] <dir/file names...>\n\t$0 [-r] <sed pattern> <dir/file names...>\n\t$0 [-h]\n "
  30.     echo "Where:\n\t-l lowerizes specified filenames\n\t-u uppersizes specified filenames"
  31.     echo "\t-r runs the script with recursion\n\t-h prints this help\n"
  32. }
  33.  
  34. modifySingleFile(){
  35.         CURRENT_NAME=`basename "$1"`
  36.         CURRENT_PATH=`dirname "$1"`
  37.  
  38.         #Don't modify yourself
  39.         if [ "$CURRENT_NAME" = "$SCRIPTNAME" ] ; then
  40.             return
  41.         fi
  42.  
  43.         case $MODE in
  44.             "1") NEW_NAME=`echo "$CURRENT_NAME" |  tr [:upper:] [:lower:]` ;;
  45.             "2") NEW_NAME=`echo "$CURRENT_NAME" |  tr [:lower:] [:upper:]` ;;
  46.             "3") NEW_NAME=`echo "$CURRENT_NAME" |  sed -s $SED_PATTERN` ;;
  47.         esac
  48.  
  49.         #New filename is prepared, check if modifying the name won't override anything
  50.         if [ -f "$CURRENT_PATH/$NEW_NAME" ] ; then
  51.              `echo "File $NEW_NAME already exists, name modifying failed" 1>&2`
  52.             return
  53.         else
  54.             mv "$1" "$CURRENT_PATH/$NEW_NAME"
  55.             CNT=$((CNT+1))
  56.         fi 
  57. }
  58. modifyNames(){
  59.     #At first, check if the file exists
  60.     if [ ! -f "$1" -a ! -d "$1" ] ; then
  61.             `echo "Neither file nor directory --$1-- exists" 1>&2`
  62.             return
  63.     fi
  64.  
  65.     if [ ! -w "$1" -a -f "$1" ]; then
  66.         `echo "File $1 cannot be modified" 1>&2`
  67.         return
  68.     fi
  69.  
  70.     if [ $RECURSIVE_MODE -eq 1 ] ; then
  71.         if [ -d "$1" ] ; then
  72.          # find "$1" -depth -type d | while read -r dir ; do #echo "$dir"
  73.             find "$1"  -depth -type f | while read -r file ; do #echo "$file"
  74.                 # echo "\t\t\t\t\t3"
  75.                 modifySingleFile "$file"
  76.             done
  77.          # done
  78.         else    #$1 is not a directory
  79.             # echo "\t\t\t\t1"
  80.         modifySingleFile "$1"
  81.         fi
  82.     else        #Recursive mode is off
  83.         # echo "\t\t\t\t2"
  84.         modifySingleFile "$1"
  85.     fi
  86. }
  87.  
  88. if [ $# -eq 0 ]; then
  89.     printHelp
  90. fi
  91.  
  92. while [ "$1" != "" ]; do
  93.     case "x$1" in
  94.         "x-r")  RECURSIVE_MODE=1 ;;
  95.         "x-l")  MODE=1 ;;
  96.         "x-u")  MODE=2 ;;
  97.         "x-h")  printHelp
  98.                 exit ;;
  99.         *)
  100.         #current argument is not a parameter, it can be a sed pattern,a directory or a filename
  101.         if [ $MODE -eq 0 ]; then
  102.             MODE=3
  103.             SED_PATTERN="$1"
  104.         else
  105.             modifyNames "$1"
  106.         fi
  107.         ;;
  108.     esac
  109. shift
  110. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement