Create file and then change the extension case.

Interpreter should be bash.

  1. The problem statement, all variables and given/known data:

I need to make a file (myText.txt or song.mp3 or cloud.tar.gz or whatever) and then change the extension to (myText.TXT , song.MP3, cloud.TAR.GZ).

It would be good if I can add all information in command line.

Example Input: sh scriptName.sh <file name.extension> <file name.NEWEXTENSION> or vice versa . If the file already exists it should say something.

  1. Relevant commands, code, scripts, algorithms:

if [ ! -f $1 ] to check file exists

  1. The attempts at a solution (include all code and scripts):
CreateFile(){
    if [ ! -f $1 ]
    then
        touch $1 > /dev/null 2>&1 && echo "File $1 created"
    else
        echo "Error: $1 file exists!"
    fi

  OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done

Some bitses, but I cant manage to make it all happen together.

  1. School (University) and Course Number:
    TTU, 1

Hi Kdenmen,

What happens if you temporarily put extra echo statements after some of the variable assignments, especially those that contain pattern matching operators? Do you get the desired result? Also where does the function "Createfile" end and where does it get to be called? Why do you need the find command if you just need to rename a single file?

S.

I still cant manage, because I am relatively new and never done any scripts.

Maybe someone will help me a bit.

I found this, but I need to get the results I described in my first post.

Thanks ahead.

#!/bin/bash

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}
 
if [ -z "$3" ]; then
    usage
    exit 1
fi
 
OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done 
 
exit 0

I have to bump my own thread.

Thanks ahead. I made a post to forum, but never got a anwer.

Maybe you know someone who still can help me.

#!/bin/bash

----------Dont know exactly but my code ---
touch > /tmp/newfilename
umask 077 && mkdir /tmp/tempdir.$$)
exit 2
----------------------------------------

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}
 
if [ -z "$3" ]; then
    usage
    exit 1
fi
 
OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done 
 
exit 0

Basically I would like to type to terminal a line like this:

sh myshellscriptname.sh temporaryfolder temporaryfile filecase1 filecase2

like : sh script.sh test file.txt txt TXT -> it would make a folder called test a file calldes file.txt and then change .txt to .TXT or vice versa and when i press CTRL + C - then the temporary file and folder will be deleted.

Greetings,
my 2 cents:
I have not much time to understand all you are trying here and I dont use bash...

So my remarks would be more like "If you were using /usr/bin/sh, ..."

1) The beginning:

#!/usr/bin/sh

#----------Dont know exactly but my code ---  # --- Isnt valid for comments...
touch  ./tmp/newfilename                             # So use <#>
umask 077 && mkdir ./tmp/tempdir.$$             # no ) at the end...
#exit 2                                                     # otherwise your program stops here!
#-----------------------------------------

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}

if [ -z "$1" ]; then                             #  why $3? what about the other
    usage                                       #  expected arguments ?
    exit 1                                       #  at least now it will show up if you type
fi                                             # nothing more than the programs name...

OLDEXT=$2
NEWEXT=$3

echo  OLDEXT=$OLDEXT                         # when things dont work, display what you loaded
echo NEWEXT=$NEWEXT                         # in order to check you did initialize some VARs
                                              # and have an idea where you are in your code
                                              # at execution...

your main:

 NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"

Since I dont use bash for me it will be difficult to say what is not working...
In sh though I doubt it can be done that way...

Why are you not using basic sh ( for compliance...)?

So now you have the beginning in working order
Explain me your choice for your while loop (If it should work in bash, Im no help...)