【问题标题】:Want to launch youtube vids from a list using random number想要使用随机数从列表中启动 youtube 视频
【发布时间】:2014-10-21 18:13:30
【问题描述】:

我制作了这段代码来播放从播放列表文件(简单的文本文件,每行有不同的链接)读取的随机视频。这是我的第二次尝试。请不要笑我,因为第一次尝试是有效的!所以脚本的效果是一个空的 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,但得到了相同的结果。所以我一无所知。

【问题讨论】:

  • 你打开了shell跟踪/调试吗?至少set -x 或在执行我们之前查看行set -vx。几乎可以肯定,url=... 步骤并没有像您想象的那样工作。看到set -x这个视图之后,把cmd从里面拆开,然后执行cat $file然后cat $file | hean -n 10,然后……看看问题出在哪里。另外,如果您只想知道文件中有多少行,请执行num=$( wc -l < $file)
  • AND ...如果您使用的是$(( .. )) 形式,请保持一致,并使用ct=$(date +%s)。 cmd 替换不需要反引号。它在 1995 年的“Kornshell 编程语言”中被标记为已弃用!祝你好运!
  • 原来问题不在脚本中。我已经从 Compiz Config Settings Manager 运行了脚本。这样它就行不通了。但是从终端或从目录运行就可以了。知道问题的原因是什么吗?

标签: bash google-chrome ubuntu youtube


【解决方案1】:

在这种情况下,我通常会尝试几个命令并确保它们正常工作。例如,您可以运行它并查看结果(将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 命令来测试它的作用。我希望这会有所帮助。

【讨论】:

  • 第一个提示的结果仍然失败:+ google-chrome --incognito 'https://www.youtube.com/watch?v=x2KRpRMSu4g' 第二个提示的结果也不起作用:+ google-chrome --incognito 'https://www.youtube.com/watch?v=cpys1c3jCNs#t=175'
猜你喜欢
  • 1970-01-01
  • 2012-10-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
相关资源
最近更新 更多