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

标签 bash google-chrome ubuntu 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

我的第一次尝试(这是有效的,但我期待挑战自己)看起来 sg。像这样:
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命令行上的命令来测试它的作用。我希望这有帮助。

关于bash - 想要使用随机数从列表中启动 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25539373/

相关文章:

javascript - 在日期字段中设置日期将其设置为前一天

java - 在 Ubuntu 12.10 上使用 Tomcat 7 --- 如何?

linux - 如何设置全局 nofile 限制以避免 "many open files"错误?

linux - Bash 脚本 - 发送电子邮件

java - OS X Mavericks 上的 Tomcat 安装问题

xml - XML 文件的第一个字符必须是什么?

android - 使用操作菜单打开带有 "Powered By Chrome"的自定义 WebView

html - <hr> 元素在 JsFiddle 和 Chrome 之间以相反的方式呈现错误

apache - 在 Ubuntu 12.04 上使用 Nginx SSL 终止的 Varnish 3.0 [无限循环问题]

bash - 静默执行 vim 命令然后在 vim 中回显