想要使用随机数从列表中启动youtube视频



我制作这段代码是为了播放从播放列表文件(简单的文本文件,每行都有不同的链接)中读取的随机视频。这是我的第二次尝试。请不要嘲笑我,因为第一次尝试就成功了!所以脚本的效果是一个空的chrome窗口,就像一个新的选项卡。我不知道为什么这不起作用。

#!/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

我的第一次尝试(成功了,但我期待着挑战自己)看起来像这样:

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

我试过@shellter的两个建议:

#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'

因此,变量替换存在问题。我尝试了$(echo $url)而不是$url,但得到了相同的结果。所以我一无所知。

在这种情况下,我通常会尝试几个命令,并确保它能正常工作。例如,您可以运行此程序并查看结果(用您的文件对urlfile进行子辅导):

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

这应该从您的文件中随机挑选(ish)行。因此,我可以在不调用google-chrome的情况下运行此操作,以实际打开URL。这使用sed并使用$RANDOM而不是head/taildate:

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

一旦您完成了这项工作,请尝试在命令行上将URL传递给google-chrome命令,以测试它的作用。我希望这能有所帮助。

最新更新