LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 07-21-2006, 02:29 AM   #1
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Rep: Reputation: 30
Simple quick one - how to set a variable from a text file...


All I need to do is set a variable in a script from the contents of a text file.

text.file contains
variable text in this file

and I want to set a variable I can use in the script with that string:

so I can get something like:

echo "${title}"

from within my script and get:

variable text in this file

The actual use is to get the contents of the file to act as the title in an ImageMagick convert -draw call:

Code:
addTitleText=$(convert -fill white -draw 'text 100,100 '${title} /tmp/img.$rand /tmp/titled 2>&1)
 
Old 07-21-2006, 02:36 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
title=`cat file`


Cheers,
Tink
 
Old 07-21-2006, 02:42 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
or
Code:
title = `< file`
 
Old 07-21-2006, 02:49 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
or
Code:
title=$(<file)
 
Old 07-21-2006, 05:07 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
or, if format is like so:

variable="thing"

you can dot it:

. text_file
 
Old 07-21-2006, 05:37 AM   #6
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
not understanding you Billy :? (bit new to some of this )

Thanks all, that has me sorted
 
Old 07-21-2006, 05:46 AM   #7
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
...actually it has me with one more problem...

I only get the first word of the title:

Code:
++ convert -fill white -draw 'text 100,100 Strangers' on Mars /tmp/img.a6cc613fd9f7297ba78b1c3c8d /tmp/titled
+ addTitleText='convert: unable to open image `on'\'': No such file or directory.
convert: unable to open image `Mars'\'': No such file or directory.'
++ xloadimage -onroot -center -border black -fullscreen /tmp/titled
+ loadImgRes='/tmp/titled is a 727x540 JPEG image, color space YCbCr, 3 comps, Huffman coding.
  Merging...done
  Building XImage...done'
Code:
title=`< /tmp/title.txt`
...
addTitleText=$(convert -fill white -draw 'text 100,100 '$title /tmp/img.$rand /tmp/titled 2>&1)
The title in the file is

Strangers on Mars

and so of course the spaces in the string are being seen by convert as separate files...

Thanks guys

Will
 
Old 07-21-2006, 05:56 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Code:
title=$(</tmp/title.txt)
...
addTitleText=$(convert -fill white -draw "text 100,100 '$title'" /tmp/img.$rand /tmp/titled 2>&1)
or even, if $title is not used elsewhere:

Code:
addTitleText=$(convert -fill white -draw "text 100,100 '$(</tmp/title.txt)'" /tmp/img.$rand /tmp/titled 2>&1)
 
Old 07-21-2006, 06:08 AM   #9
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by jlliagre
Code:
title=$(</tmp/title.txt)
...
addTitleText=$(convert -fill white -draw "text 100,100 '$title'" /tmp/img.$rand /tmp/titled 2>&1)
or even, if $title is not used elsewhere:

Code:
addTitleText=$(convert -fill white -draw "text 100,100 '$(</tmp/title.txt)'" /tmp/img.$rand /tmp/titled 2>&1)

that did it jlliagre

I guess I have much to understand about how the scripts interpret ' and " and when they are interchangable for different effect:

ie

program -option 'this needs "this here to work"'

and

program -option "this can 'be done this way if necessary'"


Last edited by stardotstar; 07-21-2006 at 06:09 AM.
 
Old 07-21-2006, 06:27 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
i'm kinda not understanding the posts here but if you want the whole line or even the whole text of the file, you can do

Code:
variable="$(<file)"
just in case something's missing
 
Old 07-21-2006, 10:07 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by konsolebox
i'm kinda not understanding the posts here but if you want the whole line or even the whole text of the file, you can do

Code:
variable="$(<file)"
just in case something's missing
Actually not, the quotes are unnecessary here, whatever the file "file" contains,

Code:
variable=$(<file)
is exactly equivalent to
Code:
variable="$(<file)"
 
Old 07-21-2006, 10:15 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by stardotstar
that did it jlliagre

I guess I have much to understand about how the scripts interpret ' and " and when they are interchangable for different effect:

ie

program -option 'this needs "this here to work"'

and

program -option "this can 'be done this way if necessary'"

in the first case, program will receive as second argument this needs "this here to work", while in the second one, that will be this can 'be done this way if necessary'


The shell is removing the surrounding single or double quotes, the difference being variables are not expanded when single quotes are used, try for example:
Code:
a=test
echo $a
echo "$a"
echo '$a'
echo "'$a'"
echo '"$a"'
 
Old 07-21-2006, 05:53 PM   #13
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
Thanks very much for the clarification it is invaluable

I did try that and got:

Code:
stardotstar@geko ~ $ ./testquote.sh
test
test
$a
'test'
"$a"
I will cogitate on this further and it should sink in with more use.

Will
 
Old 07-21-2006, 06:12 PM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by jlliagre
Code:
variable=$(<file)
is exactly equivalent to
Code:
variable="$(<file)"
sorry. maybe it was in bash 2.05b that only the first line of the text is included. my mistake.

as it was always helpful to me when i'm doing:
Code:
variable="text
abcdefgh"
i never knew $(<file) is equivalent to "$(<file)"

so i'll take note of that. thank you.
regards

Edit: if you're trying to append text btw. you can do
Code:
variable"$(<file) new text"
or
Code:
variable=$(<file)" new text"
but you can never do
Code:
variable=$(<file) new text

Last edited by konsolebox; 07-21-2006 at 06:23 PM.
 
Old 07-21-2006, 08:17 PM   #15
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
In the line:
Code:
addTitleText=$(convert -fill white -draw "text 100,100 '$title'" /tmp/img.$rand /tmp/titled 2>&1)'
The argument for -draw needs to be a string. Since part of it is a variable $title, you need to place it in double quotes.

-draw 'text 100,100 "' "$title" "'"

For example:
Code:
title="$(<text)"
convert -draw 'text 100 100 "'"$title"'"' car.jpg carout.jpg
The argument to -draw is 'text 100 100 "Strangers on Mars"'

Last edited by jschiwal; 07-21-2006 at 08:22 PM.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
how to send shell variable data to a text file ginda Programming 7 06-23-2006 05:49 AM
set a line in a file as a variable tpreitano Linux - General 5 08-24-2005 11:53 AM
NEED AWNSER QUICK how do I save editing a text file in Konsole? DDRfreak2 Mandriva 1 08-18-2005 06:09 PM
how to cat a text file and save it as a variable mrobertson Programming 37 07-05-2005 08:20 AM


All times are GMT -5. The time now is 11:28 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration