【发布时间】: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