不知道如何在 bash 脚本中将 dir/file 的间距处理为 glob
因为我的笔记本电脑里有一些视频课程。 我现在想计算每门课程的总时间。 所以我用 bash 写了一个简单的脚本。
对于没有空格的文件/目录,它会成功。但对于有空格的目录/文件,就会出错。我尝试将 IFS 设置为其他值,但也不起作用。嗯,其中也有一些错误。
#!/bin/bash
## Usage: calTime.sh dir
#set -e
getTime(){
message=$($VIDEOTOOL $OPTION $FILE | grep "Duration")
if [ $? -eq 0 ]
then
local time=$(echo $message | cut -d',' -f1 | cut -d' ' -f5)
echo -e $time "\t" $FILE
#second=$(($second + $time))
let second+=$time
fi
} 2>/tmp/error
process(){
if [ -f $FILE ]
then
getTime
elif [ -d $FILE ]
then
cd $FILE
for i in $(ls);
do
local FILE=$i
process
done
fi
}
#################
#### main #######
#################
VIDEOTOOL=/usr/bin/avidemux2_cli
OPTION="--nogui --info --load"
second=0
list=${@-./}
for FILE in $list
do
process
done
echo $second
Since i have some video courses in my laptop.
I now want to calculate the total time of every course.
So i write a simple script with bash.
It succeeds for file/dir without space. But for dir/files with space, it goes wrong. I have tried to set IFS to something else, it doesn't work either. Well, there also some bugs in it.
#!/bin/bash
## Usage: calTime.sh dir
#set -e
getTime(){
message=$($VIDEOTOOL $OPTION $FILE | grep "Duration")
if [ $? -eq 0 ]
then
local time=$(echo $message | cut -d',' -f1 | cut -d' ' -f5)
echo -e $time "\t" $FILE
#second=$(($second + $time))
let second+=$time
fi
} 2>/tmp/error
process(){
if [ -f $FILE ]
then
getTime
elif [ -d $FILE ]
then
cd $FILE
for i in $(ls);
do
local FILE=$i
process
done
fi
}
#################
#### main #######
#################
VIDEOTOOL=/usr/bin/avidemux2_cli
OPTION="--nogui --info --load"
second=0
list=${@-./}
for FILE in $list
do
process
done
echo $second
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将变量放在引号中。
Stick your variables in quotes.
您必须用双引号保护您的变量 $FILE,如“$FILE”
You have to protect your variable $FILE with double quote like this "$FILE"
ls
输出。ls
output.您的问题似乎出在这一行:
试试这个:
另外,正如其他人所说,任何时候您有一个包含文件名的变量,它都应该用双引号引起来。例如:
Your problem appears to be with the line:
Try this instead:
Plus, as others have said, any time you have a variable that contains a filename, it should be in double quotes. For example: