Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am automating some deployment task with an .sh script but at some point I need to rename a file but it is not working. The file change can be different but the postfix is the same. Let's have a look at this example so you will understand what is that I exactly need:

read folderName
#... at somepoint this scripts creates a file called foldername-1.0-SNAPSHOT.war (I need to rename it)
oldExecutableName ="" + $folderName + "-1.0-SNAPSHOT.war" 
newExecutableName ="" + $folderName + ".war"
[[ -f "${newExecutableName}" ]] &&
mv -v ${oldExecutableName}   ${newExecutableName}

Example:

Given input filename

Desired name for file is: filename.war

The text -1.0-SNAPSHOT should be removed from the name of the file

for some reason this is not working, and I think it is because the parameter I am reading from the terminal. Can someboby give me a hand to rename this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use :

read -p "Enter Filename :- " finalFinalName
remove_extention='-1.0-SNAPSHOT.war'
addExtention='.war'

[[ -f "${finalFinalName}" ]] && # if file exists then rename 
mv -v $finalFinalName   ${finalFinalName%$remove_extention}${addExtention}

If there is white space in file then then double quotes the variable eg. "${var}"

${finalFinalName%.*} this will remove extension

share|improve this answer
    
I made a little mistake in my question. I already have the extension. I need to remove it, that is what I need. –  sfrj Oct 12 '13 at 18:13
    
not an issue.. just small changes required .. ${finalFinalName%.*} –  Rahul Patil Oct 12 '13 at 18:14
    
I think I am close but is not exactly it. Please see the update to my question. I again expressing wrongly. I don't want to say extension, what I want to say is ending(not just after the dot). –  sfrj Oct 12 '13 at 18:23
    
Please check now , I have updated.. –  Rahul Patil Oct 12 '13 at 18:27
    
@sfrj , are you facing any issue ? –  Rahul Patil Oct 12 '13 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.