LinuxQuestions.org
Help answer threads with 0 replies.
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 01-08-2006, 06:56 PM   #1
osio
Member
 
Registered: Jun 2005
Posts: 70

Rep: Reputation: 15
read values of variables from a table


I'd like to write a shell script to manipulate images with some commands:
Quote:
convert -resize 1024x768 image.png
composite Mark1.png image.png watermarked.jpg
convert -gravity Center -draw "text 0,0 'Screen'" watermarked.jpg done.jpg
just by entering the name of the set of values
Quote:
#!/bin/bash

echo -n "Enter the image to convert: "
read image

echo -n "Enter the format to convert: "
read format

convert -resize [value1]x[value2] $image
composite [value3].png $image /tmp/watermarked.jpg
convert -gravity Center -draw "text 0,0 '[value4]'" /tmp/watermarked.jpg done.jpg
where the values are in a table:
format | value1 | value2 | value3 | value4
Large | 1024 | 768 | Mark1 | Screen
Regular| 800 | 600 | Mark1 | Monitor
Small | 320 | 240 | Mark2 | Monitor
Tiny | 160 | 120 | Mark2 | Mobile
How do I do that?
Thank you.
Osio
 
Old 01-09-2006, 06:38 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
If the table is in a ASCII text file called "file.txt", do something like:
Code:
#!/bin/bash

IFS='|'
while read format value1 value2 value3 value4 ; do
        # do processing of each line here
        # variables: format, value1,.. will contain the
        # fields of each line processed in this loop.

        echo "format = $format"
        echo "value1 = $value1"
        echo "value2 = $value2"
        echo "value3 = $value3"
        echo "value4 = $value4"
        echo
done <file.txt
 
Old 01-09-2006, 11:27 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I would get the spaces out of the table and maybe use a more standard separater like comma or tab.
Code:
format,value1,value2,value3,value4
Large,1024,768,Mark1,Screen
Regular,800,600,Mark1,Monitor
Small,320,240,Mark2,Monitor
Tiny,160,120,Mark2,Mobile
Here's what I have so far....
Code:
#!/bin/bash

read -p "Enter the image to convert: " image
read -p "Enter the format to convert: " format

value2=$(grep "$format" < file.txt | awk -F"," '{print$2}')
value3=$(grep "$format" < file.txt | awk -F"," '{print$3}')
value4=$(grep "$format" < file.txt | awk -F"," '{print$4}')
value5=$(grep "$format" < file.txt | awk -F"," '{print$5}')


convert -resize "$value2"x"$value3"  $image
composite $value4.png $image /tmp/watermarked.jpg
convert -gravity Center -draw "text 0,0 '$value5 '" /tmp/watermarked.jpg $image1.jpg

#Note: you probably can't use done.jpg as done is a reserved word.
 
Old 01-10-2006, 09:20 AM   #4
osio
Member
 
Registered: Jun 2005
Posts: 70

Original Poster
Rep: Reputation: 15
Yes, it works as expected.
The spaces and '|' separators are a bit cumbersome for larger tables, so I opted for the commas. I also need to sit down and try to understand 'IFS'.
If it helps to future readers, this is what I ended up with:
Quote:
#!/bin/bash

read -p "Enter the image to convert: " image
read -p "Enter the format to convert: " format

value2=$(grep "$format" < file.txt | awk -F"," '{print$2}')
value3=$(grep "$format" < file.txt | awk -F"," '{print$3}')
value4=$(grep "$format" < file.txt | awk -F"," '{print$4}')
value5=$(grep "$format" < file.txt | awk -F"," '{print$5}')

echo $image
convert -resize "$value2"x"$value3" $image /tmp/${image%%.*}.jpg
composite -dissolve 25% $value4.png /tmp/${image%%.*}.jpg /tmp/${image%%.*}_comp.jpg
convert -gravity Center -draw "text 0,0 '$value5 '" /tmp/${image%%.*}_comp.jpg ${image%%.*}2.jpg
display ${image%%.*}2.jpg
exit
Thanks a lot Hko and homey.
Osio
 
Old 01-10-2006, 10:27 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
ugh that's nasty (sorry!)
four pipelines of grep into awk?

why didn't you use the nice elegant solutions?

as an aside, you can use set to set the $n variables:
Code:
billym.>line='one two three four'
billym.>set -- $line
billym.>echo $4 $3 $2 $1         
four three two one
 
Old 01-10-2006, 11:19 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
why didn't you use the nice elegant solutions?
I know my programming skills are nonexistant/suck.
Could you post your script so I can learn from it?
 
Old 01-10-2006, 07:48 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
If it was a snake, I'd be in trouble.
Thanks bigearsbilly!
Code:
Large 1024 768 Mark1 Screen
Regular 800 600 Mark1 Monitor
Small 320 240 Mark2 Monitor
Tiny 160 120 Mark2 Mobile
Code:
#!/bin/bash

read -p "Enter the image to convert: " image
read -p "Enter the format to convert: " format

line=$(grep "$format" < file.txt)
set -- $line

echo $image
convert -resize "$2"x"$3" $image /tmp/${image%%.*}.png
composite -dissolve 25% $4.png /tmp/${image%%.*}.png /tmp/${image%%.*}_comp.png
convert -gravity Center -draw "text 0,0 '$5 '" /tmp/${image%%.*}_comp.png ${image%%.*}2.png
display ${image%%.*}2.png
exit

Last edited by homey; 01-10-2006 at 11:04 PM.
 
Old 01-11-2006, 02:52 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239

I think HKO's was the nicest looking!

also, why the read -p
that's usually used for a spawned background process,
like:
Code:
find . -type d |&
# blah
read -p
sorry to be a moaner

Last edited by bigearsbilly; 01-11-2006 at 02:59 AM.
 
  


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
Re-read Partition Table sourceman Linux - General 11 01-10-2008 04:31 AM
Mandrake 10 can't read my partition table... robza Mandriva 3 04-13-2004 11:58 AM
(Bash) Saving READ values in external .conf files spikylee Linux - Newbie 4 10-28-2003 06:46 AM
Knoppix can't read partition table sb73542 Linux - General 1 10-13-2003 09:55 AM
Unable To Re-Read The Partition Table? EMF9 Linux - General 13 12-08-2002 06:47 AM

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

All times are GMT -5. The time now is 01:30 AM.

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