diff options
author | Florian Pritz <f-p@gmx.at> | 2008-10-23 19:52:30 +0200 |
---|---|---|
committer | Florian Pritz <f-p@gmx.at> | 2008-10-23 19:52:30 +0200 |
commit | c597b01166c4ac4d042c2256826b69c6fb0bc405 (patch) | |
tree | 72f0971a761070273b625650c7cb40149db02ef7 /reformat_fstab.pl | |
download | bin-c597b01166c4ac4d042c2256826b69c6fb0bc405.tar.gz bin-c597b01166c4ac4d042c2256826b69c6fb0bc405.tar.xz |
first init
Diffstat (limited to 'reformat_fstab.pl')
-rw-r--r-- | reformat_fstab.pl | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/reformat_fstab.pl b/reformat_fstab.pl new file mode 100644 index 0000000..9c81d8d --- /dev/null +++ b/reformat_fstab.pl @@ -0,0 +1,70 @@ +#! /usr/bin/perl +use warnings; +use strict; + +my $fstabFile = shift; +my $fstabOld; +my $fstab; +my @fstabLines; +push @fstabLines, ['# <file system>', '<dir>', '<type>', '<options>', '<dump>', '<pass>']; +my @lengths = map {length($_)} @{$fstabLines[0]}; +my $longestLine; + +open(my $fh, '<'. $fstabFile) or die "Unable to open $fstabFile: $!\n"; +while(defined(my $line =<$fh>)) +{ + $fstabOld .= $line; + $longestLine = (length($line) > $longestLine) ? length($line) : $longestLine; + if ($line =~ m/^#/ and $line !~ m/<file system>/) + { + $fstab .= $line; + } + elsif ($line =~ m/^#/ and $line =~ m/<file system>/) + { + next; + } + else + { + $line =~ s/ +/ /g; + my @fields = split ' ',$line; + push @fstabLines, \@fields; + for (my $i; $i < scalar @fields; $i += 1) + { + if (length($fields[$i]) > $lengths[$i]) + { + $lengths[$i] = length($fields[$i]); + } + } + } + +} +close $fh; + +my $printfStr = join '', map( {'%-'.($_+1).'s';} @lengths); + +foreach my $line (@fstabLines) +{ + my @line = @{$line}; + $line = sprintf($printfStr, @line)."\n"; + $fstab .= $line; + $longestLine = (length($line) > $longestLine) ? length($line) : $longestLine; +} + +my $separator = ('-' x $longestLine)."\n"; + +print "\n",$separator, "ORIGINAL FILE\n", $separator, $fstabOld, $separator, "REFORMATTED FILE\n", $separator, $fstab, $separator; + +print "Overwrite old file with new one? (y/n) "; +my $ans = <>; +chomp $ans; +if ($ans eq 'y') +{ + open(my $fh, '>', $fstabFile) or die "Unable to open $fstabFile: $!\n"; + print $fh $fstab; + close $fh; +} +else +{ + print "Exiting without saving...\n"; + exit; +} |