|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
|||
Formatting OpenBSD /etc/fstab file with awk
An example of an OpenBSD /etc/fstab file:
Code:
/dev/wd0a / ffs rw 1 1 /dev/wd0m /backup ffs rw,nodev,nosuid 1 2 /dev/wd0e /home/j65nko ffs rw,nodev,nosuid 1 2 /dev/wd0d /tmp ffs rw,nodev,nosuid 1 2 /dev/wd0k /usr ffs rw,nodev 1 2 /dev/wd0l /usr/local ffs rw,nodev 1 2 /dev/wd0f /var ffs rw,nodev,nosuid 1 2 /dev/wd0h /var/log ffs rw,nodev,nosuid 1 2 /dev/wd0g /var/tmp ffs rw,nodev,nosuid 1 2 Code:
# special mount fs mount dump fsck # device point type options freq pass# # ======= ===== ==== ======= ==== ===== # /dev/wd0a / ffs rw 1 1 /dev/wd0m /backup ffs rw,nodev,nosuid 1 2 /dev/wd0e /home/j65nko ffs rw,nodev,nosuid 1 2 /dev/wd0d /tmp ffs rw,nodev,nosuid 1 2 /dev/wd0k /usr ffs rw,nodev 1 2 /dev/wd0l /usr/local ffs rw,nodev 1 2 /dev/wd0f /var ffs rw,nodev,nosuid 1 2 /dev/wd0h /var/log ffs rw,nodev,nosuid 1 2 /dev/wd0g /var/tmp ffs rw,nodev,nosuid 1 2 Code:
# Reformat OpenBSD /etc/fstab file # $Id: format_fstab,v 1.1 2010/01/27 02:14:02 j65nko Exp $ # usage: awk -f format_fstab /etc/fstab # WARNING: first backup your original fstab file before redirecting # the output of this awk script to the /etc/fstab # cd /etc # cp -p fstab fstab.orig # awk -f format_fstab fstab.orig >fstab # cat fstab BEGIN { printf("%s\t%s\t\t\t\t\t%s\t%s\t\t\t\t\t%s\t%s\n", "# special","mount","fs","mount","dump","fsck") printf("%s\t%s\t\t\t\t\t%s\t%s\t\t\t\t\t%s\t%s\n","# device","point","type","options","freq","pass#") printf("%s\t%s\t\t\t\t\t%s\t%s\t\t\t\t\t%s\t%s\n","# =======","=====","====","=======","====","=====") printf("#\n") } { T2=" " if (length($2) >= 1 ) T2="\t\t\t\t" if (length($2) >= 8 ) T2="\t\t\t" if (length($2) >= 16 ) T2="\t\t" if (length($2) >= 24 ) T2="\t" T4=" " if (length($4) >= 1 ) T4="\t\t\t\t" if (length($4) >= 8 ) T4="\t\t\t" if (length($4) >= 16 ) T4="\t\t" if (length($4) >= 24 ) T4="\t" printf( "%s\t%s%s\t%s\t%s%s\t%s\t%s\n" ,$1,$2,T2,$3,$4,T4,$5,$6) } # end of script Code:
# -------------------------------------------------- FILE=/etc/fstab FILE=./fstab ORIG=${FILE}.orig cp ${FILE} ${ORIG} # create backup echo Formatting ${FILE} cat <<END >${FILE} # special mount fs mount dump fsck # device point type options freq pass# # ====== ===== ==== ======= ==== ===== # $(awk '{ printf("%s\t%s\t\t%s\t%s\t\t\t\t%s\t%s\n", $1, $2, $3, $4, $5, $6)}' $ORIG ) /dev/cd0a /cdrom d9660 ro,noauto 0 0 /dev/fd0a /floppy msdos ro,noauto 0 0 END cat $FILE
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Good article, as i see, you love scripts ! It is very technical... hard to understand all...it is necessary to be honest ;-)
Why don't you build guide for novice users? |
|
||||
All you need for that Wesley, is to type `man awk` on any decent machine. I know FreeBSD and OpenBSDs manual pages for it do plenty of explaining, but I can't say the same for every platform.
The same thing could also be used with Perls built in rubish printing language.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
|||
J65nko is simply showing how awk(1) can be used to change the formatting of information found in /etc/fstab. Note that similar results can be seen in the output of mount(8). Although I have not spoken to J65nko about his intentions, I suspect he is wanting to start conversation with the membership at large about how the scripting languages found in all members of the *BSD family can be used to perform a number of trivial tasks without in-depth knowledge or experience in programming. Unix began with a culture assuming that users were astute enough to use the tools available to save them time, & much of that culture still exists today. The fact that you are asking questions means that J65nko has had some success.
Although the code provided does not do anything extensive, what needs to be supplied to awk(1)'s interpreter to perform this formatting is not that complicated either. In fact, a point being made is that with nominal knowledge of what awk(1) does & does well, it serves as a good tool to solve this particular type of problem. Other scripting languages can do the same thing, but other scripting languages also come from different backgrounds. The mindset needed to write similar code will result in completely different scripts. I urge others to take this same problem & implement a similar solution in other languages. The point is not to pit one language against another, but point out differences in their underlying assumptions. Just to quickly explain the methodology of awk(1) scripts, awk(1) is very good at batch processing -- performing the same tasks to each & every row in a file (preferably text files...). In meeting this goal:
As for where to find more information on awk(1):
Quote:
|
Tags |
/etc/fstab, awk |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Automating OpenBSD snapshot downloads with a .netrc file | J65nko | Guides | 1 | 7th January 2010 03:09 AM |
table formatting (could not find better title) | gosha | Programming | 10 | 19th March 2009 06:33 PM |
Formatting Hard Disk Drive to UFS in OS X 10.5 | Turquoise88 | Other BSD and UNIX/UNIX-like | 1 | 7th March 2009 09:57 PM |
Formatting fat32 drive quickly | map7 | FreeBSD General | 10 | 26th July 2008 05:17 PM |
fstab and CD/DVD device | corneliu | FreeBSD General | 7 | 24th May 2008 02:11 AM |