Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Your Answer

 
discard

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