Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash scripting problem
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
sammy2ooo
Apprentice
Apprentice


Joined: 26 May 2004
Posts: 225

PostPosted: Sun Dec 12, 2004 6:52 pm    Post subject: bash scripting problem Reply with quote

hi

i wrote a tiny script for randomize my xmms playlist
Code:

#/bin/bash

indexToPlay=$(expr $RANDOM % $(ls /home/sammy/MP3z | wc -l));

albumToPlay=$(ls /home/sammy/MP3z | nl - | grep -e $indexToPlay -m 1 | gawk -F $indexToPlay '{ print $2 }' | cut -f 2);

#for debugging
if [ -d /home/sammy/MP3z/"$albumToPlay" ]; then
        echo "PASST       $albumToPlay";
else
        echo "UPS         $albumToPlay";
fi

xmms /home/sammy/MP3z/"$albumToPlay"


Code:
sammy@uranos sammy $ ls -l /home/sammy/MP3z
...
drwxr-xr-x   2 sammy users 4.0K May  6  2004 Bajwa And Dale Anderson - Axis One
drwxr-xr-x   2 sammy users 4.0K May  6  2004 Balance 005 - Mixed By James Holden
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Barry White - All Time Greatest Hits
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Basement Jaxx - Remedy
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Basement Jaxx - Rooty
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Beatles - Anthology - CD1
drwxr-xr-x   2 sammy games 4.0K Nov  9 20:24 Beatsteaks - Smack Smash
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bee Gees - Best Of
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Beenie Man - Art And Life
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Beenie Man - Youth Quake
drwxr-xr-x   2 sammy users 4.0K Nov  9  2003 Behind The Eye - EyeQ Compilation Vol.1
drwxr-xr-x   2 sammy users 4.0K Apr  7  2004 Big Beach Boutique II
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Debut
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Homogenic
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Post
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Selmasongs
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Telegram
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bjoerk - Vespertine
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Black Sabbath - Black Sabbath (remaster)
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Black Sabbath - Paranoid
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Blank & Jones - In The Mix
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bloodhound Gang - Hooray for Boobies
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bloodhoundgang - One Fierce Beer Coaster
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Blumentopf - Blumentopf
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Blumentopf - Eins A
drwxr-xr-x   2 sammy users 4.0K May  5  2004 Blumentopf - Gern Geschehen
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Blumentopf - Grosses Kino
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bob Marley
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bob Marley - Burnin'
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bob Marley - Natural Mystic
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Body Count - Born Dead
drwxr-xr-x   2 sammy users 4.0K Oct 28  2003 Bomfunk MCs - In Stereo
drwxr-xr-x   4 sammy users 4.0K Oct 28  2003 Bossa Tres Jazz - When Japan Meets Europe
drwxr-xr-x   2 sammy users 4.0K May  6  2004 Buena Vista Social Club
drwxr-xr-x   2 sammy games 4.0K Nov  2 06:43 Bush - Deconstructed
...


the problems are:
- sometims the script isn't able to put the whole string into $albumToPlay. cut -f 2 seems to produce this error
- I don't want to create a temp file.
- it would be nicer if i could go through the listing line per line
- is there a tool something like: print_line ??

any ides on this??


greats
_________________
- Linux is sexy -
guru@linux:~> who | egrep -i 'blonde|black|brown' | talk && cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;
Back to top
View user's profile Send private message
ciaranm
Retired Dev
Retired Dev


Joined: 19 Jul 2003
Posts: 1719
Location: In Hiding

PostPosted: Sun Dec 12, 2004 11:39 pm    Post subject: Reply with quote

Might as well do it in perl / ruby / whatever if you're going to call external apps. I have a shuffle.pl which shuffles arbitrary lines in a text file:
Code:

#!/usr/bin/perl

my @lines = <>;

for (my $i = 0 ; $i < @lines ; ++$i) {
    my $x = int rand @lines;
    ($lines[$x], $lines[$i]) = ($lines[$i], $lines[$x]);
}

print @lines;

You can then do find /wherever | shuffle.pl | xargs xmms for example.
Back to top
View user's profile Send private message
vvlly
n00b
n00b


Joined: 23 Sep 2002
Posts: 52

PostPosted: Sun Dec 12, 2004 11:41 pm    Post subject: Reply with quote

I think the gawk part is giving you the problems. I think just
Code:

albumToPlay=$(ls /home/sammy/MP3z | nl - | grep -e $indexToPlay -m 1 | cut -f 2);

should work. You might also want to try using find with -type d to only list directories.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum