LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2006, 12:50 PM   #1
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 44
If, else, then or loop, help.


I have a script that changes all file extensions from .jpg to .JPG. I have written the script to go from .JPG to .jpg as well. What I need help with is. How would I edit the script to do either convert them from .jpg to .JPG or .JPG to .jpg


My current script is:


-------------------------------------

#!/bin/bash
# I just run the script and make sure I put a # at the start of the line I don't # need.


for i in *.jpg; do j=`basename "$i" .jpg`; mv -v "$i" "$j.JPG"; done

for i in *.JPG; do j=`basename "$i" .JPG`; mv -v "$i" "$j.jpg"; done

---------------------------------------



How would I make it check for jpg and change it to JPG, if there are no jpg, then it needs to check for JPG and then convert them to jpg.

Thanks
 
Old 02-23-2006, 01:01 PM   #2
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
Use If statements.
 
Old 02-23-2006, 01:20 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The following will try to rename JPG in all filenames ending in JPG to jpg. If there are no JPG files, it will try to rename jpg in all filenames ending in jpg to JPG:
Code:
rename JPG jpg *JPG || rename jpg JPG *jpg
It's not perfect, but it's quick - hope it helps...
 
Old 02-23-2006, 01:58 PM   #4
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 44
Hmm i get this error:

rename JPG jpg *JPG || rename jpg JPG *jpg
Bareword "JPG" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "jpg" not allowed while "strict subs" in use at (eval 1) line 1.


Its cool. I just created 2 seperate scripts, one to make it lower case to upper, one to make it upper to lower. Thanks.


Oh and ^^, no shit. I tried if statements, couldn't figure out how to write it. Hence the post.
 
Old 02-23-2006, 05:42 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I'm glad you've got a working solution. Just out of curiousity though - did you try the rename command wrapped in a perl script? I've never seen that error in a regular bash script before (and it works in my bash script).
 
Old 02-23-2006, 09:43 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Looks like he changed langs in midflow ;-)
 
Old 02-23-2006, 09:46 PM   #7
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Yeah - I did wonder the same thing
 
Old 02-23-2006, 11:25 PM   #8
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 44
Steve,

I just ran the rename command you posted in a command line as root, just to see what would happen. I may play with it later in a bash script. It works fine now with me just having 2 seperate scripts. One makes the .jpg to .JPG the other makes .JPG to .jpg.



oh and what's this supposed to mean??


----------------------------------------

Looks like he changed langs in midflow ;-)
__________________
 
Old 02-23-2006, 11:56 PM   #9
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
We both thought that you started in bash and then switched to perl - the only place I've seen the error about barewords is in perl.
 
Old 02-24-2006, 03:04 PM   #10
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 44
ahhh I thought he meant I changed languages, as in english to something else... Anyways.. I bet if you run that rename command from the commandline you'll see the bareword error
 
Old 02-24-2006, 03:07 PM   #11
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Umm, no I don't. On my PC it works fine - no errors and it does the rename both ways. It's odd that you're getting that error. The reason I thought you were using perl is that if you look at man perldiag you can see that same error message.

Regardless - I'm glad you have it working

Last edited by gilead; 02-24-2006 at 03:10 PM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
No loop# SeaSharp Linux - Newbie 3 09-06-2005 08:14 PM
WHILE LOOP in 'C' ']['HeBroken Programming 4 10-29-2004 01:42 AM
while-loop Thomas23 Programming 4 05-24-2004 03:35 PM
for loop klfreese Programming 16 08-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration