[ubuntu-uk] Bash and Path checks

Tony Arnold tony.arnold at manchester.ac.uk
Fri Apr 17 22:46:15 BST 2009


Cornelius,

Cornelius Mostert wrote:
> Hallo
> 
> I wrote my first Bash script!
> However I am struggling to find a way to test if 2 paths are the same:
> 1. I use a for loop to run through a set of folders in a path (lets
> say /home/myFiles/)
> 2. Now Test if the File Extension is = "mpg"
> 2.1. If true then I would like to know if the path of the previous mpg
> file is the same as this mpg file
> 2.1.1. If != then I would like to create a few dirs in this new path
> 
> So script looks like:
> for file in 4StartPath do
>   fileExt = (get the file extention from 4file)
>   if [ $fileExt = 'mpg' ]
>   then
>     newPath = (get File Path form $file)
>     if [ $newPath != $oldPath ]
>     then
>       mkdir $newPath\Sound
>       mkdir $newPath\Sound\MP3
>       mkdir $newPath\Sound\wav
>     fi
> 
> ffmpeg ding dong grab the MP3 and the Wav
> 
> mv mp3 and wav file to directories created
> ....
> 
> Obviously this is not the Exact script but i am struggling to do the
> "if [ $newPath != $oldPath ]" bit

Are you doing this to avoid the mkdir if the directory you are creating
already exists? If so, then the is an option -p which means it is not an
error if the directory already exists. It also creates parent
directories as needed, so:

	mkdir -p $newPath/Sound/MP3
	mkdir -o $newpath/Sound/wav

will do what you want without the need to test.

(BTW, it's forward slashes / in Linux, not backslahes!)

Of course, I may have misunderstood what you are trying to achieve.

Regards,
Tony.




More information about the ubuntu-uk mailing list