Find the answer to your Linux question:
Results 1 to 4 of 4
HI, as part of my college work, I have to create a UNIX command which can rename in the style of the DOS rename. I have the basic code working, ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    2

    Question UNIX rename script, DOS Style

    HI,
    as part of my college work, I have to create a UNIX command which can rename in the style of the DOS rename. I have the basic code working, which will rename the files. But i cant seem to get the file extension, rename bit sorted. I think it might be my regex but not sure, any help would be good. Here is the code i have so far, please ignore the error checking:
    Code:
    #!/bin/sh
    #
    # - A DOS STYLE RENAME
    #
    case $# in
    0)            echo "Usage: dosRename file [ ... ] target"
                exit
                ;;
    #2)            echo "dosRename: $file: Can't have anything after target"
    #            exit
    #            ;;
    '..')        echo "dosRename: $file: Can't have more than one dot"
                exit
                ;;
    '**')          echo "dosRename: $file: Can't have more than one asterisk"
                exit
                ;;
    #[!-f])      echo "dosRename: $file: Doesn't exist"
    #            exit
    #            ;;
    6)          echo "dosRename: $file: Must have a dot"
                exit
                ;;
    7)            echo "dosRename: No target"
                exit
                ;;
    *)            for file
                do
                    case $file in
                    [A-Za-z0-9]*\.\*)     filePart=name
                                        ;;
                    \*\.[A-Za-z0-9]*)     filePart=etx
                                        ;;
                    esac
                done
                endFile=$file
                fileName=`echo $endFile | sed -e 's/\.\*//'`
                extension=`echo $endFile | sed -e 's/\*\./\./'`
                while [ $# -ne 1  ]
                do
                        case $filePart in
                            'name')     result=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"`
                                        mv $1 $result
                                        ;;
                            'ext')        result=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"`
                                        mv $1 $result
                                        ;;
                        esac
                     shift 1
                done
                 ;;
    esac
    any advice would be great on where i might possibly be going wrong.
    also i am unable to use python as we have not yet covered it in the course,
    thanks guys

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, first off, this is misspelled:
    Code:
    filePart=etx
    Secondly, your code seems extremely complicated for this task, and in any event, it is against the forum rules to help with homework assignments. If you can narrow down your question to something specific, we can help you then.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    2
    well thats got it sorted thanks. but its hardly doing my homework as all of the coding is already done.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Except that the coding isn't already done, as it has errors. Whereas the question "I wrote this script, and this particular line isn't working. It's supposed to do this, but instead it's doing this." is a good way to ask about homework.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...