PDA

View Full Version : Help with Scripting: Output my script to a file? Plus other questions.



em3raldxiii
October 15th, 2011, 05:12 AM
Hey all, not sure where to put this, so I figure it was safest to put it here. I've searched around the forums, but haven't been successful in finding a solution. I have very limited experience with writing scripts, so anything you can suggest would help

I have a script that someone else wrote for generating the XML file for a Gnome wallpaper slideshow, but it just outputs to the terminal. I can certainly do this:


./myScript <my parameters> > myFile.xml

but this is an inelegant solution, in my humble opinion. I would like to just have this file output directly to the appropriate XML file automatically. It doesn't even need to prompt for a filename (although that would be sweet).

I have a feeling I would need to have it all output to a variable, and then do a cat $variable > myFile.xml or something. I have attached the existing script. For my new script, I will also add a note about the origin of the code.

Bonus Items: Now let's say I want to make this a nice portable package for folx that are CLI challenged. First up: Can a script add it's output file directly to the wallpaper database (and automatically select it sorta like right-clicking an image in a browser and choosing to make it your wallpaper instantly). Second, can a script like this be made into a binary that is double-clickable in a folder as opposed to executing it from a CLI?

koenn
October 15th, 2011, 01:42 PM
yoi can wrap the entire script in () en redirect that to a file : whatever output is generated by a command inside those backets (parenteses ? whatever) will be redirected.

like so :




#!/bin/bash

# params
outfile=~/testoutfile

# override default outtfile
echo -n "output to: "; read dummy
[ "$dummy" != "" ] && outfile="$dummy"

#DEBUG
echo "output to $outfile"; echo;

## main stuf goes here, wrapped in () and redirected to file

( #opening bracket - don't remove

echo A
echo B
echo C

echo
echo

seq 1 10 |while read x; do
echo $x
done

echo
echo D
echo E

# closing bracket and dump everything in outfile
)>$outfile

em3raldxiii
October 16th, 2011, 02:34 AM
Okay, that's a great step in the right direction. I'll work on putting that into the script, and I don't think it should be terribly difficult.

Now, let's say I am successful in generating a correct .xml file for that. Since I know that the default wallpaper directory is /usr/share/backgrounds, I should be able to make the script copy itself to a subdirectory within that directory, complete with all of the enclosed images. Now, I will have to have the script query that location to ensure a directory doesn't already exist with that name, and if so query the user for a new name (or automatically generate one - except that might lead to a very cluttered directory). The only question remains is whether or not that .xml file will be automatically included (and selected) for the desktop background.

*UPDATE*
Well, first off, just copying stuff into the /usr/share/backgrounds directory obviously has no immediate effect. This is because gnome uses this configuration file /usr/share/gnome-background-properties/ubuntu-wallpapers.xml to show the list of available backgrounds. So once I have generated the correct xml file, we're going to want to ADD the call to our xml file to that configuration file. I might be inclined to make the "current user" the owner of the xml file and turn off read priveleges for all other users. That way each user can have their own list of backgrounds. I realize this is getting a little complex, but I can see this working quite elegantly when I am done.

*UPDATE 2*
<grumble> well the /usr/share/gnome-background-properties/ubuntu-wallpapers.xml doesn't contain any of my recently added files that *are* available in the background image dialogue, so obviously they are stored somewhere else (that doesn't require root privileges to edit obviously). If you know where this is, please let me know. I will keep looking.

*UPDATE 3*
Eureka! Okay, I have found two files that I will need to edit with my script, and it turns out I don't have to worry about permissions and whatnot because they are configuration files under the user's home folder. These files are: ~/.gnome2/backgrounds.xml which is where I would add a file to the list of available backgrounds, and ~/.gconf/desktop/gnome/background/%gconf.xml which I think will let us change the current background. Now to figure out the scripting commands to insert/replace a line or two in these files.

em3raldxiii
October 16th, 2011, 07:01 AM
So I have a working script, and I feel it's an improvement over the original. I'd love to see what anyone else might have to offer in terms of either cleaning it up, or whatever. Here it is:



#!/bin/bash

if [ $# -lt 4 ]
then
echo
echo
echo "You are missing some parameters."
echo
echo "usage: $0 <hold duration> <fade duration> file1 file2 ..."
echo "example: $0 60 5.0 /path/to/dir/*.jpg /path/to/dir/*.png"
echo
echo
exit 1
fi

echo -n "Give a name to your new wallpaper slideshow: "; read show

#params
hold=$1
fade=$2
first=$3
outfile=backgrounds.xml
parent=WallpaperSlideshows

# override default outtfile
# Uncomment the following lines to query the user for a filename.
#echo -n "output to: "; read dummy
#[ "$dummy" != "" ] && outfile="$dummy"

#DEBUG
echo
echo "Executing ..."
echo "Output to $outfile"

#remove hold parameter
shift
#remove fade parameter
shift

( #all of the contents in parentheses will be output to the outfile.
echo "<background>"
echo " <starttime>"
echo " <year>2009</year>"
echo " <month>08</month>"
echo " <day>04</day>"
echo " <hour>00</hour>"
echo " <minute>00</minute>"
echo " <second>00</second>"
echo " </starttime>"
echo " <!-- This animation will start at midnight. -->"

while [ $# -gt 0 ]
do
echo " <static>"
echo " <duration>$hold</duration>"
echo " <file>$HOME/$parent/$show/$1</file>"
echo " </static>"
echo " <transition>"
echo " <duration>$fade</duration>"
echo " <from>$HOME/$parent/$show/$1</from>"
if [ $# -gt 1 ]
then
echo " <to>$HOME/$parent/$show/$2</to>"
else
echo " <to>$HOME/$parent/$show/$first</to>"
fi
echo " </transition>"
shift
done
echo "</background>"
#Closing parenthesis marks the end of material to be output to the outfile.
)>$outfile

echo "Please Note: This script currently only handles files with the .jpg extension"
echo "Copying images to ~/"$parent"/"$show
mkdir -p ~/$parent/$show
cp *.* ~/$parent/$show
echo "Done."
echo "Setting the new slideshow as current wallpaper ..."

gconftool -t string -s /desktop/gnome/background/picture_filename $HOME/$parent/$show/$outfile
gconftool -t string -s /desktop/gnome/background/picture_options scaled
echo "Done."


Now, what about making this into a file that is executable from a double-click?

Cheers!