LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-27-2008, 08:00 AM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Is this feasible


Hello all,

I am considering an attempt to make some kind of bash script which can fetch a random entry from a playlist, invoke xine to play the entry from the play list and set a volume level accordingly.

To do this, I would need to make a list of entries which would have a parameter, separated by a comma at the begining of each row, which represents a playback volume. The list would need a row count at the beginning of the file too, perhaps.

That of course is the easy bit.

I have thought this through a little bit and I think I would need a script to perform the following tasks

- Script to read first row of list which will set a variable representing the row count.

- Script to choose a random number between 1 and the "row count" found in the list.

- Script to use the random number to find a row number in the list and fetch the data from that row

- Script then sets a variable representing the volume

- Script sets a variable representing the path to a file to be played

- Script invokes xine by way of xine -f -g -S --session 0,volume=<$VOL>,mrl=<$PATH>


Not being all that familiar with BASH scripts, I was wondering if this idea was a little too ambitious or not. Can this actually be achieved with a BASH script I guess is what I am wondering?


Regards,
Greenie
 
Old 07-27-2008, 09:00 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Definately not hard. Might think about chars encountered in paths and filenames and the separator you use.
 
Old 07-27-2008, 11:20 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
That's totally doable. You only need one script, not the 6 you list. Random numbers, or at least random enough for your purposes, are trivial.
Code:
$ let foo=$RANDOM
if you want a number between 0 and 99
Code:
$ let foo=$RANDOM%100
Look at the wc command for getting a line count of a file. cut, head and tail will probably all be useful. There are lots of useful little commands like those that can all be stuck together to achieve all kinds of things.
 
Old 08-02-2008, 09:12 AM   #4
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Why does:

Code:
echo $[($RANDOM % 10) + 1]
produce:

random_script.sh: 5: Syntax error: "(" unexpected

???

I got the example from:

http://linuxtechsupport.blogspot.com...om-number.html
 
Old 08-02-2008, 09:23 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
This works:

echo $(($(($RANDOM % 10)) + 1))

I'm sure there's a more elegant notation somewhere....
 
Old 08-02-2008, 09:26 AM   #6
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by pixellany View Post
This works:

echo $(($(($RANDOM % 10)) + 1))

I'm sure there's a more elegant notation somewhere....

I get:

Code:
random_script.sh: 5: arithmetic expression: syntax error: "( % 10) + 1"
 
Old 08-02-2008, 09:46 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Works here....
Try using cut and paste to avoid typos?

Also, test things like this in pieces. For example, does this work?

echo $(($((4567 % 10)) + 1))

This?

echo $((4567 % 10))
 
Old 08-02-2008, 10:00 AM   #8
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by pixellany View Post
Works here....
Try using cut and paste to avoid typos?

Also, test things like this in pieces. For example, does this work?

echo $(($((4567 % 10)) + 1))

This?

echo $((4567 % 10))

That works, but if I replace the 4567 with $RANDOM, I then get:

random_script.sh: 5: arithmetic expression: syntax error: "( % 10) + 1"


BTW, I always copy+paste, re-typing is too slow, easier to copy+paste.
 
Old 08-02-2008, 10:12 AM   #9
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
If I do:

Code:
echo $RANDOM
Actually the above works.

But this doesn't:

Code:
number=$RANDOM
  echo $number
above code gives me a blank line.

Last edited by greengrocer; 08-02-2008 at 10:37 AM.
 
Old 08-02-2008, 11:48 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
What shell are you using?

Code:
mike@continuity:~$ echo $0
/bin/bash
mike@continuity:~$ echo $RANDOM
32275
mike@continuity:~$ number=$RANDOM
mike@continuity:~$ echo $number
13029
mike@continuity:~$
 
Old 08-02-2008, 12:23 PM   #11
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
user@edgy:~$ echo $0
bash
user@edgy:~$
 
Old 08-02-2008, 12:27 PM   #12
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Hmmmm. Try

Code:
mike@continuity:~$ bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
 
Old 08-02-2008, 12:59 PM   #13
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
This works:

echo $(($(($RANDOM % 10)) + 1))

I'm sure there's a more elegant notation somewhere....
There certainly is, no need for so much nesting:

Code:
echo $(( (RANDOM % 10) + 1))
No need for a $ to dereference RANDOM either, it is implied inside $(( )).
 
Old 08-02-2008, 01:12 PM   #14
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by greengrocer View Post
Code:
random_script.sh: 5: arithmetic expression: syntax error: "( % 10) + 1"
I can reproduce that using 'jsh' which behaves as strict as the "true" Bourne Shell.


Moving ahead of schedule slightly here's a quick idea for creating a dirlisting with sequence number, volume, filename:
Code:
createListing() { [ -d "$1" ] || exit 1; SEP=' '; N=0; VOL=90; find "$1" -type f -iname \*.avi \
-printf "\"%p\"\n"|while read LINE; do ((N++)); echo "${N}|${VOL}|${LINE}"; done|while \
read LINE; do ARRAY=(${LINE//|/  }); echo -en "${ARRAY[0]}${SEP}${ARRAY[1]}${SEP}"; 
unset ARRAY[0] ARRAY[1]; echo "${ARRAY[*]}${SEP}"; done; }
 
Old 08-02-2008, 01:22 PM   #15
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
As arizonagroovejet mentioned earlier, there's no point have a row count, just use wc to count the number of lines.

Do you need a seperate volume level for each file? Won't one do? If the files have different volumes, you could normalize them.

If you do need seperate volumes, you could read the file into two arrays, one for filenames one for volumes, then pick one to play.

E.g.

Code:
#/bin/sh

volumes=()
files=()

# Assume whitespace seperator. Uncomment if fields are comma seperated.
# In that case, you might want to save and restore IFS.
#IFS=,

while read filename volume; do
    files=("${files[@]}" "$filename")
    volumes=(${volumes[*]} $volume)
done < "$1"

len=${#files[*]}

while true; do # use whatever condition you want
    n=$(( RANDOM % len ))
    xine -f -g -S --session 0,volume=${volumes[$n]},mrl=${files[$n]}
done
Of course, that will just loop indefinitely until you break it.

If you want a limited number of plays, how do want to do it? Just play N files or do you want to play them all? I.e. if you play files at random, it's obviously unlikely you'd play them all without repeating any. If you want to make sure the're all played, you'll have to keep track of what's been played. You could remove a file from the list once it's played, then when the list is empty, it's finished. E.g.

Code:
while [[ -n "$files" ]]; do
    n=$(( RANDOM % len ))
    xine -f -g -S --session 0,volume=${volumes[$n]},mrl="${files[$n]}"
    unset files[$n]
    unset volumes[$n]
done

Last edited by smoked kipper; 08-02-2008 at 01:23 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any project feasible for newbie? shogun1234 Linux - Newbie 3 07-28-2007 10:21 AM
Synchronizing a PocketPC with Linux - Is it feasible? winterhunter Linux - Software 1 03-29-2007 09:41 AM
Would this be feasible misterjingo Linux - Networking 3 09-26-2005 03:04 AM
Is this feasible? Backdraft Linux - Security 1 06-14-2005 03:32 PM
Possible Server configuration help. Is this feasible? esears Linux - General 2 04-25-2004 02:39 PM


All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration