-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkfile
executable file
·66 lines (56 loc) · 1.22 KB
/
mkfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my %size_trans = (
"b" => 1,
"k" => 1<<10,
"m" => 1<<20,
"g" => 1<<30,
"t" => 1<<40,
);
my ($file_name, $file_size);
sub usage ()
{
my $usage ="usage:
$0 <NAME> <SIZE>
NAME is the file name to be generated, please no blank
SIZE is the file size to be generated, can be 10, 10M/m, 10G/g, etc.
";
print $usage;
exit 0;
}
if (scalar @ARGV < 2) {
usage
}
my $progress = 0;
sub update_progress ($$)
{
my $done = int 100*$_[0]/$_[1];
return if $done <= $progress;
print "\rCreating file... \%$done";
}
$file_name = $ARGV[0];
$file_size = $ARGV[1];
$file_size =~ /^(\d+)/;
my $size_n = $1;
my $c = lc substr($file_size, -1, 1);
$file_size = ($size_n *= $size_trans{$c});
print "file name: $file_name\n";
print "file size: $file_size ($ARGV[1])\n";
# confirm
print "\nAre you sure to create this file? (y/n) ";
my $ans = <STDIN>;
exit 0 if "n" eq lc substr($ans, 0, 1);
# do the creation
open my $file, ">", "$file_name" or die "cannot create file";
my $t = "0123456789abcdef"x32;
my $l = length $t;
my $n = $file_size;
while ($n > 0) {
print $file $t;
$n -= $l;
update_progress($file_size-$n, $file_size);
}
close $file;
print "\nfile $file_name generated!\n";