By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,776 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,776 IT Pros & Developers. It's quick & easy.

`du -sk $dir1/$file1` produces error

P: 11
Hi all,

I'm having a problem with the following piece of code in one of my programs:

Expand|Select|Wrap|Line Numbers
  1.  @temp = split(/\s+/, `du –sk $dir1/$file1`);
  2. push(@directorySizes, $temp[0]);
  3.  
I'm getting the following error for each time the first line is called: "du: cannot access `\226sk': No such file or directory." Everything is working as it's supposed to, I'm getting the correct value for du -sk $dir1/$file1 but it still prints this error for some reason. I've tried running the same command manually from the command line and it turns out fine. Does anyone know why this error message keeps appearing, how do I make it go away?

KiSSa
Sep 24 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
That would tell me that the "-" that precedes the options is not getting recognized. You might want to try escaping it with a "\".

Regards,

Jeff
Sep 24 '08 #2

P: 11
Hi,

I tried doing:

Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, `du \–sk $dir1/$file1`);
and it still acted the same way. Thanks for trying though.

KiSSa
Sep 24 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Try this and see if $out is populated:

Expand|Select|Wrap|Line Numbers
  1. $dir = "$dir1/$file1";
  2. $out = `du -sk $dir`;
  3. print $out;
Sep 24 '08 #4

P: 11
I tried that and the $out populated as it was supposed to. Also tried inserting $dir instead of $dir1/$file1 in the split line but the error message still shows. This is so weird.

KiSS
Sep 24 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I tried that and the $out populated as it was supposed to. Also tried inserting $dir instead of $dir1/$file1 in the split line but the error message still shows. This is so weird.

KiSS
Well, I am not sure why that is but you can now split $out instead of trying to split the return data from the shell directly in the split() function. You could also try opening "du" with a pipe and split the data line by line if that is necessary.
Sep 24 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
I tried your code on my local Linux machine and it works fine with no produced error. What OS are you working on?

I also produced the following, which does the same thing:

Expand|Select|Wrap|Line Numbers
  1. my @temparray;
  2. my @directorySizes
  3.  
  4. foreach my $line (`du -sk /home/jlk`){
  5.     @temparray=();
  6.     chomp($line);
  7.     @temparray = split(/\s+/, $line);
  8.     push(@directorySizes, $temparray[0]);
  9. }
  10.  
Either way, it seems to work fine. I was wrong about the \ earlier as the command is all inside of back tics, so it wouldn't matter. Just make sure that that character before the 'sk' is a dash. It doesn't show in the code tags for some reason.

Regards,

Jeff
Sep 24 '08 #7

Icecrack
Expert 100+
P: 174
Just make sure that that character before the 'sk' is a dash. It doesn't show in the code tags for some reason.

Regards,

Jeff


Sorry this isn't part of the topic but the - is coming up in *nix Character Encoding.
Guessing the code is set to windows character enc,

But for the topic

Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, `du -sk $dir1/$file1`);

try this


Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, "`du -sk $dir1/$file1`");

or

Expand|Select|Wrap|Line Numbers
  1. @temp = system(`du -sk $dir1/$file1`);
  2. @temp1 = split(/\s+/, "@temp");
Sep 24 '08 #8

P: 11
Sorry this isn't part of the topic but the - is coming up in *nix Character Encoding.
Guessing the code is set to windows character enc,

But for the topic

Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, `du -sk $dir1/$file1`);

try this


Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, "`du -sk $dir1/$file1`");

or

Expand|Select|Wrap|Line Numbers
  1. @temp = system(`du -sk $dir1/$file1`);
  2. @temp1 = split(/\s+/, "@temp");
I tried both of those, still no dice. Thanks for trying to help though. This is driving me nuts, the program works, I just want those ugly errors to go away.

KiSS
Sep 25 '08 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
I tried both of those, still no dice. Thanks for trying to help though. This is driving me nuts, the program works, I just want those ugly errors to go away.

KiSS
Again, specifically, what OS are you on? I have tried this several ways now, even similar to your original way and all work just fine.

Regards,

Jeff
Sep 25 '08 #10

P: 11
Sorry forgot to answer your question Jeff. I'm using F-secure SSH Client to log onto a linux machine.

KiSS
Sep 25 '08 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
Sorry forgot to answer your question Jeff. I'm using F-secure SSH Client to log onto a linux machine.

KiSS
Ok, Linux, that's a start. OS stands for Operating System. I ran the code on an Ubuntu 8.04 Hardy Heron machine and a Fedora Core 9 machine without issue.

Regards,

Jeff
Sep 26 '08 #12

Icecrack
Expert 100+
P: 174
try putting a reg ex to cancel perl Special characters,
example:


Expand|Select|Wrap|Line Numbers
  1. @temp = split(/\s+/, `du -sk $dir1/$file1`);
  2. @temp =~ s/\\ /\\\ /g;  #continue to cancel perl special char \ * ; ' " etc.
  3.  


i have found that reading input can error on text that contains perl special char.


other than that i got nothing.

note: if you see something wrong above or a better method please post as this will teach me & others (thank you).
Sep 26 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.