Hey /g/.
I have this shell script that takes music from /playlist, then plays it, and then moves it into /played.
It is working fine but how do I make it so that the loop ends when /playlist is empty?#!/bin/sh
echo "Starting to play music"
cd ./playlist
pwd
while [ ]
do
SONG=$(ls -r | tail -n 1)
echo $SONG
sleep 3s
mplayer /var/www/html/vault/drive1/audio/playlist/$SONG
echo "Moving" $SONG
mv $SONG /var/www/html/vault/drive1/audio/played/
echo "----Next----"
done
echo "Finished, out of fresh music."
forgive me i'm not familiar with the syntax and shit for shellscript, but couldn't you just do something like count the files in your folder and if it is == 0 do something
check out http://stackoverflow.com/questions/13727069/count-files-and-directories-using-shell-script
>>51665834for $f in "path/to/files/*" do
# bla bla
done
Or something like this. Use google m8.
>>51665834
Check for the size of the folder and under a certain threshold quite the loop?
Why this doesn't work?#!/bin/sh
echo "Starting to play music"
cd ./playlist
pwd
LIST=$(ls | wc -l)
while ($LIST > 0)
do
SONG=$(ls -r | tail -n 1)
echo $SONG
echo $LIST
sleep 3s
mplayer -volume 2 /var/www/html/vault/drive1/audio/playlist/$SONG
echo "Moving" $SONG
mv $SONG /var/www/html/vault/drive1/audio/played/
$LIST --1
echo "----Next----"
done
echo "Finished, out of fresh music."
>>51666352
Because $List isn't updated?
Why not just do something likefor $f in "files/*" do
play $f
done
mv "files/*" "some/other/place"
Isn't it paramount that you move each file individually after you've played it?
>>51666391
Disregard my first comment. I was to stupid to read what you wrote.
Can you clarify what you mean by "not working" is it looping forever, is it crashing?
>>51666413Starting to play music
/var/www/html/vault/drive1/audio/playlist
./play.sh: 8: ./play.sh: 2: not found
Finished, out of fresh music.
This is the output and then it creates file called "0" in the /playlist directory.
>>51666352
>$LIST --1
That's not a thing in sh
>>51666523
Neither iswhile ($LIST > 0)
>>51666452
Which lines is 2 and 8?
You could do this:for f in *;
do
#play $f
#move $f
done
Tested it and it works afaik.
>>51666555
>Which lines is 2 and 8?
That's not what it's saying.
>>51666452
I can't figure why its making a file yet but
>/var/www/html/vault/drive1/audio/playlist
if empty it should skip the while but isn't, no? something broke. maybe check if empty of a filetype not just empty.
>>51666584
>I can't figure why its making a file yet$LIST > 0writes the contents of $LIST to the file "0"
Seriously OP learn shell scripting before you try this shit. Half the stuff you're trying to do isn't even valid and you're just incorrectly guessing at syntax
>>51666576
What does it mean then?
It has nothing to do with what
>>51666523
and
>>51666544
pointed out then?
>>51666523
How do I make it to be 1 less after every loop?
>>51666544
Okay, I'm stupid.
>>51666555
I don't know how I should sort that out?
>>51666584
When /playlist is empty it just loops the echos and says that it can't play anything.
/sp/ get
#!/bin/bash
PLAYLIST=/path/to/playlist
PLAYED=/path/to/played
printf "Starting to play music. \n"
for i in "$PLAYLIST" ; do
printf "\n Now playing "$i" \n".
mplayer -volume 2 $i
printf "\n Moving "$i" to the played directory \n"
mv $i "$PLAYED"
sleep 3s
done
printf "Done."
Why not like this?
>>51666636
>What does it mean then?LIST=$(ls | wc -l)is resulting in "8"while ($LIST > 0)is trying to execute "8" and write its output to the file "0"
"8" is not a command, so you get a not found error.
Basically this entire script is retarded
>>51666669
Yeah, I'll try this.
Thanks for writing it down so I can actually think how it works. <3
>>51666669
Just looking over this, I imagine it might fail when file names have spaces in them.
>>51666724
Enclose the variable in quotesecho "$var"
>>51666709
For loops are really easy and useful, it's essentially saying "For every file in this directory, run these commands on it one after the other."
>>51666724
It shouldn't, all the variables should be encased in quotation marks. I understand where you're coming from though, I had problems with my ffmpeg batch converter until I quotation marked the fuck out of it.
>>51666669
>>51666724
I realized I missed some quotation marks, mplayer -volume 2 $i will probably need to be mplayer -volume 2 "$i"
>>51666775
>It shouldn't, all the variables should be encased in quotation marks
$i isn't
>>51666828
Yeah, I'm just now noticing the ones I missed. Sorry about that.
>>51666669
>>51666809
Uhm, almost...
Starting to play music.
Now playing /var/www/html/vault/drive1/audio/playlist
.MPlayer2 2.0-728-g2c378c7-4+b1 (C) 2000-2012 MPlayer Team
Cannot open file '/root/.mplayer/input.conf': No such file or directory
Failed to open /root/.mplayer/input.conf.
Cannot open file '/etc/mplayer/input.conf': No such file or directory
Failed to open /etc/mplayer/input.conf.
Playing /var/www/html/vault/drive1/audio/playlist.
Cannot open file '/var/www/html/vault/drive1/audio/playlist': No such file or directory
Failed to open /var/www/html/vault/drive1/audio/playlist.
Exiting... (End of file)
Moving /var/www/html/vault/drive1/audio/playlist to the played directory
mv: cannot stat ‘/var/www/html/vault/drive1/audio/playlist’: No such file or directory
Done.#
>>51666773
Or use bash parameter expansion: ${var}
This should be done out of habit
>>51666901
>Cannot open file '/root/.mplayer/input.conf'
Are you running Mplayer as root?
>>51666901
>lel
Do you have any idea what you're doing, or just hoping to accidentally get something right?
>>51666969
Yes but that should still work.
>>51666971
I do have some sort of idea.
>>51666936
That doesn't fix the problem
AYY, I modified my original script and now it just werks.
#!/bin/bash
echo "Starting to play music"
cd ./playlist
pwd
LIST=$(ls | wc -l)
for ((i = 0; i <= $LIST; i++));
do
SONG=$(ls -r | tail -n 1)
echo $SONG
echo $LIST
sleep 3s
mplayer -volume 2 /var/www/html/vault/drive1/audio/playlist/$SONG
echo "Moving" $SONG
mv $SONG /var/www/html/vault/drive1/audio/played/
echo "----Next----"
done
echo "Finished, out of fresh music."