[ubuntu-uk] Bash and Path checks

Neil Greenwood neil.greenwood.lug at gmail.com
Fri Apr 17 21:55:55 BST 2009


2009/4/17 Cornelius Mostert <corneliusmostert at googlemail.com>:
> 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
>
> as I am new to bash scripts a code snippet would be nice and as I
> would like to know what the code does could you explaine it to me as
> well :-)
>
>
> Pretty please
>
> --
> ubuntu-uk at lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-uk
> https://wiki.ubuntu.com/UKTeam/
>

Does it have to work with symlinks and relative paths? If so, the only
way (as I understand it) is to cd into the directory and use the pwd
command to get the directory name.

If not, you could try putting double-quotes around the variables in
the if test, i.e.
if [ "$newPath" != "$oldPath" ]

This does a string comparison.

This is all untested. I'm OK at bash scripting, but not an expert by
any measure.


Cofion/Regards,
Neil.



More information about the ubuntu-uk mailing list