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 want to do the following on a bash script:

I want the user to enter a file name and the bash script must move the file to another file as follows. If the user enters input.tex, I want to move this file to another file named inputmerged.tex.

I have the following bash script named myscript.sh:

#!/bin/bash
mv $1 $2

The above however needs to be run as ./myscript input.tex inputmerged.tex The second input is redundant, since the new file, which I move to, is of the same type and always has the word merged appended at the end of the first filename. How should I do it to get rid of the second input in the bash script?

Thanks

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Something like:

NEWFILE=`echo ${1} | sed "s/\./merged./"`
mv "${1}" "${NEWFILE}"

This will however fail if there are multiple . characters in the name.

share|improve this answer
    
Thanks Cry Havok. –  user27323 May 25 '13 at 18:34
1  
Or just mv "$1" "${1%.tex}merged.tex". mywiki.wooledge.org/BashFAQ/030 –  geirha May 26 '13 at 6:58
add comment

Your Answer

 
discard

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