Helpful Information
 
 
Category: UNIX Help
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

The following should do what you want:


#!/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.










privacy (GDPR)