Linux.com

Shell Script Help.....

Link to this post 18 May 09

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

brian@Brian-Linux ~ $


--- 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
#

Link to this post 18 May 09

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.

Link to this post 18 May 09

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.

Link to this post 18 May 09

The $ sign is clearly a mistake on his part when he copy pasted from his terminal. His shell promt ends in $.

Link to this post 18 May 09

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.

Link to this post 20 May 09

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

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board