0

I made this code to play random vids read from a playlist file (simple text file, with different link in each line). This is my second attempt. Please don't laugh at me, because the first attempt was working! So the effect of the script is an empty chrome window, just like a new tab. I've no clue why is this not working.

#!/bin/bash
#initializing the file and current time in millis
file="youtube.songs"
ct=`date +%s`
#counting the lines in the list file
num=`wc -l $file | cut -f1 -d' '`
rem=$(( $ct % ( $num - 1 ) ))
ln=$(( $rem + 1 ))
#geting the url by line number
url=`cat $file | head -n $ln | tail -n 1 `
google-chrome --incognito $url

My first attempt (which was working, but I was looking forward to challenge myself) looked sg. like this:

ct=`date +%s`
rem=$(( $ct % 22 ))
case $rem in
1)
  url="https://www.youtube.com"
;;
*)
;;

I've tried both of @shellter's advice:

#initializing the file and current time in millis
file="youtube.songs"
+ file=youtube.songs
ct=$( date +%s )
 date +%s )
 date +%s 
++ date +%s
+ ct=1409239606
#counting the lines in the list file
num=$( wc -l $file | cut -f1 -d' ' )
 wc -l $file | cut -f1 -d' ' )
 wc -l $file | cut -f1 -d' ' 
++ wc -l youtube.songs
++ cut -f1 '-d '
+ num=25
num=$(( $num - 1 ))
+ num=24
rem=$(( $ct % $num ))
+ rem=22
ln=$(( $rem + 1 ))
+ ln=23
#geting the url by line number
url=$( cat $file | head -n $ln | tail -n 1 )
 cat $file | head -n $ln | tail -n 1 )
 cat $file | head -n $ln | tail -n 1 
++ head -n 23
++ cat youtube.songs
++ tail -n 1
+ url='https://www.youtube.com/watch?v=DmeUuoxyt_E'
google-chrome --incognito $url
+ google-chrome --incognito 'https://www.youtube.com/watch?v=DmeUuoxyt_E'

So there is an issue with the variable substitution. I've tried $(echo $url) instead of $url, but got the same results. So I am clueless.

Sgt.Doakes
  • 97
  • 2
  • 6
  • did you turn on shell trace/debugging? At least `set -x` or to see lines before they are executed us `set -vx`. Almost certainly the `url=...` step isn't working quite as you think it is. After seeing the `set -x` view of that, the take the cmd apart from the inside, and execute `cat $file` then `cat $file | hean -n 10`, then ... to see where the problem is. Also, if you just want to know how many lines in a file, do `num=$( wc -l < $file)`. – shellter Aug 28 '14 at 02:28
  • AND ... if you're using `$(( .. ))` form, then be consistent, and use `ct=$(date +%s)`. No need for back-quotes for cmd-substitution. It was marked as deprecated in 1995 "The Kornshell Programming Language"! Good luck! – shellter Aug 28 '14 at 02:31
  • It turns out the problem isn't in the script. I've ran the script from **Compiz Config Settings Manager**. That way it is not working. But running from terminal or from the dir works just fine. Any idea what's the cause of the problem? – Sgt.Doakes Aug 28 '14 at 18:47

1 Answers1

1

What I usually do in this circumstance is try several commands and make sure that's working. For example, you could run this and see what the result is (subsituting urlfile with your file):

head -n $(( $( date +%s ) % $( wc -l < urlfile ) + 1 )) < urlfile | tail -1

This should pick random(ish) lines from your file. So, I'd get this running without calling google-chrome to actually try to open the URL. This uses sed and uses $RANDOM instead of the head/tail and date:

sed "$(( $RANDOM % $( wc -l < urlfile ) + 1 ))"'!d' < urlfile

Once you have that working, try passing the URL to the google-chrome command on the command line to test what this does. I hope this helps.

Community
  • 1
  • 1
zerodiff
  • 1,690
  • 1
  • 18
  • 23
  • result of the first hint is still a failure: `+ google-chrome --incognito 'https://www.youtube.com/watch?v=x2KRpRMSu4g'` result of the second hint also not working: `+ google-chrome --incognito 'https://www.youtube.com/watch?v=cpys1c3jCNs#t=175'` – Sgt.Doakes Aug 28 '14 at 16:56