LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   help with bash script: remove * to the last . in string (https://www.linuxquestions.org/questions/linux-software-2/help-with-bash-script-remove-%2A-to-the-last-in-string-438729/)

drkstr 04-25-2006 01:59 PM

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

Hobbletoe 04-25-2006 02:03 PM

To remove everything until the last period, you'll need the ## ...

Code:

${file##*.}

drkstr 04-25-2006 02:11 PM

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

ioerror 04-25-2006 04:54 PM

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).


All times are GMT -5. The time now is 06:16 PM.