我制作了这段代码来播放从播放列表文件(简单的文本文件,每行有不同的链接)读取的随机视频。这是我的第二次尝试。请不要笑我,因为第一次尝试是有效的!所以脚本的效果是一个空的 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
/tail
和 date
:sed "$(( $RANDOM % $( wc -l < urlfile ) + 1 ))"'!d' < urlfile
一旦你有这个工作,尝试将 URL 传递给
google-chrome
命令行上的命令来测试它的作用。我希望这有帮助。
关于bash - 想要使用随机数从列表中启动 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25539373/