Sign up here and you can log into the forum!

some scripting help please

General homebrew discussion area

some scripting help please   

Postby KAD » Thu Sep 22, 2011 10:09 pm

so my latest endevours, I've run into something I'm stuck on the script below, anybody that actually knows perl, I'm sure could help sort this fairly easy

Code: Select all
#!/usr/bin/perl
use strict;
use File::Spec::Functions;

open(CFGFILE,"/conf/config");
my @cfg_array = <CFGFILE>;
my $location = (grep(/RANDOM_MENUSHEETDIR='*'/,,@cfg_array));
close CFGFILE;

if (my @line = grep(/RANDOM_MENUSHEETDIR='(.*)'/,@cfg_array)) {
    ($location) = @line[0] =~ /RANDOM_MENUSHEETDIR='(.*)'/;
}

if (opendir(DIR,$location)) {
    system("logger -t random_menusheetdir 'Failed to access $location!'");
    die;
}

my @menusheetdirectories =(opendir(DIR,$location));
closedir(DIR);

my $numentries = @menusheetdirectories;         
if ($numentries == 0) {
    system("logger -t random_menusheetdir 'No valid wallpapers found in $location!'");
    die;
}

my $directory = $menusheetdirectories[int rand $numentries];

system("config_tool -c MENUSHEETDIR=$directory");
system("/osd/etc/init.d/S99menusheets restart");
system("logger -t random_menusheetdir 'Changed MENUSHEETDIR'");


The script runs and completes the config_tool -c
but it always runs it as config_tool -c MENUSHEETDIR=<empty/blank>
and yes directories do actually exist

here's what I get in /tmp/messages.txt
Dec 31 16:35:51 WDLXTVLIVE user.notice random_menusheetdir: Failed to access /tmp/mnt/E8FA-9D51/MENUSHEETS!
anybody see what I'm doing wrong, the rest of the project is already complete and in working order

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Re: some scripting help please   

Postby PaulF » Thu Sep 22, 2011 11:11 pm

I think you want $line[0] instead of @line[0]
User avatar
PaulF
Developer
 
Posts: 422
Joined: Sat May 08, 2010 8:34 pm
Location: Oregon

Re: some scripting help please   

Postby KAD » Fri Sep 23, 2011 12:07 am

it was worth a try, but system complains and fails to run with that syntax

the problem is I think here
Code: Select all
if (opendir(DIR,$location)) {
    system("logger -t random_menusheetdir 'Failed to access $location!'");
    die;
}


if I have this check it die's due to unable to open the $location

if I omit this part, it runs through and config_tool -c blahblah=<empty>
because it's not reading the $location

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Re: some scripting help please   

Postby PaulF » Fri Sep 23, 2011 12:31 am

OOPS it should be $location = $line[0] etc. $line[0] is a scalar and ($location) is an array.
User avatar
PaulF
Developer
 
Posts: 422
Joined: Sat May 08, 2010 8:34 pm
Location: Oregon

Re: some scripting help please   

Postby KAD » Fri Sep 23, 2011 7:31 am

if I'm understand correct, you suggest

Code: Select all
if (my @line = grep(/RANDOM_MENUSHEETDIR='(.*)'/,@cfg_array)) {
    ($location) = $line[0] =~ /RANDOM_MENUSHEETDIR='(.*)'/;


I tried this last night, when run from cmd line, system complains about syntax errors, I guess later when i'm home, I should post the full error message, unless somebody has another idea, but it stated this line
Code: Select all
($location) = $line[0] =~ /RANDOM_MENUSHEETDIR='(.*)'/;
as having a problem with $line needing to be defined or something like that

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Re: some scripting help please   

Postby PaulF » Fri Sep 23, 2011 10:41 am

KAD wrote:if I'm understand correct, you suggest

Code: Select all
if (my @line = grep(/RANDOM_MENUSHEETDIR='(.*)'/,@cfg_array)) {
    ($location) = $line[0] =~ /RANDOM_MENUSHEETDIR='(.*)'/;


I tried this last night, when run from cmd line, system complains about syntax errors, I guess later when i'm home, I should post the full error message, unless somebody has another idea, but it stated this line
Code: Select all
($location) = $line[0] =~ /RANDOM_MENUSHEETDIR='(.*)'/;
as having a problem with $line needing to be defined or something like that

KAD
OK, try:
Code: Select all
if (my @line = grep(/RANDOM_MENUSHEETDIR='(.*)'/,@cfg_array)) {
    $location = $line[0];
}
User avatar
PaulF
Developer
 
Posts: 422
Joined: Sat May 08, 2010 8:34 pm
Location: Oregon

Re: some scripting help please   

Postby recliq » Fri Sep 23, 2011 10:50 am

I had some problems getting your script at first but I think i understand what you are trying to do is this:
- read RANDOM_MENUSHEETDIR from /conf/config (eg. /tmp/msheets)
- read the subdirs of that dir (eg /tmp/msheets/a, /tmp/msheets/b, ...)
- set MENUSHEETDIR to a random subdir

here's what I would do in bash (much simpler for this purpose ;))
Code: Select all
#!/usr/bin/bash
eval `grep 'RANDOM_MENUSHEETDIR=' /conf/config`
[ ! -d "$RANDOM_MENUSHEETDIR" ] && logger -t random_menusheetdir 'No such dir: $RANDOM_MENUSHEETDIR' && exit
DIRS=($(find "$RANDOM_MENUSHEETDIR" -maxdepth 1 -type d))
[ ${#DIRS[@]} -eq 0 ] && logger -t random_menusheetdir 'No menusheet dirs found' && exit
RND=$RANDOM
let "RND %= ${#DIRS[@]}"
config_tool -c MENUSHEETDIR=${DIRS[RND]}
/osd/etc/init.d/S99menusheets restart
logger -t random_menusheetdir 'Changed MENUSHEETDIR'

:ugeek:


well, because it's you KAD and you've learned so much already, here's a commented version which might spare you some headaches 8-)
Code: Select all
#!/usr/bin/bash

# get RANDOM_MENUSHEETDIR line from config, the eval make it a shell variable ($RANDOM_MENUSHEETDIR)
eval `grep 'RANDOM_MENUSHEETDIR=' /conf/config`

# if there is no such dir, log it and exit
[ ! -d "$RANDOM_MENUSHEETDIR" ] && logger -t random_menusheetdir 'No such dir: $RANDOM_MENUSHEETDIR' && exit

# load all subdirs of dir into array $DIRS
DIRS=($(find "$RANDOM_MENUSHEETDIR" -maxdepth 1 -type d))

# if no dirs are found, log it and exit
[ ${#DIRS[@]} -eq 0 ] && logger -t random_menusheetdir 'No menusheet dirs found' && exit

# generate random number
RND=$RANDOM

# make random number in range of dir count
let "RND %= ${#DIRS[@]}"

# save config
config_tool -c MENUSHEETDIR=${DIRS[RND]}

# restart init script, log it and done.
/osd/etc/init.d/S99menusheets restart
logger -t random_menusheetdir 'Changed MENUSHEETDIR'
­WDLXTV Project Maintainer
-:] If you like my contributions feel free to donate for a beer or a new flash drive. ...and always remember: RTFM! (README, FAQ, WIKI) [:-
User avatar
recliq
WDLXTV Team
 
Posts: 5035
Joined: Thu Apr 15, 2010 8:09 am
Location: Kiel, Germany

Re: some scripting help please   

Postby KAD » Fri Sep 23, 2011 11:16 am

wow, this is really great, I was using perl, because that's what Merlin used for randomwallpaper.app.bin, I didn't know that bash could do random

I'm looking forward to trying this when I get home

just in case I don't say it often, I appreciate all the help from every here, particularly the devs,
being a noob to programing/scripting
I've really learned a lot

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Re: some scripting help please   

Postby KAD » Fri Sep 23, 2011 6:57 pm

@PaulF, thanks but that also did not work

so on to recliq's bash script, I had to fix some syntax errors to get it to run

here's what I currently have
Code: Select all
    #!/usr/bin/bash
    eval `grep 'RANDOM_MENUSHEETDIR=' /conf/config`
    [ ! -d "$RANDOM_MENUSHEETDIR" ] && logger -t random_menusheetdir 'No such dir: $RANDOM_MENUSHEETDIR' && exit
    DIRS=$(find "$RANDOM_MENUSHEETDIR" -maxdepth 1 -type d)
    [ ${#DIRS[@]} -eq 0 ] && logger -t random_menusheetdir 'No menusheet dirs found' && exit
    RND=$RANDOM
    let "RND%=${#DIRS[@]}"
    config_tool -c MENUSHEETDIR=${DIRS}[$RND]
    /osd/etc/init.d/S99menusheets restart
    logger -t random_menusheetdir 'Changed MENUSHEETDIR'


and the result is
Code: Select all
config saved val:key => (MENUSHEETDIR,/tmp/mnt/E8FA-9D51/MENUSHEETS)


anybody know why it's omitting the random directory??
example of correct path would be
Code: Select all
/tmp/mnt/E8FA-9D51/MENUSHEETS/Windows-Wallpapers

1 thought, I ommited $DIRS in one test and $RND produced the number 245, is that correct? there are only 5 DIRS to choose from

thanks again

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Re: some scripting help please   

Postby KAD » Fri Sep 23, 2011 11:33 pm

so quite a bit more digging

Code: Select all
${#DIRS[@]}

this statement is not returning the correct value
it returns 274, which happens to be the exact number of files and directories combined together (including sub directories and files in sub directories)

so if I start making some changes
Code: Select all
DIRS=$(find "$RANDOM_MENUSHEETDIR" -maxdepth 0 -type d)
    [ ${#DIRS[@]}

produces 29
with this low of a number, $RND eventually selected 4, which you'd think would produce a valid config_tool because there are 5 directories to choose from
but instead it passes the number 4 instead of the name of directory 4 from the array
output looks like this
Code: Select all
config saved val:key => (MENUSHEETDIR,/tmp/mnt/E8FA-9D51/MENUSHEETS/[4])


further testing
Code: Select all
DIRS=$(find "$RANDOM_MENUSHEETDIR" -mindepth 1 -type d)
    [ ${#DIRS[@]}

produces 549
but it also output this
Code: Select all
config saved val:key => (MENUSHEETDIR,/tmp/mnt/E8FA-9D51/MENUSHEETS/Linux-Wallpapers)
which is a valid working directory,
but it's not random, running the script over and over the selected directory never changes

off to bed now

KAD
If you like my work please consider a Donation. Donate
Please read the appropriate documentation before posting questions! READ ME FAQ WIKI
User avatar
KAD
Global Moderator
 
Posts: 4021
Joined: Mon Apr 12, 2010 4:59 pm
Location: Seattle, WA USA

Next

Return to WDTV G2 & WDTV Live homebrew discussion

Who is online

Users browsing this forum: No registered users and 1 guest