Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is part of Shell script.
This is to check disk Usage.
I want to know how this could convert to perl script ?
Please help me for this.
DFCMD="df -P"
for fs in `eval $DFCMD`; do
FS=`echo $fs | awk ' { print $1 } '`
done
Thanks
perl -le 'print /(\S+)/ for `df -P`'
There is probably a 1liner to do it, but I'd do this (un tested)
my #command = qw/df -P/;
open(my $in, "-|", #command) or die "Can't run `#command`: $!";
while(<$in>) {
my ($first_col) = split;
print "$first_col\n";
}
well, in perl, it is often use the system() function to execute the shell command, but the system() function can not return the output of the command.You can use backticks (``) to get the output. Use the out put, you can convert the shell script to perl script
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my #output = split('\r\n', `df -P | awk '{print \$1}'`);
6 print #output;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for a Perl script which does the following:
takes two options, like script.pl -option1 -option2
Option 1 has two choices, say choice1 and choice2 and I have shell code
to do both actions (which I hope I can port to Perl)
Here option 2 is the path and is optional. If it's not specified it uses the current directory
Desired script
checks arguments
if choice 1 :
go to path (arg2)
run a code (i have it ready)
If choice 2
go to path (arg2 )
run a code
(not tested)
#! /usr/bin/perl
use warnings;
use strict;
my ($action, $path) = #ARGV;
$path //= '.';
chdir $path or die "Cannot chdir to $path.\n";
my $script = {
choice1 => 'script1.sh',
choice2 => 'script2.sh',
}->{$action};
die "Invalid choice $action.\n" unless defined $script;
0 == system $script or die "Status: $?";
Run as script.pl choice1 /path.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm running a perl script with a lot of input options, one of them being:
'errorcode=s{1,}' => \#ecodes,
I have a die at the end of the GetOptions if anything entered doesn't match the input. However if I input '--ecode 500' the program runs.
Why isn't the script dying? If I try something else like '--testing 123' it does die.
I'm guessing you have a option with a required argument such as
"foo=s" => \$foo,
and that you did something like
program --foo --ecode 500
which puts --ecode in $foo and 500 in #ARGV.
$ perl -MGetopt::Long -e'
use feature qw( say );
GetOptions("foo=s" => \$foo)
or die "usage\n";
say "ok <$foo> <#ARGV>";
' -- \
--ecode 500
Unknown option: ecode
usage
$ perl -MGetopt::Long -e'
use feature qw( say );
GetOptions("foo=s" => \$foo)
or die "usage\n";
say "ok <$foo> <#ARGV>";
' -- \
--foo --ecode 500
ok <--ecode> <500>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Can anyone tell me why I'm getting this:
usage: gen-non-random.pl <count> <outputfile>
From the code below:
#!/usr/bin/perl -w
#
# Script to generate non random values, to demonstrate a bad randomness graph
# for my "Howto Analyse SessionIDs".
#
# written by:
$version = "0.0.4";
$filename = "gen-non-random.pl";
$usage = "usage: $filename <count> <outputfile>\n";
$count = $ARGV[0] or die ("$usage\n");
$output = $ARGV[1] or die ("$usage\n");
print ("-- $filename Version: $version\n");
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use Math::Random;
use Digest::MD5 qw(md5_hex);
open (OUT, ">$output") or die ("Can't open $output\n");
for ($i=0; $i<$count;$i++)
{
# generate a random number
$random = random_uniform();
# cut out char 3-9 of $random and put it in $randsub
$randsub = substr($random, 2, 6);
# get seconds and microseconds since epoch
($seconds, $microseconds) = gettimeofday;
# get the last two chars of the seconds and put them into $s
$s = substr($seconds, 8, 2);
# sleep for a while
usleep $randsub;
# put together the last two digits of seconds and the microseconds
$time = $s . $microseconds;
$md5_time=md5_hex($time);
# print out the stuff we put together above
print OUT ("$md5_time\n");
}
close (OUT) or die ("Can't close $output\n");
print ("$count values written to $output\n");
exit;
I am new to programming so i need really simple answer please! I do not own this code I am using for my research paper at University. Also, could someone please explain to me what Usage actually is i can't seem to find a good explanation for it?
Thanks.
You're getting that error because you're not using the program correctly:
usage: gen-non-random.pl <count> <outputfile>
This basically means you have to provide a count and output file as arguments, such as:
perl gen-non-random.pl 42 outfile.txt
This will generate forty-two numbers and output them to the outfile.txt file.
It's the two lines near the start, checking ARGV[0/1] and die-ing if you don't provide them, that are outputting this message and exiting the program.
Hmmm. I can't run the above code because Time::HiRes::ualarm() is not implemented on Windows. That said, it appears to be generating a MD5 has string of the current time (in integer form) after sleeping for a random number of seconds, then dumping the result into a text file. You are getting the usage message mentioned above because the program expects input. Try running it from the command line like so:
perl gen-non-random.pl 10 MyResults.txt
I suspect that will dump 10 HD5 hash results into a file called "MyResults.txt".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there any way to write a Perl script to calculate the MD5sum of every file in a directory?
If so, how could I do this?
Well there are many ways to do this but it comes down to two operations you need to preform. You will first need to locate a list of the files you would like to run the check and then you will need to run the md5sum check on each of those files. There is a ton of ways to do this but the following should work for your needs.
#!/usr/bin/perl
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
my $dirname = "/home/mgreen/testing/";
opendir( DIR, $dirname );
my #files = sort ( grep { !/^\.|\.\.}$/ } readdir(DIR) );
closedir(DIR);
print "#files\n";
foreach my $file (#files) {
if ( -d $file || !-r $file ) { next; }
open( my $FILE, $file );
binmode($FILE);
print Digest::MD5->new->addfile($FILE)->hexdigest, " $file\n";
close($FILE);
}
The above will grab the md5sum for each file in the directory and skip any sub-directories and print it to STDOUT. The MD5 checksum part is then done by the Digest::MD5 module which is ultimately what I think you are looking for.
I do like your question though as it is open-ended with alot of possible solutions like all "How do I do this in perl?" questions so I am sure you will get alot of possible solutions and I will most likely update mine when I get home later.
Maybe using find with -exec can do the job?
find . -name "*.*" -exec md5sum '{}' \;
The '{}' will be replaced with the name of each file.
Use opendir and readdir or other recursive method.
Here is an example:
#!/usr/bin/perl -w
use warnings;
my $DIR_PATH="a";
opendir DIR, ${DIR_PATH} or die "Can not open \"$DIR_PATH\"\n";
#filelist = readdir DIR;
foreach $file (#filelist) {
open(IN,"a/$file")or die "cannot open";
while(<IN>){...}
close IN;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove every lines in a text file that contain the word "script" and then write whatever left into another file. But keep the originl file.
like this:
open the file
delete any line with the word "script"
then output whatever left after delete to another file.
perl -ne '/script/ or print' file > newfile
grep -v script original.file > new.file
Or if you really need perl:
#!/usr/bin/perl
use strict;
use warnings;
open(my $in, '<', 'input.txt')
or die "Cannot open input.txt: $!";
open(my $out, '>', 'output.txt')
or die "Cannot open output.txt: $!";
while (<$in>) {
print $out $_ unless /script/;
}
close($in);
close($out);
Finally, if you are only looking to match "script" if it is a word (and not part of a bigger string like "prescription" or "scripting") then change:
/script/
To:
/\bscript\b/