LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-09-2005, 05:30 PM   #1
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Rep: Reputation: 15
Need help with a bash script


I'm working on a little script at the moment and have got myself stuck. Basically I want the script to take an argument (a filename) and then parse the argument and remove the file extension. So for example if I submit:

./myscript test.pov

It should return
"The filename is now test"

and should remove the file extension. It prints out the following error message:
tr: empty string2

I am assuming this is because I left '' unspecified in the tr part of it. I was thinking it should be able to just remove that part of the string... but obviously not. Can anyone help me on getting this simple script to run? Much appreciated




#!/bin/bash
#here is myscript
for filename in "$@"
do
echo "Examining file $filename"
$filename = `echo $filename | tr '.pov' ' '`
echo "filename is now $filename"
done
 
Old 03-09-2005, 06:15 PM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
I think the problem here is an incorrect use of tr.

tr (used in this manner) only replaces the occurence of one character with another.

What it is trying to do in your script is replace each character in the first list with the corresponding character in the second list. But the second list is empty, hence the error message.

If the second list is shorter than the first list it will replace each succeeding character in the first list with the last character in the second list. (I think - man tr to be sure).

I think sed would be more suitable for this,
Code:
filename=$(echo $filename | sed -e 's/\.pov$//')

Last edited by ahh; 03-09-2005 at 06:22 PM.
 
Old 03-09-2005, 06:33 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Alternatively use basename :)
man basename
for details


Cheers,
Tink
 
Old 03-09-2005, 07:03 PM   #4
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
Excellent thanks much guys. That was just what I was looking for. I knew something wasn't quite right. many thanks!!!
 
Old 03-09-2005, 07:18 PM   #5
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
Whoops one more question while I'm at it. If I wanted to add something back to the variable how would I do that.. here's a better idea of what is going on...

for filename in "$@"
do
echo "Examining file $filename"
/opt/bin/local/povray $filename +W320 +H180
file=$(echo $filename | sed -e 's/\.pov$//')
echo "filename is now: $file"
curl -T $file.png -u userassword -O ftp://test.ca/public_html/xgrid/
echo "jobs done"
done

So basically what I want to do is render a file in povray, then strip the povray extension from the file, and then upload the finished result (a .png file) back to my server. I was thinking that sed could again be used but I just can't figure out the syntax... I'm a noob.
 
Old 03-09-2005, 07:23 PM   #6
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
If you just want to change .pov to .png,
Code:
sed -e 's/pov$/png/'
 
Old 03-09-2005, 07:25 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Quote:
Originally posted by obelxi

curl -T $file".png" -u user:password -O ftp://test.ca/public_html/xgrid/


Cheers,
Tink
 
Old 03-10-2005, 07:49 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You need to convert the files from one format to another. Imagemagick has a 'convert' program to do this.

example: convert frame1.pov frame1.png

There are many options. You can even reduce the speckle on a picture while converting.

To convert a directory full of pov's to png's, you could use this oneliner:
for pic in *.pov; do convert ${pic} ${pic%.pov}.png

If the file names contain spaces or 'evil' characters, you need to put the variables inside double quotes.

Here I used variable substitution to strip off the .pov extension from the filename.
 
Old 03-10-2005, 10:57 AM   #9
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks a lot guys. That was just the info I needed. I don't need to convert the files with image majick because when povray renders the files it automatically outputs to a .png file. It's good to know that you can do it that way though.
 
Old 03-10-2005, 06:36 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
In that case, you just need to change from using the 'convert' command to the 'mv' command.

If the files are contained in different subdirectories, the 'find' command may be preferred to a 'for' loop, calling a script or bash function

find ./povrootdir -iname "*.pov" -exec pov2png "{}" \;
---
#!/bin/bash
# Change extension from .pov to .png

mv "$1" "${1%.pov}.png"
---

However, if you are producing a script for more than interactive use, you will want to include error correction, such as checking the extension, so you don't produce a 'file.POV.png' or 'file.jpg.png' if it is called incorrectly.

Well I'll let you get back to the fun of 3d animation. I'm into using Blender3d myself, however it's been a while.
 
Old 04-07-2005, 10:14 PM   #11
phorensic
LQ Newbie
 
Registered: Apr 2005
Location: Corona, CA
Distribution: Debian - Sarge
Posts: 2

Rep: Reputation: 0
I have a directory full of *.jpg.html files and I'm trying to use these methods to strip out the '.jpg' part, however I am having no luck. I might need some special characters in my command to do it properly or something.

What would be a better solution is if I could get the original command that created the files to strip out the .jpg part. Here is my situation. I have a directory full of JPG files that I need a corresponding page assigned the name of each. For example: P319004.jpg needs a page called P319004.html. The page that it creates must have a template inside of it. I've sucessfully done that with this command:
#ls *.jpg | awk '{print "cp 'template.html' "$1".html"}' | sh
template.html being the template that it copies into the new file it creates.

But that creates P319004.jpg.html. I want to strip out the '.jpg' part. Any help is extremely appreciated, as this will save me tens of hours doing it manually on a Windows box.

BTW, I still need to search the files and replace the references to the photos to the correct photos, but I'll figure that stuff out later.
 
Old 04-08-2005, 04:07 AM   #12
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
If I understand what you're trying to do, I think this will work:-
Code:
for i in $(ls | grep "\.jpg$"); do filename=$(echo $i | sed -e 's/\.jpg$//'); cp template.html $filename.html; done
This assumes template .html is in the same directory as the jpg's, if not you will have to append the path to template.html
 
Old 04-08-2005, 11:21 AM   #13
phorensic
LQ Newbie
 
Registered: Apr 2005
Location: Corona, CA
Distribution: Debian - Sarge
Posts: 2

Rep: Reputation: 0
Yep, that works. Thank you so much. Of course it was way more complicated than my newb scripting skills could come up with. Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script zaicheke Programming 3 11-07-2004 06:32 PM
bash script how to? Lleb_KCir Programming 11 05-21-2004 07:03 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration