LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need help with a bash script (https://www.linuxquestions.org/questions/linux-general-1/need-help-with-a-bash-script-299779/)

obelxi 03-09-2005 05:30 PM

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 :D




#!/bin/bash
#here is myscript
for filename in "$@"
do
echo "Examining file $filename"
$filename = `echo $filename | tr '.pov' ' '`
echo "filename is now $filename"
done

ahh 03-09-2005 06:15 PM

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$//')

Tinkster 03-09-2005 06:33 PM

Alternatively use basename :)
man basename
for details


Cheers,
Tink

obelxi 03-09-2005 07:03 PM

Excellent thanks much guys. That was just what I was looking for. I knew something wasn't quite right. :D many thanks!!!

obelxi 03-09-2005 07:18 PM

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 user:password -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. :(

ahh 03-09-2005 07:23 PM

If you just want to change .pov to .png,
Code:

sed -e 's/pov$/png/'

Tinkster 03-09-2005 07:25 PM

Quote:

Originally posted by obelxi

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


Cheers,
Tink

jschiwal 03-10-2005 07:49 AM

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.

obelxi 03-10-2005 10:57 AM

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.

jschiwal 03-10-2005 06:36 PM

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.

phorensic 04-07-2005 10:14 PM

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.

ahh 04-08-2005 04:07 AM

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

phorensic 04-08-2005 11:21 AM

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


All times are GMT -5. The time now is 10:40 PM.