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

    Join Date
    Feb 2003
    Rep Power

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

    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.

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

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

    path() {
      case "$1" in
        echo "$1"
        echo "$HOME/$1"
    echo -n 'Source: '
    read src
    src=`path $src`
    if [ ! -e "$src" ]; then
      echo $src does not exist >&2
      exit 1
    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