Re: [OT] Need some bash scripting assistance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 06/10/2010 08:32 AM, Marko Vojinovic wrote:
> Hi folks! :-)
>
> This is the story: I have a directory full of files named as:
>
> 01 Some file
> 02 Some other file
> 03&  yet another file
> 04 etc...
>
> There is also a symbolic link called "target" pointing to one of them.
> The numbers at the beginning of file names are there so that there is
> some specific ordering of files. What I need is a bash script that
> will determine the name of the file that goes after the one target
> points to, and relink the target to point to that one instead.
>
> So if target points to "02 Some other file", after executing the
> script it should point to "03&  yet another file". When the target
> reaches the end it should restart from the beginning. Files typically
> have spaces and other escapable characters that should be properly
> taken care of.
>
> Can anyone point me into the easiest way of doing this?
>
> What I am actually doing is more complicated, but this step is where I
> am a bit clueless... :-)
>
> Thanks! :-)
> Marko
>    

To reliable generate the list of files containing white space and 
special characters in bash, you need to use find with the argument -print0.

This will return a string with null delimited names.  Programs like 
xargs have a --null option to handle this type of list.  It is not 
possible to have a null character in a file system name, so this covers 
all bases.

If, however, you are willing to play in perl, this limitation goes away, 
because perl can read the actual directory listing, and strings in perl 
are always null terminated.

Here is a small sample used to change spaces to '_' in mp3 files.  And 
yes, there are guys who could do this in 5 lines, but for my sanity I 
did it long hand so I could still read it a year later. :)

I only include here how to walk the directory.

#!/usr/bin/perl -w
# Takes a dir argument
while (<>)
     {
     chop;
     $dir = $_;
     chdir $dir;
     $flag = 0;
     opendir (DIR,'.') or die "cannot open '.'\n";
     while ( $file = readdir ( DIR ))
         {
         if ( $file =~ /\.mp3$/ )
             {
             $flag = 1;
             }
...
         }
     }


Good Luck!
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux