Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Sep 2008
Posts: 11
#1: Sep 24 '08
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

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#2: Sep 24 '08

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


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
Newbie
 
Join Date: Sep 2008
Posts: 11
#3: Sep 24 '08

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


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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Sep 24 '08

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


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;
Newbie
 
Join Date: Sep 2008
Posts: 11
#5: Sep 24 '08

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


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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Sep 24 '08

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


Quote:

Originally Posted by KiSSFRo

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.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#7: Sep 24 '08

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


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
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#8: Sep 25 '08

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


Quote:

Originally Posted by numberwhun

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");
Newbie
 
Join Date: Sep 2008
Posts: 11
#9: Sep 25 '08

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


Quote:

Originally Posted by Icecrack

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
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#10: Sep 25 '08

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


Quote:

Originally Posted by KiSSFRo

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
Newbie
 
Join Date: Sep 2008
Posts: 11
#11: Sep 25 '08

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


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

KiSS
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#12: Sep 26 '08

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


Quote:

Originally Posted by KiSSFRo

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
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#13: Sep 26 '08

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


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).
Reply