Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Write a script modify with the following syntax:
- # modify [-r] [-l|-u] <dir/file names...>
- # modify [-r] <sed pattern> <dir/file names...>
- # modify [-h]
- # modify_examples
- # which will modify file names. The script is dedicated to lowerizing (-l)
- # file names, uppercasing (-u) file names or internally calling sed
- # command with the given sed pattern which will operate on file names.
- # Changes may be done either with recursion (-r) or without it. Write a
- # second script, named modify_examples, which will lead the tester of the
- # modify script through the typical, uncommon and even incorrect scenarios
- # of usage of the modify script.
- #VARIABLES
- RECURSIVE_MODE=0
- LOWERCASE=0
- UPPERCASE=0
- SED_MODE=0
- MODE=0 #lowercasing:1 uppercasing:2 sed_pattern:3
- SED_PATTERN=""
- SCRIPTNAME=`basename $0`
- #echo "My name is $SCRIPTNAME\n"
- printHelp(){
- 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 "
- echo "Where:\n\t-l lowerizes specified filenames\n\t-u uppersizes specified filenames"
- echo "\t-r runs the script with recursion\n\t-h prints this help\n"
- }
- modifySingleFile(){
- CURRENT_NAME=`basename "$1"`
- CURRENT_PATH=`dirname "$1"`
- #Don't modify yourself
- if [ "$CURRENT_NAME" = "$SCRIPTNAME" ] ; then
- return
- fi
- case $MODE in
- "1") NEW_NAME=`echo "$CURRENT_NAME" | tr [:upper:] [:lower:]` ;;
- "2") NEW_NAME=`echo "$CURRENT_NAME" | tr [:lower:] [:upper:]` ;;
- "3") NEW_NAME=`echo "$CURRENT_NAME" | sed -s $SED_PATTERN` ;;
- esac
- #New filename is prepared, check if modifying the name won't override anything
- if [ -f "$CURRENT_PATH/$NEW_NAME" ] ; then
- `echo "File $NEW_NAME already exists, name modifying failed" 1>&2`
- return
- else
- mv "$1" "$CURRENT_PATH/$NEW_NAME"
- CNT=$((CNT+1))
- fi
- }
- modifyNames(){
- #At first, check if the file exists
- if [ ! -f "$1" -a ! -d "$1" ] ; then
- `echo "Neither file nor directory --$1-- exists" 1>&2`
- return
- fi
- if [ ! -w "$1" -a -f "$1" ]; then
- `echo "File $1 cannot be modified" 1>&2`
- return
- fi
- if [ $RECURSIVE_MODE -eq 1 ] ; then
- if [ -d "$1" ] ; then
- # find "$1" -depth -type d | while read -r dir ; do #echo "$dir"
- find "$1" -depth -type f | while read -r file ; do #echo "$file"
- # echo "\t\t\t\t\t3"
- modifySingleFile "$file"
- done
- # done
- else #$1 is not a directory
- # echo "\t\t\t\t1"
- modifySingleFile "$1"
- fi
- else #Recursive mode is off
- # echo "\t\t\t\t2"
- modifySingleFile "$1"
- fi
- }
- if [ $# -eq 0 ]; then
- printHelp
- fi
- while [ "$1" != "" ]; do
- case "x$1" in
- "x-r") RECURSIVE_MODE=1 ;;
- "x-l") MODE=1 ;;
- "x-u") MODE=2 ;;
- "x-h") printHelp
- exit ;;
- *)
- #current argument is not a parameter, it can be a sed pattern,a directory or a filename
- if [ $MODE -eq 0 ]; then
- MODE=3
- SED_PATTERN="$1"
- else
- modifyNames "$1"
- fi
- ;;
- esac
- shift
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement