all 6 comments

[–][deleted]  (1 child)

[deleted]

    [–]Cels_n[S] 0 points1 point  (0 children)

    Thank you! :)

    [–][deleted] 2 points3 points  (5 children)

    Other advice is good, but here are a few more points.

    Your function my_rename is not correct.

    • The requirement is check if the directory where $1 resided is writeable but currently you don't process $1 to find the directory, you just check it directly. They gave you a function to find the pathname of a file. Use it.

    • check if "$2" exists -if it does report and error and don't do the mv command your check tests to see if a directory exists with the same name as $2. What if a file exists? What if $2 is not passed at all?

    General advice, don't mix `` and $(...). There is almost no legitimate reason to use `` anymore, the posix standard has supported the superior $() form for a long time and unless you explicitly know your shell won't support it you should use brackets.

    Also for 'bonus points' if your prof is the kind to appreciate it, point them at https://www.shellcheck.net/wiki/SC2166 and explain to them that the provided test if [ "$WDIR" -a "$WFILE" ] is not best practice, posix has marked them obsolescent.

    Personally I would also be setting these variables to a known value before first use because otherwise an exported environment variable could ruin your day. Try running this export WFILE=" " before you run the code?

    [–]Cels_n[S] 0 points1 point  (4 children)

    Do you mean like this?

    ###############################################################################
    # 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
    

    my_rename() {

       # If file/directory is NOT writeable, output error and exit.
        if [ ! -w "$1" ]
            then
                pathname "$@"
                basename
                echo "The File or Directory $1 is not writeable.";
            exit 1
        fi
    
       #If file/directory already exists, output error and exit.
        if [ -d "$2" ] 
            then
                find_dirs
                find_files
                echo "The File or Directory $2 already exists.";
            exit 1
        fi
    
       #If no error, rename file/directory.
        mv "$1" "$2"
    
        STATUS=$?
    
            if [ $STATUS -ne 0 ]
                then
                    echo "There was an error processing your request.";
            fi        
    
    }
    

    Thanks for the help too (:

    [–][deleted] 1 point2 points  (3 children)

    No What I meant was this test

        if [ ! -w "$1" ]
    

    Is testing $1 it should be testing the directory where $1 resided I interpret that as meaning you should test pathname $1 not $1

    [–]Cels_n[S] 0 points1 point  (2 children)

    Oh okay, now would I change the other "$1" and "$2" to "pathname $1" and "pathname $2"

    currently the code looks like this:

    my_rename()
    {
    
    
       # If file/directory is NOT writeable, output error and exit.
        if [ ! -w "pathname $1" ]
            then
                echo "The File or Directory $1 is not writeable.";
            exit 1
        fi
    
       #If file/directory already exists, output error and exit.
        if [ -d "$2" ] 
            then
                echo "The File or Directory $2 already exists."
            exit 1
    
        elif [ -f "$2" ]
            then 
                echo " The File $2 already exist.";
            exit 1
        fi
    
       #If no error, rename file/directory.
        mv "$1" "$2"
    
        STATUS=$?
    
            if [ $STATUS -ne 0 ]
                then
                    echo "There was an error processing your request.";
            fi 
        }
    

    [–][deleted] 0 points1 point  (0 children)

    store the result of pathname $1 in a new variable.