The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers > Homework & Coursework Questions
.
google unix.com



Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to match the last XML extension by using Case statement sunitachoudhury Shell Programming and Scripting 3 04-06-2008 01:19 AM
change file extension from root and subdirectories Astrid Shell Programming and Scripting 10 02-17-2008 07:18 AM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 01:41 PM
how do i change extension kswaraj Shell Programming and Scripting 2 06-28-2004 08:07 PM
How to change extension? prkwan Shell Programming and Scripting 4 11-16-2002 07:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
Kdenmen Kdenmen is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
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.

2. Relevant commands, code, scripts, algorithms:

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


3. The attempts at a solution (include all code and scripts):

Code:
CreateFile(){
    if [ ! -f $1 ]
    then
        touch $1 > /dev/null 2>&1 && echo "File $1 created"
    else
        echo "Error: $1 file exists!"
    fi

[/size][/font][/font]  [font=Verdana][size=2]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.

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

Last edited by DukeNuke2; 4 Weeks Ago at 12:42 PM..
  #2 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 584
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.
  #3 (permalink)  
Old 4 Weeks Ago
Kdenmen Kdenmen is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
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.
Code:
#!/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
  #4 (permalink)  
Old 3 Weeks Ago
Kdenmen Kdenmen is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
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.

Code:
#!/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:

Code:
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.
  #5 (permalink)  
Old 2 Weeks Ago
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,568
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:
Code:
#!/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:
Code:
 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...)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0