LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-31-2003, 11:38 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
shell script general question


in the shell script:

for i in ./*; do
mplayer -ao alsa9 -vo xv -fs "${i}"
done

is there any way to randomise the "i" that is issued to mplayer? basically I want it to play all files in current dir in random order, mplayer has no randomiser. and if it did it closes after second file is played when issued several from a programming error.
 
Old 10-31-2003, 11:52 PM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
http://www.linuxgazette.com/issue55/tag/4.html

generates random numbers in bash. it looks like you can use that first loop to put all the files in an array and then generate the random number in the second loop (with the mplayer command) and mod it with the total files in that folder. kinda like how one may do it in c/c++. i think bash uses % for the modulus operator (just like in c/c++)
 
Old 10-31-2003, 11:55 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
shell scripting and very basic java apllet is all I know, and I am still learning.. I don't know how to do what you said :-P
 
Old 10-31-2003, 11:58 PM   #4
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
gimme a tick. it would all be shell scripting. you just need the random number generation. actually give me a while.
 
Old 11-01-2003, 12:06 AM   #5
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
hey thanx. sorry if it seems like I am trying to get someone to do it for me :-P

btw, are you a big shell scripter.. possibly with a little free time here and there (nothing big)?
if so I am looking for peoplke to help me develop my project (already quite far it is more than started) the url is at in my stuff under my msg.
 
Old 11-01-2003, 12:13 AM   #6
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
maybe to 2 shell scripts. one that generates a playlist and the other one that takes in the playlist and plays them in random order. the first on is easy. something like:

Code:
#!/bin/bash

# accept a passed argument for naming the playlist file
# ie.  ./gen_playlist $HOME/vids/playlist.txt
if [ -n "$1" ]
then
	# here use the directory path to your playlist
	ls $HOME/vids > "$1"
else
	# use generic path to playlist and playlist name if none given
	# this generates playlist in user's home directory
	ls $HOME/vids > $HOME/playlist.txt
fi

exit 0
lemme work on the second part.

edit: nevermind this. it doesn't make any sense to make 2 shell scripts.

Last edited by megaspaz; 11-01-2003 at 12:38 AM.
 
Old 11-01-2003, 03:20 AM   #7
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
well, this shows how much i suck. the random number generation isn't really being all that random. it plays randomly but always in the same random order. i'm too tired to keep trying to figure this out. but here's a script. it works, but it's not bullet proof. this format is ./script_name path_to_video_media - the argument should be a directory. this doesn't check if the files in the directory are actual media files or any other checks what-so-ever. this one uses xine, but i guess you can sub that xine command with the mplayer command you want to use.

Code:
#!/bin/bash

# seed random number generator
RANDOM=$$$(date '+%s')

#declare arrays to be used
declare -a f_name_array
declare -a played_already

# get the files in your video directory
if [ -n "$1" ]
then
	# user specified path to video media.
	ls "$1" > "$1/playlist.txt"

	# initialize generic counter
	i=0
	# populate array list
	while read f_name
	do
		f_name_array[$i]="$f_name"
		# inc counter
		i=$(($i + 1))
	done < "$1/playlist.txt"

	# get total count - array starts at 0 so total count is i + 1
	f_count=$(($i+1))

	# play the list in random order
	# initialize generic counter
	i=0
	while [ "$i" -lt "$f_count" ]
	do
		# get random index
		index=$[ ( $RANDOM % $f_count  ) ]
		# check if index has been played already

		has_played=0

		for pl_index in "${played_already[@]}"
		do
			if [ "$pl_index" -eq "$index" ]
			then
				has_played=1
				break
			fi
		done

		# determine action from playlist comparison
		if [ "$has_played" -eq 0 ]
		then
			# add the index to the played list
			played_already[$i]="$index"
			# play the file
			xine "$1/${f_name_array[$index]}"
			# inc. counter
			i=$(($i+1))
		fi
	done

	# debug crap
	#echo "playlist: "
	#for file in "${f_name_array[@]}"
	#do
	#	echo "$file"
	#done

	#echo
	#echo

	#echo "played already: "
	#for file in "${played_already[@]}"
	#do
	#	echo "$file"
	#done
else
	echo "Did not pass path to video media argument"
fi
rm -f "$1/playlist.txt"
exit 0
this should work for you, but i'm by no means a bash scripting expert or even intermediate in bash experience. i guess the usual disclaimer of use at your own risk, blah, blah, blah...

Last edited by megaspaz; 11-01-2003 at 03:23 AM.
 
Old 11-01-2003, 10:01 AM   #8
musrum
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 112

Rep: Reputation: 15
Just a note: you can get random numbers into a shell script by reading /dev/random. For example:

rnd=`dd if=/dev/random bs=2 count=1`

This will give you a value betwee 0 and 2^16.
 
Old 11-01-2003, 04:40 PM   #9
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
lol, thanx everyone, this will be a big help
 
Old 11-01-2003, 04:42 PM   #10
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
thanx again the script works great.
 
Old 11-18-2003, 10:56 PM   #11
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
I made my own perfect one!

#!/bin/sh

counter=1
digits=3
thenumbermin=1
thenumbermax=9

while [ ${digits} -ge 2 ]; do
let "thenumbermin=((${thenumbermin})*10)"
let "thenumbermax=(9+((${thenumbermax})*10))"
let "digits=(${digits} - 1)"
done

let "thenumbermax2=(thenumbermax - thenumbermin)"

# make all possible variables
nummax="${thenumbermax}"
nummin="${thenumbermin}"

while (("${nummin}" <= "${nummax}")) ; do
alist[${nummin}]="nothing"
let "nummin=((${nummin})+1)"
done



for i in "${1}"/*; do
looptry()
{
anumber=$((RANDOM%thenumbermax2+thenumbermin))

if [ "${alist[${anumber}]}" == "nothing" ]; then
alist[${anumber}]="${i}"
echo "${anumber} ${alist[${anumber}]}"
else
looptry
fi
}

looptry
done
echo out
thenumberminx="${thenumbermin}"

#while (("${thenumberminx}" <= "${thenumbermax}")) ; do
# tester=`echo "${alist[${thenumbermin}]}"`
# if [ ! "${tester}" == "nothing" ]; then
# echo "${alist[${thenumbermin}]}" >> ~/testlist.txt
# fi
# let "thenumberminx=((${thenumbermin})+1)"
#done

thenumbermin="${thenumbermin}"

while (("${thenumbermin}" <= "${thenumbermax}")) ; do
tester=`echo "${alist[${thenumbermin}]}"`
if [ ! "${tester}" == "nothing" ]; then
`mplayer -fs -ao alsa9 -vo xv "${alist[${thenumbermin}]}"`
clear
echo ""
echo "press control+c to quit"
sleep 1s
if [ "$exiting" == "q" ]; then
exit 0
fi
fi
let "thenumbermin=((${thenumbermin})+1)"
done
exit 0


so yeah... if anyone still cares.
 
  


Reply



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
Shell Script Question abdul_zu Programming 7 10-06-2005 03:33 AM
Shell Script Question. rvijay Linux - General 2 07-14-2005 06:41 PM
BASH question(or shell in general) jrmann1999 Linux - Software 4 05-11-2005 05:16 PM
Shell Script Question swinchen Programming 1 08-20-2004 02:09 PM
Shell script question. siaful Linux - Newbie 1 04-29-2004 05:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:20 AM.

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
Open Source Consulting | Domain Registration