Linux Shell

循环

根据文件列表循环

1
for i in *.wmv; do ffmpeg -i "$i" "${i%.*}.mp4"; done

根据文本行循环

1
2
3
while read p; do
echo "$p"
done < test.txt

1
2
3
4
cat peptides.txt | while read line 
do
# do something with $line here
done

示例

定时转换当天新增视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# test.sh

mypidfile=/var/run/wmv_to_mp4.sh.pid

# Add check for existence of mypidfile
if [ -f $mypidfile ]; then
echo "script is already running"
exit
fi


# Ensure PID file is removed on program exit.
trap "rm -f -- '$mypidfile'" EXIT

# Create a file with current PID to indicate that process is running.
echo $$ > "$mypidfile"

cd $(date +%Y%m%d)

for i in *.wmv;
do
if (( $(date '+%s') > $(( $(date -r $i '+%s') + 600 )) )); then
ffmpeg -y -i "$i" "${i%.*}.mp4"
mkdir wmv
mv "$i" "wmv/${i}"
fi
done

References