Welcome to the new Linux Foundation Forum!

Shell Script Help.....

I'm trying to learn to write scripts, this is my first attempt from reading online and I need some help.

The end result of the script will be to look in my directory and make a file of all file names with a VOB extension. Then the scipt will use mencoder to convert all the VOB files in to XVid files. Currently the script I have written will convert the movies, but I have to cut and paste the names into the script. When I use the $ls *.vob > vob.lst I end up with the error below.

Any help would be great, or if somebody could point me in the direction of a good book/site to read up on.

--- Screen Output ---

Knowledge is Power

./trial: line 7: /home/brian/*.vob: No such file or directory

./trial: line 8: /home/brian/A_NIGHT_AT_THE_MUSEUM.avi: Permission denied

[email protected] ~ $

--- The Trial Script I'm Working With ---

# My first shell script

#

clear

echo "Knowledge is Power"

echo ""

#

$ls /home/brian/*.vob > VOB.lst

$ls /home/brian/*.avi > AVI.lst

#

Comments

  • mfillpotmfillpot Posts: 2,180
    I use the for statement quiet alot.

    for i in $(ls|grep .vob);do convert $i $i.newext;done

    hopefully this will give you an idea, it would probably be best if you used sed to remove the extension from the names in the $() statement, then the conversion would be cleaner.
  • FluFlu Posts: 8
    Not sure if this is just an error with copying and pasting what you have or not, but you shouldn't need to use the dollar sign before the commands you want inside of the script, so:

    $ls /home/brian/*.vob > VOB.lst

    should be:

    ls /home/brian/*.vob > VOB.lst

    Also, you may want to hard code the path to your .lst file, just in case you end up running the script from an odd location..that way you can always be sure of where the resultant file is going to end up.

    Also, since Linux is case sensitive, you can use the excellent command that mfillpot mentioned, but make the search for the vob files case insensitive, like so: $(ls|grep -i .vob$)

    The -i on the grep command tells it to ignore case and the $ on the end of the .vob will make sure that it matches only .vob as a file extension and not anywhere else in the file.
  • jnvilojnvilo Posts: 10
    The $ sign is clearly a mistake on his part when he copy pasted from his terminal. His shell promt ends in $.
  • banatellobanatello Posts: 1
    It was the $, for some reason when I tried taking it off last night I was still getting the same error... I must have fat fingered the keyboard last night..... Thanks for the quick help.
  • vonbibervonbiber Posts: 28
    I would do something like the script below
    comments about the script
    first thing is to get the current working directory and
    set a variable to it (CWD)
    then set the location of the file where you're gonna
    write the list of your vob files (vob.lst in the current directory)
    set the location of the top directory of your vob files
    VOB=

    then I cd to that directory and recursively search for all
    it contents. Whenever I encounter a file with an .vob
    extension I write the full path to it in my list
    the if [ -d "$v" ] is to exclude directories
    this line
    if [ $(echo "$v" | grep -c "\.vob$") -eq 1 ]; then
    echo "$v" | grep -c "\.vob$"
    returns 1 if the filename ends with .vob
    0 otherwise


    f=$VOB/$(echo $v | sed 's?^\./??')
    the $v files are displayed as './something' (relative path)
    first I append the top directory to it and I remove the './'
    string, that way I get an absolute path
    ########### write a script, say getvobs.sh ###
    #!/bin/sh

    CWD=$PWD
    VOBLIST=$CWD/vob.lst

    rm -f $VOBLIST

    #replace by your top vob directory
    VOB=$CWD/vob

    cd $VOB || exit 1

    find . | while read v
    do
    if [ -d "$v" ]; then
    continue
    fi
    if [ $(echo "$v" | grep -c "\.vob$") -eq 1 ]; then
    f=$VOB/$(echo $v | sed 's?^\./??')
    echo $f >> $VOBLIST
    fi
    done
    ############# end of script ##############

    you don't need to use sed to get the output filename
    you can use the shell ${f%.*} construction
    e.g.
    if f=foo.vob
    convert $f ${f%.*}.newextension
    the output will be: foo.newextension
  • carpiecarpie Posts: 1
    If you're using bash, things can be a little simpler. Here's the general command I use for converting video files. (You can replace the ffmpeg line with your preferred conversion technique)
    for i in *.vob; do
    ffmpeg -i $i -acodec mp3 -ab 128k -vcodec mpeg4 -qscale 4 ${i%%.vob}.avi
    done
    

    $i becomes each vob filename and the sequence ${i%%.vob} rips off the .vob extension.
Sign In or Register to comment.