talz13
September 9th, 2008, 03:44 PM
I'm trying to write a shell script that will handle filename inputs (or a directory via output of 'ls'):
#! /bin/bash
if [ -e $1 ]
then
if [ -f $1 ]
then
# it's a file, start processing it...
echo "Processing a single file...";
command="~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f $1";
echo $command;
elif [ -d $1 ]
then
# it's a directory, list the contents and start processing them in a batch...
echo "Processing an entire directory...";
for i in $(ls $1 | grep -v db\$);
do
`~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f $1/$i`;
done
fi
fi
But when the filename contains spaces, brackets, or other special characters, it either drops the escape \ or doesn't receive an escape to begin with (i.e. from 'ls' output). Is there a way to programatically escape these characters so that it can find the files?
Example:
jeff@server:~$ ls /home/shares/video/anime/bleach/
[DB]_Bleach_183_[07493DEF].avi [DB]_Bleach_185_[B1B2B639].avi
[DB]_Bleach_184_[CC171141].avi [DB]_Bleach_186_[37E46364].avi
jeff@server:~$ ./pspEncoder.sh /home/shares/video/anime/bleach/\[DB\]_Bleach_183_\[07493DEF\].avi
Processing a single file...
~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f /home/shares/video/anime/bleach/[DB]_Bleach_183_[07493DEF].avi
jeff@server:~$
It completes "successfully" even though it didn't actually run (since it couldn't find a file "[DB]_Bleach_183_[07493DEF].avi", it needs to find "\[DB\]_Bleach_183_\[07493DEF\].avi" instead...)
#! /bin/bash
if [ -e $1 ]
then
if [ -f $1 ]
then
# it's a file, start processing it...
echo "Processing a single file...";
command="~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f $1";
echo $command;
elif [ -d $1 ]
then
# it's a directory, list the contents and start processing them in a batch...
echo "Processing an entire directory...";
for i in $(ls $1 | grep -v db\$);
do
`~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f $1/$i`;
done
fi
fi
But when the filename contains spaces, brackets, or other special characters, it either drops the escape \ or doesn't receive an escape to begin with (i.e. from 'ls' output). Is there a way to programatically escape these characters so that it can find the files?
Example:
jeff@server:~$ ls /home/shares/video/anime/bleach/
[DB]_Bleach_183_[07493DEF].avi [DB]_Bleach_185_[B1B2B639].avi
[DB]_Bleach_184_[CC171141].avi [DB]_Bleach_186_[37E46364].avi
jeff@server:~$ ./pspEncoder.sh /home/shares/video/anime/bleach/\[DB\]_Bleach_183_\[07493DEF\].avi
Processing a single file...
~/Downloads/encode-handheld-1.9.pl -t psp -threads 4 -f /home/shares/video/anime/bleach/[DB]_Bleach_183_[07493DEF].avi
jeff@server:~$
It completes "successfully" even though it didn't actually run (since it couldn't find a file "[DB]_Bleach_183_[07493DEF].avi", it needs to find "\[DB\]_Bleach_183_\[07493DEF\].avi" instead...)