LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-25-2006, 01:59 PM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
help with bash script: remove * to the last . in string


Hello, I finally decided to sit down and learn how to write scripts in bash so I wrote one to encode any video files in a directory to DIVx using mencoder. I wanted it to only touch valid video files by checking the suffix at the end of the file. I used FILESUF=${file#*.} to remove everything but the suffix. I ran into a problem with files that have multiple '.'s in the name. Is there a way to start at the end of the sting going backwards and remove everything after (and including) the first '.' it finds? THanks for the help!

Here is the full code for anyone that's interested:
Code:
#!/bin/bash
#  Converts all video in a directory to DIVx5.0
#  Uses the "mencoder" binary from the "mplayer" package,

SUFFIX=avi          # New filename suffix. 
VALIDSUF=(mpg mpeg mjpeg mv rm rmvb asf mov)  #removed wmv as it does not convert well
element_count=${#VALIDSUF[@]}

#process arguments
case "$1" in
 --help | -h)
  echo "Use encodedivx to encode all video media in a directory to the DIVx5.0 codec format"
  echo
  echo "Usage: encodedivx <directory>"
  echo "will assume current directory if none specified"
  exit 1
   ;;
 */)  #directory argument?
  DIR=$1
  ;;
 *)   #default
  if [ -n "$1" ]   #argument is given but invalid
   then
    echo "Usage: encodedivx <directory>"
    exit 1
   else        #use current DIR if non specified
    DIR=$PWD 
    echo DIR=$PWD
  fi
esac  

echo "Encode all video files in $DIR?: {Y|N}"
read VERIFY

while true  #loop until valid input for $VERIFY
do
 case "$VERIFY" in
  "Y" | "y")
   for file in $DIR/*    #check files in $DIR
   do
    FILENAME=${file%.*}  #break up filename
    FILESUF=${file#*.}
    i=0
    while [ "$i" -lt "$element_count" ]   #check if suffix is valid
    do
     if [ "${VALIDSUF[$i]}" == "$FILESUF" ]  #if valid, encode
     then
      echo
      mencoder $file -ovc lavc -oac lavc -ffourcc DX50 -o $FILENAME.$SUFFIX
     fi
     let "i=$i+1"  #check next suffix
    done
   done
   break
   ;;
  "N" | "n")
   echo "Quiting..."
   exit 1
   ;;
  *)   #invalid input for $VERIFY
   echo "Encode all video files in $DIR?: {Y|N}"
   read VERIFY
 esac
done


exit 0
I would be more then happy to hear any suggestions on how to improve this. This was my first attempt at a shell script and it seems kind of clunky to me.

thanks!
...drkstr

Last edited by drkstr; 04-25-2006 at 02:04 PM.
 
Old 04-25-2006, 02:03 PM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
To remove everything until the last period, you'll need the ## ...

Code:
${file##*.}
 
Old 04-25-2006, 02:11 PM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
that was a lot more simple then I thought, thanks for the help! While I'm at it, is there a better way to check if something exists in an array instead of looping though and checking each element? Any thoughts on this would be much appreciated.

thanks!
...drkstr
 
Old 04-25-2006, 04:54 PM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Quote:
is there a better way to check if something exists in an array instead of looping though and checking each element?
Not with an array. You'd need to use an associative array (hash table), but bash doesn't have associative arrays (zsh does though).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
String manipulation with BASH script King V Programming 9 04-21-2006 03:15 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration