Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
Can someone please help me with the below?
Create an executable script file called “newname” that will perform the followings:
Rename a file upon the user’s request. If the file exists, prompt the user for confirmation before renaming the file. The screen should prompt the user for
“Name of file you want to rename.” Use the “\c” escape character.
“New name of file” Use the “\c” escape character.
“File Exist” to avoid overriding a file.
“File $oldname changed to $newname”
“Do you want to rename another file?” If the user selects “yes” the system should refresh the screen and if the user selects “no” the system should exit to the prompt sign.I am stuck on how to get input from the user and how to check if it is an existing file. Also, I am unclear with using the "\c" escape character. Please help or direct me somewhere I can get help. Thanks!
PS..
This is what I have so far. I'm using the vi editor. I'm not even sure if I'm on the right track. I read a post on how to set a variable to a command but im still not sure how to use it to achieve the above..
vi nename [ENTER]
#!/bin/sh
echo "Please eneter the name of the file you want to rename"
Thank You

for getting user input, you can use the read command. eg read userinput. After the user enters something, the variable called userinput will contain what the user types in.
to check if a file exists, you can use if/else/test.
If you do a man test or man bash, you can see how file testing is done. eg for file exists, you can use the -e option.
if [ -e "filename" ]
...
and so on...
As for the "\c", the echo command has this option, but i am just guessing. you might want to elaborate which command your "teacher" expects you to use with the \c option.

Ok, I think I'm on the right track, I have written this script but I'm stuck on how to get it to loop back to the beginning if the user decides to rename another file.. ans yes, my teach wants it used on line 2 & 4 below : ) Thanks in advance!
1. #! / bin/sh
2. Echo “Name of the file you want to rename?””\c”
3. Read oldname
4. Echo “New name of file?””\c”
5. Read newname
6. # test newname and file exist
7. If [ ! –f “$newname” ];
8. Then
9. Echo “File exist”
10. (HERE I NEED TO LOOP BACK TO “New name of file” – line 4)
11. else
12. mv $oldname $newname
13. echo “File $oldname changed to $newname”
14. echo “Do you want to rename another file?”
15. if [ $answer=”yes” ];
16. then
17. (HERE I NEED TO LOOP BACK TO “Name of the file you want to rename?””\c” – line 2)
18. else
19. fi
20. exit 0Thank You

use a loop. I have done almost what you needed. its all up to you to learn how its done...and find your own ways of doing things.
[code]
while true
do
printf "name of the file you want to rename: "
read filename
printf "name of new file? "
read newfile
if [ -f "$newfile" ];then
echo "File $newfile already exists"
continue
fi
mv "$filename" "$newfile"
echo "old file $filename renamed to $newfile"
printf "Do you want to continue renaming? "
read choice
if [ "$choice" != "Y" -a "$choice" != "y" ];then
break
fi
done
[/code]

ghostdog,
Thank you very much for your assistance. I will test this this evening and let you know if I can execute.. I owe you 1!

I finally got it. 1 of my buddy found a cool way to make it loop back, he just eneterd the script name : P
Thanks all!
Thank You

then you would have a lot of different processes belonging to your script...because you keep calling it. however, you have solved it, so that's alright now

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |