#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    4
    Rep Power
    0

    HELP NEEDED in writing a script to rename a file.......


    Hi,
    I need some help in writing a shell script in a Bourne shell to do the following task,

    prompts a user for the source and target files. If the absolute path is not given, then assume that the file is in the home directory. Should also check if have permissions to rename the file. If cannot execute the command for any reason, print the reason on the screen. If it is possible, rename the file.

    I have been struggling to come up with a solution but can't get it working. Any help is really appreciated.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    52
    Rep Power
    12
    The following should do what you want:

    Code:
    #!/bin/sh
    
    path() {
      case "$1" in
      */*)
        echo "$1"
        ;;
      *)
        echo "$HOME/$1"
        ;;
      esac
    }
    
    echo -n 'Source: '
    read src
    src=`path $src`
    
    if [ ! -e "$src" ]; then
      echo $src does not exist >&2
      exit 1
    fi
    
    echo -n 'Destination: '
    read dst
    dst=`path $dst`
    
    mv $src $dst
    However, assuming this is not a homework assignment, why do you want to do this? Why not just use "mv" with explicit pathnames? Generally, programs that take command-line arguments are much preferred in the Unix world to those that are interactive.

IMN logo majestic logo threadwatch logo seochat tools logo