You are not logged in.
Hello every body,
What I'd like to now is: is there a way for you to concatenate the expansions of variables to get a string which is the same as the name of a
variable and then expand that string?
like i have variables x = var and y = iable and then i want to make z = variable by using x and y.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here's some context:
I'm trying to right my fisrt bash script that does something meaningful, I've found a number of good resources on the topic, but I'm still
banging my head into a brick wall as far as making the script do what I want to do.
Basically, I would like to use the script to simultanously change the theme for SLiM and my background for fluxbox at the same time. I
figured this could be accomplished by creating a folder for "current" theme (in /usr/share/slim/themes ) and a wallpaper in /usr/share/wallpapers
called "dummy", with my overlay file for fluxbox would set as background.
The script would copy a given background into the "current" and copy it's background to "dummy", then I could leave my slim.conf and overlay files
alone. There would also be a "current_theme_number" file to keep track of which theme I'm using at the moment:
Here's how I though I would implement the script:
Name the themes I want to rotate as variables like theme1 , theme2 , etc and give a variable for the total number of themes.
Delete all the theme files in "current" folder
Figure out the current theme by reading the "current_theme_number" file. (this is where the problem comes in)
I can get a number from the current_theme_number file and then do arithmetic mod no. of themes to get the number
for the next theme, but then I need to use this to get the name for next theme, which I cant do.
once I do that I copy the theme files from the next theme into the current folder
then I I change the number in the theme1 file.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Here's what I have of the script so far:
#!/bin/bash
#this is a script to rotate a number of backgrounds
#to be shared by the fluxbox desktop and SLiM login manager.
#it is the first meaningful script I've written. It may be
#freely distributed and used with the understanding that all
#use is at your own risk.
#
#Fred Drueck 2008
#first check you are running this script as super-user:
if [ $(id -u) != "0" ]; then
echo "You must be the superuser to run this script" >&2
exit 1
fi
echo "rotating themes"
#Define here all themes to be rotated and the total number of
#themes to rotate:
rotate_number=9
theme1=/usr/share/slim/themes/default
theme2=/usr/share/slim/themes/lake
theme3=/usr/share/slim/themes/lunar
theme4=/usr/share/slim/themes/flower2
theme5=/usr/share/slim/themes/the-light
theme6=/usr/share/slim/themes/mindlock
theme7=/usr/share/slim/themes/parallel-dimensions
theme8=/usr/share/slim/themes/wave
theme9=/usr/share/slim/themes/fingerprint
#figure out which theme is currently set, then name it as the variable
#"current theme number", otherwise quite w/ error message
if [ -f /usr/share/slim/themes/current/current_theme_number ]
then
echo "checking current theme"
cd /usr/share/slim/themes/current/
last_theme_number=$(cat /usr/share/slim/themes/current/current_theme_number)
echo $last_theme_number
else
echo "no theme is currently set!"
exit 1
fi
#set the new theme number
new_theme_number=$(($(($last_theme_number % $rotate_number))+1))
#select the new theme
declare new_theme=$"theme$new_theme_number"
echo $new_theme
#now clean out the "current" theme where I keep the current
#theme for slim
rm /usr/share/slim/themes/current/background*
rm /usr/share/slim/themes/current/panel*
rm /usr/share/slim/themes/current/slim.theme
#the wildcards are there since the themes use jpg and png files
I realize that I might not be able to write the script using my current approach, but I thought
I would ask before I go through and adapt a new approach. If I just keep a fixed number of
themes, then I can use "if ... then ... else" to rotate them, but this would make it alot of work to
add more themes to the script.
I might be able to do something similar to my approach by renaming the themes; folders
theme1 , theme2, etc, but this seems inelegant since everytime I install a new theme I want to
add to the rotation I will need to edit both the script and rename the theme ... plus then I will have
no idea which theme is which when I just look at the folder names.
If somebody has a more elegent approach they would like to suggest, then I would welcome the suggestion,
So anyway, thanks in advance for any help you can give me.
Last edited by pseudonomous (2008-07-04 01:55:43)
Offline
Hello every body,
What I'd like to now is: is there a way for you to concatenate the expansions of variables to get a string which is the same as the name of a
variable and then expand that string?like i have variables x = var and y = iable and then i want to make z = variable by using x and y.
I haven't read your entire post, but yes, you can do this:
x=var
y=iable
z=$x$y
Offline
yes and you get a string of characters:
"variable"
however, if you already defined "variable" to mean something, say:
/usr/share/slim/themes/wave
I don't get
/usr/share/slim/themes/wave
I just get "variable".
What I'd like to get is
/usr/share/slim/themes/wave
-thanks though
Last edited by pseudonomous (2008-07-04 01:41:04)
Offline
Oh bullocks, that's not quite what you want to do it seems. You want something like
x=var
y=1
var1=pie
eval theme=\$$x$y
echo $theme
-edit- perhaps a bit of explanation why this works. eval is the key here - eval takes all its parameters and executes them as if it were a bash statement, for example.
So, if you did "eval theme=hi" then $theme would be set to "hi". The example above is a bit more complex though - as arguments to eval I pass "theme=\$$x$y". I'll try to explain what bash does behind the scenes here.
First, bash looks through the line for variables it can substitute. It reaches the \$, and since it's escaped, it leaves that dollar sign alone. Then bash parses $x and replaces it with 'var', and $y which it replaces with '1'. Then it passes the entire argument to the 'eval' command.
So, when I write eval theme=\$$x$y, bash evaluates that line and does the above. Then eval gets theme=$var1 as its argument, and sets the variable appropriately.
I hope that was clear, it's a bit confusing.
Offline
Maybe I'm missing something, but I still get back (transfering to your langauge)
"echo $var1"
instead of
"pie"
edit:oops!
Last edited by pseudonomous (2008-07-04 01:49:35)
Offline
I missed that "eval" in your post.
Thanks a whole bunch!
Offline
Yeah, you can split it up if it makes it any clearer I suppose, too.
# Set up your variables
front=var
var1=pie
var2=cake
var3=pudding
# Iterate over all variables
for back in 1 2 3; do
varname=\$$front$back # Sets varname="$var1", varname="$var2", etc...
eval dessert=$varname # Bash substitutes $varname for its value, then passes the whole thing to eval
echo $dessert
done
Offline
By the way, a working version of this script can be found at:
http://bbs.archlinux.org/viewtopic.php?id=51398
It's still not elegant, but it does work.
Offline