我有以下代码:
#! /bin/sh
tail -f /var/log/omxlog | stdbuf -o0 grep player_new | while read i
do
omxd S
done
它以以下格式输出视频播放列表(实时):
...
Playing 33/75 /myfolder/F01_Ship_111.mp4
Playing 0/22 /myfolder/F02_Car_101.mp4
Playing 11/32 /myfolder/F03_Animal_001.mp4
Playing 23/84 /myfolder/F04_Book_000.mp4
...
现在我想将当前正在播放的视频的名称保存在多个变量中,例如:
x1 = F01_Ship_111.mp4
x2 = F01_Ship_111
x3 = F01
x4 = Ship
x5 = 111
x6 = mp4
那么,我该怎么做呢!
答案1
Values=$(omxd S | awk -F/ '{print $NF}')
x1="${Values}"
x2="${Values%.*}"
x3="${Values%%.*}"
x4=$(echo ${Values} | cut -d_ -f2)
x5=$(echo ${x2} | cut -d_ -f2)
x6=${Values##*.}
答案2
既然你标记了 bash,我将使用它[[ ... ]]
条件构造:
#!/bin/bash
tail -f /var/log/omxlog | stdbuf -o0 grep player_new | while read i
do
[[ $i =~ .*/([^_]+)_([^_]+)_([^.]+).(.+)$ ]] && {
x1=${BASH_REMATCH[1]}_${BASH_REMATCH[2]}_${BASH_REMATCH[3]}.${BASH_REMATCH[4]}
x2=${BASH_REMATCH[1]}_${BASH_REMATCH[2]}_${BASH_REMATCH[3]}
x3=${BASH_REMATCH[1]}
x4=${BASH_REMATCH[2]}
x5=${BASH_REMATCH[3]}
x6=${BASH_REMATCH[4]}
}
omxd S
done