Mailinglist Archive: opensuse (686 mails)

< Previous Next >
Re: [opensuse] bash: how to get file extension
lynn [15.07.2013 11:01]:
Hi everyone
How do I get the mp4 from this?
video_id="Robin Thicke - Blurred Lines ft. T.I.,
Pharrell-yyDUC1LUXSU.mp4"

I've tried these:

ext=${video_title/*./}
ext=${FILENAME##*.}
ext=$(echo "$video_title" | sed 's/^.*\.//')

The problem is the .'s

I want to ignore all .'s until the final one (not just for this string).

Thanks,
L x


Not nice, but works. On
<http://tldp.org/LDP/abs/html/string-manipulation.html>, I did not find
something like strrchr() in C, so I tried to implement it the
complicated way:

---snip---
video_id="Robin Thicke - Blurred Lines ft. T.I.,Pharrell-yyDUC1LUXSU.mp4"
STRLEN=${#video_id}
LOOPI=$((STRLEN-1))
TESTSTR="z${video_id:$LOOPI:1}"
while [ "$TESTSTR" != "z." ] ; do
TESTSTR="z${video_id:$LOOPI:1}"
LOOPI=$((LOOPI-1))
done
EXTLEN=$((STRLEN-LOOPI))
FIRSTPART=${video_id:1:$LOOPI}
FIRSTLEN=$((${#FIRSTPART}+2))
LASTPART=${video_id:$FIRSTLEN:$EXTLEN}
echo "Found first part: $FIRSTPART"
echo "Found extension: $LASTPART"
---pins---

Whenever I leave away the "z" in the comparison, I get '"." is a
directory'. Hm.

HTH,
Werner
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
References