0

I have an sh script:

#!/bin/bash
fullpath="$1"
filename="${fullpath##*/}"
dir="${fullpath:0:${#fullpath} - ${#filename}}" 
base="${filename%.[^.]*}"
ext="${filename:${#base} + 1}"   
if [ -f $fullpath ]; then
  if [ $ext != "mp4" ]; then
      ffmpeg -threads 4 -i $fullpath -y -vcodec libx264 -g 100 -bt 100k mp4 -vpre fast -acodec libfaac -ab 128k "$dir$base.mp4"
      mv -f "$dir$base.mp4" "/data/www/rfpl/htdocs/videotapes/$base.mp4"
  fi
fi

So, I wrote it for Linux, now I need to run it under FreeBSD, but I don't know anything about FreeBSD. There is an error:

./convert.sh: ${fullpath:0...}: Bad substitution.

I think that there is no bash, how can I make it to work?

Pierre.Vriens
  • 1,159
  • 34
  • 15
  • 19
  • Your script is running bash. Is it an old version? I'm wondering also if maybe you changed your script to run /bin/sh and forgot to indicate that in the question? – Phil Hollenback May 21 '11 at 19:24

2 Answers2

2

If you don't have bash installed already, then the FreeBSD preferred way is to use the ports:

cd /usr/ports/shells/bash
make install clean

If you don't have the ports tree installed, you can install it by running:

portsnap fetch extract

Doing either of these things will need root privs.

Note also, that typically bash is installed in /usr/local/bin/bash So you'll want to edit the first line of your script. In fact check that bash isn't already installed, before trying the above.

barryj
  • 978
  • 1
  • 5
  • 8
  • This is a good answer, but the OPs script says it is already running bash. So I wonder why he can't use standard bashisms? Maybe he's running a really old bash? Maybe the question is just missing some info. – Phil Hollenback May 21 '11 at 19:23
2

I don't have access to a freebsd box, but try this:

#!/bin/bash
fullpath="$1"
filename=$(basename $fullpath)
dir=$(dirname $fullpath)
ext=$(echo $filename | awk -F. '{ print $NF }')
base=$(echo $filename | sed "s/.${ext}//")
if [ -f $fullpath ]; then
  if [ $ext != "mp4" ]; then
    ffmpeg -threads 4 -i $fullpath -y -vcodec libx264 -g 100 -bt 100k mp4 -vpre fast -acodec libfaac -ab 128k "$dir/$base.mp4"
    mv -f "$dir/$base.mp4" "/data/www/rfpl/htdocs/videotapes/$base.mp4"
  fi
fi
toppledwagon
  • 4,245
  • 25
  • 15
  • I was just going to post something very similar - I checked the bsd man pages and dirname/basename are available. – user9517 May 21 '11 at 16:33