If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Renaming files in Bash - Script Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-30-03, 10:12
warlord warlord is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Renaming files in Bash - Script Help

I'm trying to write a script that allow the user to delete the prefix of a file, insert (append) a prefix, rename a prefix and rename the extension. So far all the options but i) work. Any suggestions how I can have bash insert a prefix to an existing file name ex.
(29.txt to burp29.txt) in the context of this script?

Thanks Pete

#!bash
clear

echo
echo -n -e "\033[40;33m RENAME FILES\033[0m" "\033[40;32m$PIXEL_SIZE \033[0m"
echo
echo
echo "******************************************"
echo "* Choose the correct Spots Method *"
echo "* ------------------------------- *"
echo "* *"
echo "* d)Delete text (burp29.txt to 29.txt) *"
echo "* *"
echo "* i)Insert text (29.txt to burp29.txt) *"
echo "* *"
echo "* f)rename filetext (29.txt to bur.txt) *"
echo "* *"
echo "* e)rename ext (29.txt to 29.xxx) *"
echo "* *"
echo "* x)it *"
echo "* *"
echo "******************************************"
echo;echo;echo;echo;echo;echo
echo
read job

################################################## ################################

if [ $job = d ]; then
clear
ls
echo
echo -n -e "\033[40;32m ENTER TEXT TO DELETE \033[0m"
read DEL
echo
null=""
if [ $DEL = $null ]; then
echo
echo
echo
echo "ERROR: ENTER TEXT:"
echo
echo
echo
exit 1
fi

array=($DEL*)
for file in ${array[@]};do
file1=${file//$DEL/}
mv $file $file1
done

exit 1

################################################## ################################

################################################## ################################
elif [ $job = i ]; then
clear
ls
EXT=*.*
echo
echo -n -e "\033[40;32m ENTER TEXT TO INSERT \033[0m"
read INS
echo
null=""
if [ $INS = $null ]; then
echo
echo
echo
echo "ERROR: ENTER TEXT:"
echo
echo
echo
exit 1
fi


array=($INS)
for file in ${array[@]};do
file1=${file//$INS/$EXT/}
mv $file $file1
done

exit 1


################################################## ################################
elif [ $job = f ]; then
clear
ls
echo
echo -n -e "\033[40;32m ENTER TEXT TO CHANGE \033[0m"
read TC
echo
null=""
if [ $TC = $null ]; then
echo
echo
echo
echo "ERROR: ENTER TEXT TO CHANGE:"
echo
echo
echo
fi

echo
echo -n -e "\033[40;32m ENTER TEXT TO CHANGE TO: \033[0m"
read TW
echo
null=""
if [ $TW = $null ]; then
echo
echo
echo
echo "ERROR: ENTER TEXT TO CHANGE TO:"
echo
echo
echo
exit 1
fi
array=($TC*)
for file in ${array[@]};do
file1=${file//$TC/$TW}
mv $file $file1
done

exit 1

################################################## ################################
elif [ $job = e ]; then
clear
ls
echo
echo -n -e "\033[40;32m ENTER EXT TO CHANGE (case sensitive): \033[0m"
read E1
echo
null=""
if [ $E1 = $null ]; then
echo
echo
echo
echo "ERROR: ENTER EXT TO CHANGE:"
echo
echo
echo
exit 1
fi

echo
echo -n -e "\033[40;32m ENTER EXT TO CHANGE TO (case sensitive): \033[0m"
read E2
echo
null=""
if [ $E2 = $null ]; then
echo
echo
echo
echo "ERROR: ENTER EXT TO CHANGE TO:"
echo
echo
echo
exit 1
fi



array=(*.$E1)
for file in ${array[@]};do
file1=${file//$E1/$E2}
mv $file $file1
done

exit 1
################################################## ################################
################################################## ###############################
elif [ $job = x ]; then
echo
echo -n -e "\033[40;31m RENAME ABORTED \033[0m"
echo
echo
exit 1
################################################## ################################
else
echo " "
echo "*** Pick either d, i, f, e or x***"
echo
fi


#EOF

Last edited by warlord; 10-30-03 at 11:11.
Reply With Quote
  #2 (permalink)  
Old 10-31-03, 11:00
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Lightbulb

Try this:

Code:

array=($EXT)
for file in ${array[@]};do
  file1="${INS}${file}"
  mv $file $file1
done
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 11-03-03, 08:23
warlord warlord is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Nope that didn't seem to work either. Keeps saying Urinary expected or something like that...
Reply With Quote
  #4 (permalink)  
Old 11-03-03, 15:22
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Exclamation

The error you are getting is unrelated to the code I posted, try reviewing your logic, specially around this statement:

elif [ $job = i ]; then

because you have:

if [ $job = d ]; then
...blah...blah...
exit 1
fi
...more blah....
exit 1
elif ...

It is very doubtfull the job = i statement will ever be executed!

Also you may want to code your if statements like this:

if [[ "$job" == "i" ]]; then


__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #5 (permalink)  
Old 11-03-03, 17:45
warlord warlord is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Ok, the problem is I still can't insert any prefixes to files. Just simply then say I have 20 files called 1.tif, 2.tif, 3.tif etc and I wanted to write a simple script to add the prefix (bos) to all the *.tif files as in (bos1.tif, bos2.tif).

That might be the easiet way for me to work this problem out as I'm no Bash expert here.

Thanks...
Reply With Quote
  #6 (permalink)  
Old 11-03-03, 18:39
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Exclamation

Same coding:

Code:

#!/bin/bash
INS='bos'
array=$(ls *.tif)
for file in ${array[@]};do
  file1="${INS}${file}"
  mv $file $file1
done
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #7 (permalink)  
Old 11-04-03, 04:26
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by LKBrwn_DBA [/i]
Same coding:

Code:

#!/bin/bash
INS='bos'
array=$(ls *.tif)
for file in ${array[@]};do
  file1="${INS}${file}"
  mv $file $file1
done
Isn't 'array' just going to be a string in this instance? Using ${array[@]} in that case would simply split the array string where it finds an IFS character and assign each token to 'file'.

I would try something like this..

INS=bos
for file in *.tif; do
! test -f $INS$file && mv $file $INS$file || echo Could not rename file $file
done

HTH
Reply With Quote
  #8 (permalink)  
Old 11-04-03, 10:49
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Exclamation

Quote:
Originally posted by Damian Ibbotson
Isn't 'array' just going to be a string in this instance? Using ${array[@]} in that case would simply split the array string where it finds an IFS character and assign each token to 'file'.

I would try something like this..

INS=bos
for file in *.tif; do
! test -f $INS$file && mv $file $INS$file || echo Could not rename file $file
done

HTH
True, I was only trying to follow the original code posted, in fact, array=$(ls *.tif) is incorrect in my code, it should be array=(ls *.tif) (without the '$')
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #9 (permalink)  
Old 11-04-03, 11:01
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by LKBrwn_DBA
True, I was only trying to follow the original code posted, in fact, array=$(ls *.tif) is incorrect in my code, it should be array=(ls *.tif) (without the '$')
I've seen that syntax before but I'm not sure which shells it works with. Probably bash!

I'm stuck with...

set -A array a b c; echo ${array[@]}

:-(
Reply With Quote
  #10 (permalink)  
Old 11-04-03, 18:17
warlord warlord is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Ok I'll try that out. My Bash is pretty remedial, but then again I guess so is the simple thing I'm trying to do here. Thanks guys...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On