LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple quick one - how to set a variable from a text file... (https://www.linuxquestions.org/questions/programming-9/simple-quick-one-how-to-set-a-variable-from-a-text-file-466140/)

stardotstar 07-21-2006 02:29 AM

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)

Tinkster 07-21-2006 02:36 AM

title=`cat file`


Cheers,
Tink

spooon 07-21-2006 02:42 AM

or
Code:

title = `< file`

jlliagre 07-21-2006 02:49 AM

or
Code:

title=$(<file)

bigearsbilly 07-21-2006 05:07 AM

or, if format is like so:

variable="thing"

you can dot it:

. text_file

stardotstar 07-21-2006 05:37 AM

not understanding you Billy :? (bit new to some of this :) )

Thanks all, that has me sorted :)

stardotstar 07-21-2006 05:46 AM

...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

jlliagre 07-21-2006 05:56 AM

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)

stardotstar 07-21-2006 06:08 AM

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

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'"

:)

konsolebox 07-21-2006 06:27 AM

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

jlliagre 07-21-2006 10:07 AM

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)"

jlliagre 07-21-2006 10:15 AM

Quote:

Originally Posted by stardotstar
that did it jlliagre :cool:

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"'


stardotstar 07-21-2006 05:53 PM

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

konsolebox 07-21-2006 06:12 PM

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

jschiwal 07-21-2006 08:17 PM

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"'


All times are GMT -5. The time now is 07:59 PM.