Hiya, Something very odd is going on here (Very probably as a result of my dodgy coding) :)

The script below is script designed to neatly convert all the avi/mp4/ogm files in a directory into MKVs.
#!/usr/bin/perl while(<*>) { $o = $_; s/\ /\¬/g; rename($o, $_); } foreach (<*.avi>) { $cmd = "mkvmerge $_ -o "; s/\.avi$/.mkv/; $cmd .= $_; print "$cmd\n"; system($cmd); } @filelist = glob("*.avi"); unlink @filelist; foreach (<*.ogm>) { $cmd = "mkvmerge $_ -o "; s/\.ogm$/.mkv/; $cmd .= $_; system($cmd); } @filelist = glob("*.ogm"); unlink @filelist; foreach (<*.mp4>) { $cmd = "mkvmerge $_ -o "; s/\.mp4$/.mkv/; $cmd .= $_; system($cmd); } @filelist = glob("*.mp4"); unlink @filelist; while(<*>) { $_ = $o; s/\ /\¬/g; rename($_, $o); } while(<*>) { $o = $_; s/\¬/\ /g; rename($o, $_); }
I've saved it in /usr/bin to give me systemwide capability. In essence, the script should run through a four step process-
1. Replace any spaces in filenames with the ¬ character.
2. Run MKV merge on the files that need it.
3. Delete the source files.
4. Put the spaces back into the filenames.


The problem is that it only works correctly as root. If I launch it as a normal user, it only works if a file with spaces in it's name is present. When there are no files with spaces, it simply skips on to the deletion phase.

Basically, I don't understand why when there are no filenames with spaces present it works correctly as root, but not as a normal user.

FWIW, I chose to write this in Perl, as this script occasionally gets used on my Windows machine. Here it works correctly too.

Cheers

In reply to Why does this only work correctly as root? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.