Thread: Why doesn't this work?

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266

    Why doesn't this work?

    I'm writing a simple script to change the file extension of a file. I won't lie, it's a homework assignment and I finally figured out how to get sed to behave in the way I expected but when running the program, I get: "Bad: modifier in $( )" if I specify a file that doesn't exist. How can I fix that? What I WANT to happen is that if the second argument is blank or the file doesn't exist, for it to go to the then portion of the if code.

    Code:
    #!/bin/tcsh                                                                       \
                                                                                       
    
    set fileExt="$1"
    set oldName="$2"
    
    if (-r $oldName) then
        set newName = `echo $oldName | sed 's/.$/'$fileExt/''`
        mv $oldName $newName
    else
        echo $oldName: No such file
    
    endif
    Last edited by Syscal; 04-16-2012 at 06:06 PM.

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Hmm, got it to work using bash, any reason why the above doesn't work using tcsh?

    Code:
    #!/bin/bash                                                                        
    
    fileExt="$1"
    oldName="$2"
    
    if test -r $oldName;
        then
            newName=`echo $oldName | sed 's/.$/'$fileExt'/'`
            mv $oldName $newName
        else
            echo $oldName: No such file
    fi

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The tcsh version worked fine for me, though your regular expression doesn't replace the extension, it only replaces the last character. You need to escape the . if you want a literal . and look into grabbing everything after the last . in the file name.

    The error you're getting is probably related to your use of the colon in the echo statement: https://www.google.com/search?q=tcsh...r+in+%24(+)%22.

    EDIT: Bash has a much sexier way then your sed hack. Check out rule #6 here: http://bashcurescancer.com/10-steps-...l-scripts.html. Not sure if tcsh has an analog.

    EDIT 2: You could leverage the word modifiers (the probably source of your error) to strip the old extension. Read the "History substitution" and "Variable substitution" sections of the tcsh man page. Lots of other good stuff in there too.
    Last edited by anduril462; 04-16-2012 at 06:42 PM.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    whoops, I hadn't realized that it only replaced a single character! how can I match everything after a pattern with tcsh? Wildcard "*" doesn't seem to work. Also, for this assignment it is required that we use SED

    Edit: I'll check out the man page as suggested. If I can't figure it out, I'll post back

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Looks like I figured it out:

    Code:
    #!/bin/bash                                                                       \
                                                                                      
    fileExt="$1"
    
    shift
    
    for oldName in $*
        do
            if test -r $oldName;
                then
                    newName=`echo $oldName | sed 's/[^\.]*$/'$fileExt'/'`
                    mv $oldName $newName >& err.out
                else
                    echo $oldName: No such file
            fi
    done
    Thanks for the help! This works as expected. I now need to go back through and check over the rest of it. We submit it through an "auto grader" tool that checks the script vigorously for many cases...

    EDIT: Got it to work for all cases - Seems like that was insane. He said "test it" before submitting, I didn't think he'd cover so many cases!

    Code:
    #!/bin/bash                                                                       \
                                                                                      
    fileExt="$1"
    
    shift
    
    oldName="$1"
    #for oldName in $*                                                                 
    while test "$oldName" != "";
        do
            if test -r "$oldName";
                then
                    newName=`echo $oldName | sed "s/[^\.]*$/$fileExt/"`
                    mv "$oldName" "$newName" >& err.out
                else
                    echo $oldName: No such file
            fi
        shift
        oldName="$1"
    done
    What have I learned here? You have to use double quotes for your shell variables that contain spaces instead of single quotes - or so it seemed here -. Seems like that was the biggie here. Aside from bash's ..........ing about whitespace in variable assignment... Seems really picky and sloppy to me.
    Last edited by Syscal; 04-16-2012 at 08:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. it doesn't work!
    By Francesco Leone in forum Windows Programming
    Replies: 10
    Last Post: 07-26-2011, 09:56 AM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. Anyone see why this doesn't work?
    By tzuch in forum C Programming
    Replies: 4
    Last Post: 03-27-2008, 08:40 AM
  4. Why doesn't it work???
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2006, 08:24 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM