Exporting a variable to a variable in Bash?

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
I'm running Ubuntu Fiesty on my personal box, and to get around a problem described here: http://ubuntuforums.org/showthread.php?t=438639, I tried to do this in a shell script:


#!/bin/sh
export DBUS_SESSION_BUS_ADDRESS=`echo $DBUS_SESSION_BUS_ADDRESS` && rhythmbox-client --play-pause


The problem is that the echo statement in the backtick goes into the export, not the eval-ed value. I tried a few combinations of "sh -c ..." but nothing seemed to work. BTW I'm on bash.

Pointers?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
You can't just have $DBUS_SESSION_BUS_ADDRESS by itself? :confused:

EDIT: Actually I'm much more confused. Do you want DBUS_SESSION_BUS_ADDRESS to equal DBUS_SESSION_BUS_ADDRESS or the value contained in DBUS_SESSION_BUS_ADDRESS?
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
Check out his original thread, n0c. He's trying to get a session-specific DBUS_SESSION_BUS_ADDRESS into a cron job, which is difficult because cron doesn't use the same environment as the session he wants to pull the info from.

OP, I don't think that the way you're trying is going to work, regardless of syntax. The problem is that the variable doesn't exist in cron's environment. Adding export statements is pointless - if you run that script from cron, the variable still isn't available to export, because the script is started from cron's env. I would try to either a) get that variable into a text file when the session is started (then your script can parse it) or b) find a way to talk to dbus directly and get the information from there. Not knowing a lot about dbus, I'm not sure how to accomplish either of these choices, but I'm pretty sure that playing around with export statements is not going to get you anywhere.

Also, out of idle curiosity, what are you doing with rhythmbox that you want it to turn off and on every minute?
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
OK, here's a simple, dirty way. I don't know enough about dbus to say whether it's correct or reliable. Just create a script savebusaddress.sh that does...

echo $DBUS_SESSION_BUS_ADDRESS > ~/.busaddress

Make it executable and add the savebusaddress.sh script to your GNOME session (System->Pref->Sessions). Then your cron script startstoprb.sh looks like...

export DBUS_SESSION_ADDRESS="`cat ~/.busaddress`"
rhythmbox-client --play-pause

Seems like that should work.

edit: one >, not two
 

lousydood

Member
Aug 1, 2005
158
0
0
Also, just use:

DBUS_SESSION_BUS_ADDRESS="`cat ~/.busaddress`" rhythmbox-client --play-pause

if you want the environment variable to be in scope only for this one invocation of rhythmbox-client.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Thanks for the replies!

I have thought of doing what cleverhandle described but I was hoping that I can get away with it :p I didn't know that cron runs its own environment so that bit helps a lot.

I'm not trying to run it every minute, just every Monday to Friday at 9am so I can wake up to my playlist ;) I used "--play-pause" just for testing - it stops if it's playing and vice versa.


Edit: BTW it works perfectly. Thanks!