Author Archives: Todd Partridge (Gently)

Get down with getopt

What is getopt?

getopt is a command used in scripts to parse their options and add a basic error checking ability. getopt is not getopts the bash built-in that has similiar functionality. Urban Vagabond explains:

getopt and getopts are different beasts, and people seem to have a bit of misunderstanding of what they do. getopts is a built-in command to bash [that] processes command-line options in a loop and assigns each found option and value in turn to built-in variables [(so that they can be further processed)]. getopt, however, is an external utility program, and it doesn’t actually process your options for you the way that [(e.g.)] bash getopts, the Perl Getopt module, or the Python optparse/argparse modules do. All that getopt does is canonicalize the options that are passed in — (i.e. convert them to a more standard form) so that it’s easier for a shell script to process them.

For example, a use of getopt converts the following:

tmpscript -abd -ooutfile.txt

into this:

tmpscript -a -b -d -o outfile.txt

getopt can also process the long format option of --output=/tmp.... Also basic error check abilities, it has:

tmpscript -c
tmpscript: invalid option -- 'c'

or

tmpscript -o
tmpscript: option requires and argument -- 'o'

getopt gets defined by telling it the available options in short and long form (if you like). An option with a colon (:) following denotes that an argument is required for that option. If followed by two colons the argument is optional. Here is an example:

Options are generally set as variables so that they can defined how to be used after the while loop (usually, so that mainly they are processed in the right order). getopt parses in the same order as specified on the input, so a -a -d -b input would not work real well if the -d option required the -b option to run correctly.

One caveat of getopt is that each option allows either one or no arguments to follow. So in input like tmpscript -v --files file1.txt file2.txt file3.txt just isn’t possible in any type of predictable-fashion. One can however use quotes on the input:

tmpscript -v --files "file1.txt file2.txt"

or use mutiple options (-f file1.txt -f file2.txt) and append them to an array, as seen, here.

Lg monitor ips224v-pn, look

I decided to get the LS IPS224V-PN and here is a look at it. First thing I noticed was that the stand was a bit… rickety, probably from the thin neck. This does get noticed especially with the desktop I own, tetters. The second thing I noticed was how even the picture was: good, crisp, even, vibrant. The other thing I noticed was the permeating, yellow din that I’m familiar with with LEDs. This last thing can fortunately be adjusted).

Details Value
Contrast ratio 5,000,000:1
Resolution 1920×1080
Response time 14ms
Input D-Sub, DVI, HDMI
Output Headphone
IPS Yes

I’m not sure what ips is but I think it contributes to the nice viewing angle.

Setup was real easy, took no more than a few minutes and monitor calibration was basic.

Setting Default New
Brightness 100
Contrast 50 71
Color Temp Custom Cool

The color temperature of cool does a good job to neutralize the yellow color the LED; however the custom setting did better with pictures.

Alternate (yellow LED color remains):

Setting Default Alt.
Brightness 100
Contrast 50 67
Color Temp Custom
Red 50 46
Green 50 46
Blue 50 59

Ultimately I choose to use the cool temperature.

The power button is a large half-circle with an refracting light to indicate power-saving. However, it is extremely bright. There is an option to turn it off when the power is off, but it will still be seen in standby-mode; I’ve had to cover mine up.

I got this display for reading and it is good for that. Text looks very nice. But it also is good with pictures at least as far as my semi-professional eye is concerned. The 14ms (WTG?) response time is a bit on the high end. Videos do show a bit of “ghosting” but it doesn’t bother me that bad.

Numbered file backups – Easy as can beasy

file-twoWhen I’m about to edit a configuration file or a bash script, sometimes I don’t know if the edit will work. In these cases I create a backup of it in numbered order (to keep track of them if I need to revert the changes). Because this is something I regularly do, I decided to build a script to make this easy. The usage is basic: define the file and destination-directory:

# bckfile definedfile.bash .
‘/home/johndoe/.local/bin/definedfile.bash’ -> ‘/home/johndoe/.local/bin/definedfile_00.bash’

Optionally a tag can be added too:

bckfile youdle .vault/ new
‘/home/johndoe/.local/bin/youdle’ -> ‘/home/johndoe/.local/bin/youdle_02-new’

A limitation of the script is that filename cannot have ‘periods’ in their filename. A heuristics of discovering multiple extensions and with . in the filename itself would be tough.

gt – script to create a temp file in gedit

posticon

I am really lazy with my editors. I have aliases in my shell configuration for gedit and vim that are very basic:

alias v="vim -p" # open in tab
alias sv="sudo vim -p"
alias g="bgcmd gedit"
alias sg="bgcmd gksudo gedit"

This is very nice for me because I use my editors quite a bit. One thing I needed though was a command that would create and open a temporary file in gedit. The main reason for this is that at the time I don’t know how to name or place the file properly. Another reason is that at other times I like to have a scratchpad but would lose information if it was just a New File and a crash… happened.

Running just gt will create and open a file named of the current time (MMDDhhmm) and will be saved in the trash folder. If gt is followed by a name (e.g. gt cssbox or cssbox.css) the name will be appended to the current time.

Screenshot from 2013-08-26 08:17:12

The name is helpful if wanting to dig the file out of the trash folder at a later time.

WTFM (Write the Fine Manual) with Pod2man text-converter

Note: This is a guide from a lightly-proficient manual writer that’s targeted for beginning users: it’s formatting method is easy and conversion method is basic. This method may also be helpful for those that use github because it has rendering support for this method. For a more traditional approach, nixCraft has an article for that.

Because it was the authors first effort to write a man page, talk was that it was good to use Pod2Man. Pod2man is an application that converts text using Plain Old Document (POD) formatting to traditional manual (man) pages. POD markup is easy to do and pod2man is conveniently pre-installed on most Linux distributions (as it is part of the Perl package).

An example

To get a good idea of what to do, here is a basic example demonstrating the formatting (of a fictitious program dr-smile):


=encoding UTF8

=head1 NAME

dr-smile - output platitudes to help to improve one's mood

=head1 SYNOPSIS

dr-smile [B<--happy>] [B<--joke>=F<type>]

=head1 DESCRIPTION

B<dr-smile> outputs jokes and happy thoughts designed to make one fell F<--better>.  It has the ability to output pleasant thoughts, jokes, good memories, optimistic fortunes...  B<dr-smile> I<isn't> a replacement for a professional doctor, please use responsibly and enjoy.  Additional options are available in the configuration file F</etc/dr-smile.conf>.

=over 4

=item B<-h>B<--happy>

A general happy thought will be output (e.g. C<dr-smile --happy>).

=item B<-j> F<type>B<--joke>=F<type>

A good-sense joke will be told.  The types are: F<knock> and F<oddball>.  Read more in B<jokes(1)>.

=back

Formatting

There is both text formatting and section formatting that is required for a man page.

Text formatting

All the POD text formatting tags needed to know are as follows:

Bold B<text>
Italic I<text>
Link L<http://address.org >
Filename F<file>
email E<gt>name@address.comE<lt>

Man pages use two types of formatting (that I’ve ever seen). That are implemented by POD’s bold formatting and PODS filename-or-italic formatting:

Formatting type Used for
POD Bold formatting
  • Program names
  • Options
  • Manuals
POD Filename formatting
POD Italic formatting
  • Files
  • Keywords
  • Emphasis
  • Arguments
  • Occasionally manuals

Section formatting

There are three section tags that are used in pod2man (that I know of). The pod2man manual refers to these as Command Paragraphs and they are: header/category sections, item sub-sections, and “over” sub-sub-sections for additional descriptions of item sections. References to all of these are in the above example.

  • Header/Category sections are the block descriptors and their contents (like NAME, and DESCRIPTION…).
  • Item sections refer to options generally and often override =over sections for their list item.
  • Over sections refer to option descriptions.

General layout structure

Man pages follow a basic (though liberal) structure can be well represented by this:

NAME
The name of the command, followed by a one-line description of what it does.
SYNOPSIS
A representative invocation of how to run the command and options it has.
DESCRIPTION
A detailed description of the functioning of the command.
OPTIONS
A list of it's options (these are sometimes put in DESCRIPTION).
EXAMPLES
Some examples of common usage.
BUGS
List known bugs.
SEE ALSO
A list of referenced or related commands.
AUTHOR
   Specifying the contact information.
COPYRIGHT
   Specifying the copyright information.

The order of categories varies a bit from man page to man page. The first four categories are almost always the same, however the remaining vary a bit. The above layout is an mnemonic aggregate of category order of the man pages that the author has seen.

Convert pod format to manual page

Pod2man usage is basic:

pod2man dr-smile.pod > dr-smile.1

However, these additional options will probably be wanted to be added, plus compression:

pod2man --section=1 --center="dr-smile manual" --name="dr-smile" --release="1.0" dr-smile.pod | gzip > dr-smile.1.gz

Result

screenshot-manpage

Resources

xuserrun – Run a command on the currently-active user’s X.org server display

xuserrun is a bash script to run a command on the active X.org server display.  This is primarily necessary if from within another environment  (different user, console, cron, boot script…).  xuserrun gathers DISPLAY and user environmental variables via systemd and passes them on to be able to dothis. xuserrun is designed for use with only a single user X.org server display. Running it is basic:

xuserrun xclock -digital

Tto put out a notification:

xuserrun notify-send "Hello, Dave."

It’s available for Arch users in the AUR and is also downloadable on github.

No-think link (redone)

When I first used ln, I expected the behavior of ln source link to work. But ln is slightly more complicated than that. While learning it from the beginning, I discovered it does not always resolve the path correctly; additionally, it doesn’t not error if the source is non-existence. I discovered that this was necessary because ln is designed to be versatile. So I understood that one would want links at times to be created that don’t exist yet, but it was unhelpful to me in the way that I felt ln should logically work, and the way that I wanted to use it.

Originally, I would do a link like this and it would be broken:

# ln -s ../dir/file dir/

I’ve attempted other links since then that too (of which I can’t remember now) would also be broken. So the way I discovered to do links correctly without doubt was to use whats called the fully-qualified or absolute path (i.e. a path defined from root [/]):

# ln -s /home/username/Document/file /home/username/Desktop/file

There’s a program called realpath that does this, so I wrote a wrapper script for it and it turned out to be pretty easy. The script resolves the absolute paths, verifies the existence of the source directory and link parent directory, and detects write permissions prompting sudo when needed. It works like this:

# lnk ~/Documents/Resumes/ ~/Desktop
‘/home/username/Documents/Resumes’ -> ‘/home/username/Desktop/Resumes’

By doing:

# lnk ~/Documents/Resumes/ ~/Desktop/Resumes-I-like
‘/home/username/Documents/Resumes’ -> ‘/home/username/Desktop/Resumes-I-like’

a specifically named link will be put on the Desktop.

# lnk /etc/fstab  ~  # or use ~/fstab-edit
‘/etc/fstab’ -> ‘/home/todd/fstab’

If trying to link to a write-protected directory:

# lnk /usr/share/vim/vim73/vimrc_example.vim /etc/vimrc
[sudo] password for $USER:
‘/usr/share/vim/vim73/vimrc_example.vim’ -> ‘/etc/vimrc’

An error message will appear if the source or link parent directory doesn’t exist:

Non-exist: .bashr

Limitations:

* The script will work for just about any instance with the exception of removable media where relative-paths would be better used.

Encode an Audio file to Video file

I wanted to convert an mp3 file to an mp4 or avi file because I wanted to be able to use it on my PS3 ( the PS3 has a basic audio player that doesn’t save position [ particularly troubling for large audiobooks ] ). This although is good for audio posting on youtube… I found a script by Jeremy Tharp that needed a bit of a tuneup (quoting parameters for file names with spaces, fixed exit status, better detection for the duration) and it works pretty good:

Handling display calibration

Though it is the expectation that a monitor is ready as soon as it is removed from the box, most monitors need to be calibrated. A much more vivid, detailed, true experience can become available after it is done that can be enjoyed and “feels right”. Calibrating a monitor correctly requires training of the eye so it initially can take a bit of work.

Hardware

All settings done to calibrate the monitor should be done on a hardware level (except for possibly gamma) as software solutions almost never adjust the image truely. Before beginning, have the monitor on for about ten minutes as it can take the lamp this long to warm up and represent accurate values.

Gamma

Gamma correction is the adjustment of mid-tone luminosity. It is used to compensate for the non-linear relationship between the input signal and the luminance of a monitor. Televisions, computers, and the internet use a gamma of 2.2 as a standard so monitors set to this to be able to correctly display output. Most monitors default to the 2.2 standard but some monitors deviate and therefore hardware and/or software gamma correction is required. A high gamma will look glowy and a low gamma will appear errie and dark.

Alternate

Gamma test and Alternate

There is likely a gamma setting on the monitor if it needs to be adjusted. If there isn’t, or for further adjustment, a software solution is available. The first software solution would be to use the EDID data built-in to the monitor of most modern-day computers. It contains details about the monitor including gamma correction. The Desktop Environment may have the ability to grab the EDID and save it as an ICC profile (GNOME does), otherwise a program like Quickgamma in windows will do. If the monitor does not have EDID information, Quickgamma also has the ability to manually-calibrate the gamma and create an ICC profile from that; it saves the ICC profiles to C:\Windows\System32\spool\drivers\color.

To load an ICC profile put it in ~/.local/share/icc/ and see if your Desktop Environment supports it. If it does not, a good program that can load them is xcalib.

In the image, lightly squint the eyes (or step away) to find the match where gamma blends with the background.

Contrast

Contrast defines the tonality of an image. Tonality is the gradient leveling from light to dark. With a high contrast the light and dark extremes become “crushed” or “blended” together, a low contrast the and images will appear flat. Contrast is also reflects the white-level (the brightness of white) of the monitor; contrast levels are often defined when buying a monitor because they will tell how bright the lamp is.

In this image, turn up the contrast to maximum and the reduce until all whites become distinct and the first block is just barely discernable.

Brightness

Brightness is better-referred to as black-level as it defines the “brightness of black”, or how bright darkness goes. Black is “black” or will be just above the black of the monitor if turned off. Adjust the image so that the left box just barely discernable. It may be necessary to go back and forth between contrast and brightness until the right balance is met.

Note: Discernability of the lightest light boxes and the blackest dark boxes should be possible on a modern monitor; however, it should be known that some monitors are unable to reproduce them.

Color balance

For color the first thing to do is adjust saturation. Saturation is the total amount of color the monitor will display. Too much saturation and images will be heavy with color, too little and they will appear faded. On some monitors the setting will be called Color, on others it will be Saturation, and on others it will be controled through an accumulative adjustment of the Red, Green, and Blue channels. Use the images below to determine saturation. Skin tone is a good indicator for this; however, also look at the colors on the color wheel as “bleeding” will at times occur when over-saturation occurs.

To adjust the color balance, also use the images below with skin tone as a reference. Do one color at a time, go back and forth, back and forth, until it feels right. When doing this be careful not to strain the eye too much as eye fatigue effects colorreception. Take a break after a little bit (get up and strech, make lunch…) and come back and you’ll immediately see, “Ah, the image is too red” or “Ah, the image is too blue”… The base colors Red, Green, and Blue also have complementary colors or complmentary light, the opposite of Red is Cyan, Green Magenta, and Blue is Yellow. If an image has too much Magenta it will need more Green. Again look at the skin tone (the gray in the first image works good). This is where the trained eye comes in. With practive eventually color bents will become discernable. Once it is achieved, the discovery of a well defined monitor can be begun to be enjoyed.

Skin-tone, gray background

Skin-tone, gray background

Light skin-tone

Light skin-tone

Darker skin-tone

Darker skin-tone

Resources

Firefox: Defining font type and size

roystonlodge_Alternate_Mozilla_Browser_Icon

What a professional typesetter knows is the importance of a good font. For centuries typesetters have evolved fonts to provide ease of reading that we know today. Having the text look good in the web browser is a nice bonus; choosing the right type and size can make a big difference to how well we read, especially if used quite a bit.

Font installation

To make a web page feel right — as the designer had in mind — the fonts should be on your system that a page requires. On some Linux systems, only the basic fonts are installed. Installing missing fonts usually adds nice touches that may have not been realized before. To help discover missing fonts, the Context Font add-on will display font type and size of a selected font. Many sites define their fonts as Arial, or sometimes, Verdana, or Georgia; these fonts can be added by installing Microsoft’s core fonts; a few define theirs with Apple fonts, and a few less with others. These font packages contain the most popular fonts (for Arch Linux):

arpa -i otf-bitter otf-exo ttf-bitstream-vera ttf-dejavu ttf-inconsolata ttf-lato ttf-liberation ttf-mac-fonts ttf-opensans ttf-win7-fonts

Font size

To get a good idea on good font size to use, look at a book. The size there that feels comfortable will likely feel comfortable on the monitor too. When choosing font size also think about making the various typefaces the same size (i.e. serif, sans-serif, and monospace).

Font type

Being able to define one’s own font can help readability quite a bit; however,keep in mind, tastes differ. In Firefox, the settings that can be defined are generic typefaces of: serif, sans-serif, and monospace (additionaly, the minimum font size can be set). It should be known though, that many sites still force a specific font type and size; however, gradually, a greater number of sites are using generic typeface definitions. In the future this means that personalizing fonts will be of greater availability. When choosing a font type, keep in mind to pick one that helps improve readability rather than one that grabs ones attention.

Font tests

Here is a basic test of what Firefox’s base-defined font types and sizes look like (click to view):

Font test: Type and size (based defined). (click to view)

Font test: Type and size (based defined).

Here are what a few of the common font groups look like (click to view):

Font test: Common webfonts group 1. (click to view)

Font test: Common webfonts group 1.

Font test: Common webfonts group 2. (click to view)

Font test: Common webfonts group 2.

What did I choose?

After adding all the new fonts and testing them, I found out I like a varied group; they read beautifully and scale good. Ultimately I came up with these:

Typeface Font Size
Serif Bitter 13
Sans-serif Open Sans 13
Monospace DejaVu Sans Mono 12
Minimum 10

DejaVu Sans Mono and Open Sans are good fonts and, for me, hard to beat. Rather than just define them in Firefox, I prefer to define my serif, sans-serif, and monospace fonts system-wide. This allows me to have a consistent overall look to me desktop. Here is my Fontconfig configuration (note: fonts are listed preferential/available first): fonts.conf.

For GNOME the fonts sizes are:

Setting Font
Window Titles Cantarell Bold 11
Interface Cantarell 11
Documents Cantarell 11
Monospace Monospace 10

What they look like:

Font type and size.

Font type and size.

Common webfont groups 1

Common webfont groups 1

Desktop

Desktop

Related

External monitor as Discrete

I use my laptop primarly at home with an external monitor as discrete, meaning that I have the laptop monitor turned off and I only use it. At times this is also called a dedicated monitor. GNOME can be set to disable the laptop monitor and enable the external but it wasn’t able to hotplug the monitor after I returned the laptop, and at times wouldn’t do so after resuming from sleep. Also in the proccess I discovered that the X.org server DPI setting wasn’t being done correctly and that GNOME’s text scaling needed to be adjusted. So I decided to do it in a script and it turned out to be pretty easy.

I wrote the basic script that toggles monitors depending if the external monitor is present, then it detects correct physical size dimension of the screen so the the correct DPI can be set. After this, I added a startup script (.desktop file), a pm-utils script to runafter resuming, and a udev script to detect andset the monitor when plugged in. The udev rule is generic but appears to be working for a lot of people, it relys on Kernel Mode setting (KMS) so doesn’t work for me wiht the catalyst driver, but every thing else works great. I put it on github for any who like to look at it.

The bash script cannot be used right away instead a couple bit will need to be directed:

 The package cannot be installed directly and be expected to work, some edits
 will need to be made.  First, in the resume script '80_discretemon' a username 
 will need to be defined; next, the monitor names will need to be defined as
 created by the driver in 'discretemon'.

Also, the monitors can be defined in xorg.conf but the fix for after resume from sleep, remains.

Section "Monitor"
  Identifier  "0-LVDS"
  Option      "VendorName" "ATI Proprietary Driver"
  Option      "ModelName"  "Acer Aspire Laptop Screen"
  Option      "DPMS" "true"
  Option      "TargetRefresh" "60"
  Option      "Position" "0 0"
  Option      "Rotate" "normal"
  Option      "Disable" "true"
  DisplaySize  344 194 # only works with xrandr disabled.

EndSection

Section "Monitor"
  Identifier  "0-DFP1"
  Option      "VendorName" "ATI Proprietary Driver"
  Option      "ModelName"  "Samsung SyncMaster SA350"
  Option      "DPMS" "true"
  Option      "PreferredMode" "1920x1080"
  Option      "TargetRefresh" "60"
  Option      "Position" "0 0"
  Option      "Rotate" "normal"
  Option      "Disable" "false"
  DisplaySize  476 268 # only works with xrandr disabled.
  Option      "DPI" "102 x 102"
EndSection
Follow

Get every new post delivered to your Inbox.

Join 52 other followers