Creating own bootline command variable


Forum: Other Help Topics
Topic: Creating own bootline command variable
started by: beakmyn

Posted by beakmyn on Feb. 16 2007,16:07
I've been busy remastering DSL for my picture frame and I got to thinking, "What if the picture directory was a variable set at boot?"

I've got a script (not mine) that will take user input for a directory based on pre-defined initial directory

Code Sample
#!/bin/sh
#
# Script to run Digital Picture Frame using Feh
# drware@thewares.net
#

# Set display so that the script will effect
# the screen on the frame
export DISPLAY=:0

# Stop the currently running Slide show
/mnt/hda3/frame/kill.sh feh

clear
echo ""
echo -n "Enter the number of seconds for photo rotation "
echo -n "and press [ENTER]: "
read var_seconds
echo ""
echo "Available options for the directory are:"
ls /mnt/hda3/Pictures
echo ""
echo -n "Enter the directory of photos to display and "
echo -n "press [ENTER]: "
read var_dir
echo ""

/usr/bin/feh -qzrZF -D $var_seconds /mnt/hda3/Pictures/$var_dir &

exit 0


So I was looking at /etc/init.d/knoppix-autoconfig and I thought maybe it would be cool to tack on my own check for PICTURES=var_dir

which would write var_dir to a text file or some sort of global memory variable. I could then modify my scripts to query this variable.

Looking at the autoconfig I believe I can figure out the "getting the varible part" but I'm unsure on the "what to do with it" part, I.E. putting it into a global variable that another script can query and use.


Tips? Pointers?

Posted by mikshaw on Feb. 16 2007,17:43
The boot line is stored in /proc/cmdline, so you can get the string like this:
Code Sample
CMDLINE=`cat /proc/cmdline`

From there you can use the $CMDLINE variable along with some of DSL's built-in functions to find a particular boot option. That variable is used extensively in dsl-functions.
This command will load the functions into your current script:
Code Sample
source /etc/init.d/dsl-functions

As an example, you could send "pictures=/path/to/directory" on the bootline, and then in your script do something like this:
Code Sample
source /etc/init.d/dsl-functions
CMDLINE=`cat /proc/cmdline`
PICTURES=`getbootparam pictures`

Posted by beakmyn on Feb. 16 2007,18:29
That makes sense. It looks like the "bulletproof" way would be something like:

(pulling code from a couple different files)

Code Sample

#!/bin/sh
#Sample script to query boot command line and store a
#parameter into a value to use later
#Here we just echo the path and list it's contents
#
#

. /etc/init.d/dsl-functions
CMDLINE="$(cat /proc/cmdline)"
PICTURES="$(getbootparam pictures 2>/dev/null)"
[ -n "$PICTURES" ] || PICTURES="/home/dsl"

echo "$PICTURES"
ls "$PICTURES"



Some Notes: (in case someone else reads this)

One could reasonably assume that CMDLINE would already be populated but it's best to populate it, just in case. It's also good practice to know what type ($ = string) your variable is before you act on it.

This gives either the passed boot command line variable or defaults user dsl home directory if no parameter is passed.


Thanks.

Posted by mikshaw on Feb. 16 2007,19:17
Good thinking there.

If you always need PICTURES to be a directory, you could use "-d" instead of "-n" for your test. This allows you to drop the need to redirect stderr to /dev/null, since the output of getbootparam will be used only if it's an existing directory. The "-n" test simply checks to see if the variable's length is non-zero.

Posted by beakmyn on Feb. 16 2007,20:02
Code Sample

#!/bin/sh
#Sample script to query boot command line and store a
#parameter into a value to use later
#Here we just echo the path and list it's contents
#
#

. /etc/init.d/dsl-functions
CMDLINE="$(cat /proc/cmdline)"
PICTURES="$(getbootparam pictures"
[ -d "$PICTURES" ] || PICTURES="/home/dsl"

echo "$PICTURES"
ls "$PICTURES"


I understand that ">/dev/null" is the redirect. But what's the 2 for?

Posted by ^thehatsrule^ on Feb. 16 2007,22:30
2 is used for stderr, without anything defaults to 1 (stdout).

You can look up linux file descriptors if you want more info.

Posted by mikshaw on Feb. 17 2007,03:45
you'll probably want to fix the PICTURES="$(getbootparam pictures" (add a closing ")" to it) if you haven't already noticed that.
Posted by beakmyn on Feb. 17 2007,11:58
Yep, got it fixed. Thanks for the "file descriptors" without out know what it was it's kinda difficult to search google for 2> to figure out what it is.
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.