DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 27th January 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default 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
A nicely formatted version:
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
The awk script to accomplish this:
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
The previous version :
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
Reply With Quote
  #2   (View Single Post)  
Old 27th January 2010
wesley wesley is offline
Real Name: Wesley
Shell Scout
 
Join Date: Aug 2009
Location: Reunion Island
Posts: 92
Default

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?
Reply With Quote
  #3   (View Single Post)  
Old 27th January 2010
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

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''.
Reply With Quote
  #4   (View Single Post)  
Old 28th January 2010
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by wesley View Post
It is very technical... hard to understand all...
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:
  • There may (or may not) be some pre-processing which needs to be done before starting
  • repeatedly performing a different set of tasks on each row in a file
  • followed (possibly) by a summary at the end
You may see that this described what needs to be done in any report -- output header information, churn through all rows, followed by providing totals at the end. awk(1) does this with:
  • an optional BEGIN block (which is executed only once if present...)
  • followed by a unnamed block which processes each record
  • followed by an optional END block (which is also only executed once if present...)
J65nko didn't provide an END block because in this particular problem, it was not needed. However, the language implemented by an awk(1) interpreter is rich enough to provide the functionality if such summation is required.

As for where to find more information on awk(1):
  • The best book introducing awk(1) (in my estimation...) is Aho, Weinberger, & Kernighan (& if you look at the last names long enough, you may see where the language got its name...):

    http://www.amazon.com/AWK-Programmin...4658668&sr=8-2

    If you are really interested in learning about awk(1), go to a library & read it. Other books have been published. You may find some at your local library too.
  • A brute force Web search:

    http://www.google.com/#hl=en&source=...df356c6a3f8304

    ...will also result in displaying a number of sites which discuss the idiosyncrasies of awk(1) programming.
  • And as already mentioned, the awk(1) manpage itself distills this information into a concise document.
Quote:
Why don't you build guide for novice users?
In a perfect world this might be possible, but you are asking people to invest significant time into writing detailed documents when they don't have the time. Online forums aren't the best venue for presenting extended lectures either. However, you are invited to read the following threads which describes what forums do best:Study up, & come back with more specific questions.

Reply With Quote
Reply

Tags
/etc/fstab, awk

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 10:33 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick