How to read a files content to a variable? <Solved>

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

How to read a files content to a variable? <Solved>

#1 Post by e_mattis »

hey!

I'm playing around with "sampling" desktop icons. basically, you choose an icon theme, try it out, if you don't like it, restore your original theme. Much like the icon_switcher but having script remember the original theme.

I thought iI knew how to read the contents of a file :roll: but apparently i'm not doing something right. So I went back through all the information I could find on it. Now i'm more confused that ever :shock:

I'm certain this is a simple one-line process that i'm just not grasping. I've tried some things such as:

Code: Select all

NewTheme = [cat /etc/desktop_icon_theme]

NewTheme = " ' cat /etc/desktop_icon_theme' "

NewTheme = $('cat /etc/desktop_icon_theme')
and several others. Nothing seems to capture the contents in the variable. Anyone who can, correct my confusion plz :D

Thanks!

E
Last edited by e_mattis on Mon 12 May 2014, 23:36, edited 1 time in total.

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#2 Post by rufwoof »

Not sure about other keyboards, but for a UK keyboard you use the downward sloping single quote that's just to the left of the 1 key (as in 1 2 3 ..etc)

current_dir=`pwd`

for instance. So for a file

content=`cat somefile`

You can then use that captured data inside the script by prefixing with a $ $current or $content for example

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#3 Post by amigo »

Code: Select all

NewTheme="$(cat /etc/desktop_icon_theme)"
or:

Code: Select all

NewTheme="`cat /etc/desktop_icon_theme`"

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: How to read a files content to a variable

#4 Post by L18L »

For read use read :D

Code: Select all

read NewTheme < /etc/desktop_icon_theme
which will read one line.

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#5 Post by e_mattis »

Thanks guys!

I've tried each one of those but get the same results - the icons do not revert back to the original and the test save file (saving as desktop_icon_theme-test) shows nothing in the file - have even had it echo to a window on screen and shows nothing - indicating that the variable does not contain anything.

Is there something i'm missing here?

thanks

E

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#6 Post by L18L »

If the file is empty then it is OK if the variable is also empty.
What did you expect?

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#7 Post by RSH »

If you are changing the icon theme you'll need to restore this theme - better saying its icons.

Changing a theme overwrites icons in /usr/local/lib/X11/pixmaps with the icons from the theme chosen. These themes are in /usr/local/lib/X11/themes (sub-directory's name is the theme name).

So you would need to call /usr/sbin/icon_switcher or /usr/sbin/icon_switcher_cli and also to submit the original theme.

E.g.

Code: Select all

/usr/sbin/icon_switcher_cli "crystal"
To restore only content of file /etc/desktop_icon_theme just won't work.
Last edited by RSH on Mon 12 May 2014, 20:54, edited 1 time in total.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#8 Post by RSH »

This works over here:

Get the original theme and store it:

Code: Select all

read OrigTheme < /etc/desktop_icon_theme
echo $OrigTheme > /tmp/original_icon_theme
Restore to original theme:

Code: Select all

read OrigTheme < /tmp/original_icon_theme
/usr/sbin/icon_switcher_cli "$OrigTheme"
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#9 Post by e_mattis »

UPDATE: that seems to have done the trick! Gonna mark it solved and move on. thanks guys! you're awesome!

-----------------------------------------------------------------------------------
thanks guys

@L18L
What I meant was the file I have the program write as a test to see if the variable contained any data was empty, not the file i'm reading from :D

@RSH
Will try that and make sure, but it looks familiar so I think that will work for me. Will let yo know in a little while.

Thanks a lot!

E

Post Reply