Category Archives: Command Line

Command Line Calculator

I can usually type faster in the terminal than doing mouse click on a gui calculator, so I created this scipt to be able to do it quickly from the terminal. There are alot of command line calculators out there so use the one you are comfortable with but I like using bc because of the syntax. For example, you can type:

calc "6/(3*10)"

or something more complex:

calc "8^2*(10/2+(13.2032565*2030)/.349548)" 100

100 is optional, it will specify how many decimals you want to carry it out to (the default is 4).

HTML Entities from the Command Line

While doing HTML work I tend to do my work with text editors. For this, I use Arachnophilia a Java HTML editor with easy, editable, customizable tags (Review here).

Arachnophilia has support to convert characters to HTML entities but isn’t easy to get to (HTML > More Functions > Char to Entity. There are various web sites that do but if willing to use the terminal they can be quickly gotten there as well. Thanks to script by Darren this can be done easily. It requires script Perls’ HTML::Entities module to do so (for help installing Perl modules look at this page). You’ll probably need redirect the script to point to the Perl program proper:

whereis perl

More than likely its in /usr/bin/perl. After fixing that run the script. This will put you in a sub-shell that you can copy and paste characters to be encoded:

You can also convert a whole file. This will print to standard output (terminal text):

htmlentities filename

Or convert a file by doing:

htmlentities  < file > convertedfile

Installing Perl Modules Manually

HeaderIf you do Perl programming or if a program you have needs a perl module, you could download and compile it manually but the easier way would be to use CPAN (the Comprehensive Perl Archive Network).

CPAN

First thing you should do is see if your distribution has it in it’s packages repository so that it can be easily added/removed. If it isn’t, be sure you have perl installed and start the cpan shell:

perl -MCPAN -e shell

Then upgrade the local CPAN module database:

install Bundle::CPAN
reload cpan

Then to install (for example HTML::Template):

install HTML::Template
exit

Once the database is downloaded you don’t need to use the shell anymore to add a module:

perl -MCPAN -e 'install HTML::Template'

Vi(m) Reference Card

I use the Vi Reference Card all the time but I seem to have lost my copy and since my printer is broke I decided to make an html version of it:

Vi(m) Reference Card

Backup Configurations with tar Helpers

Update: This article has been supplanted by The Beauty of rsync and Backup Script.

When I have to do a reinstall, sometimes I have to install from scratch – doing a clean install is just sometimes necessary. My configurations are priceless to me and after my reinstall I restore them from a backup copy. Here’s how I quickly add my configurations to an include file and back them up on a regular basis done with a basic script.

Basic tar Command

A good number of GUI programs can do this (you can read about the ones I’ve looked at here), but they seem to make the process more complicated, so I went back to tar and created a script. And it makes adding and subtracing files easy. For those new to tar the basic archive command is:

tar -c --xz -f <backupname>.tar.xz /folder/file /folder ...

Include/Exclude Files

By putting the archive command in a bash script it can be used later. Files and folders can be appended to the command in the script but for multiple files consider using include and exclude files:

#!/bin/bash

tar --files-from=include.txt --exclude-from=exclude.txt -c --xz -f backupname.tar.xz

Include/exclude files contain the path on a line of what or what-not to backup. The include file should list the full path and cannot use regexps but the exclude fine can:

/home/*/.local/share/Trash/files
/home/*/.Trash

Add Paths to Include/Exclude Quickly

To add paths to the include/exclude files readlink can be used. Adding to the include and exclude files can be done like this (using the script below):

$ bckcfg i /etc/rc.conf 
 Added "/etc/rc.conf" to bckcfg include file.
$ bckcfg e ~/.thumbnails/
 * "bckcfg" doesn't check if patch is correct because the exclude file can
 contain regexps.  Be sure the path is correct (e.g. '/mnt/win/*')
 Add "/home/todd/.thumbnails/" to bckcfg exclude file? (y/n): y
 Added "/home/todd/.thumbnails/" to bckcfg exclude file.

Creating notes of backups can be done by:

$ bckcfg n Added font config
$:cat bckcfg-not.txt | tail -n 2
20xx-08-14-02:52:23 PM: Added dhcp cacher config
20xx-08-14-02:52:33 PM: Added font config

The Backup Script

To backup do:

bckcfg c

The script names the backup by several definable variables and removes old backups if desired.

Consider putting it in a cron job to get regular backups.

o/

Restore Settings on a Broken Firefox

Update: 09-29-11 – Using script to automate process, see end of post.

When people have a issue with Firefox I’ve seen many people will resort to deleting their old profile (or folder) and creating a new one. This works but doing this will get rid of any passwords, history, bookmarks… therein. Having used Firefox quite a bit creating a new profile from time to time is a good idea anyhow as cruft, bad extensions, … can slow down browsing.

Manually

Copying the Firefox configs can be done by:

cd ~/.mozilla/firefox/

Backup the old profile and profile list:

mv xxxxxxxx.default{,.bck}
mv profiles.ini{,.bck}

Create a new profile:

firefox -CreateProfile <profilename>

This command will return the name of the new folder. Copy the basic settings to the new profile:

cd *.default.bck
cp places.sqlite key3.db cookies.sqlite mimeTypes.rdf formhistory.sqlite signons.sqlite permissions.sqlite webappsstore.sqlite persdict.dat content-prefs.sqlite ../*.<profilename>

This will transfer the bookmarks, browsing history, form entries, passwords, personal dictonary changes, and page zooms. There might be a couple other things wanted to add (possibly your firefox preferences), take a look at Transferring data to a new profile for more information.

Getting Help from Console

If you’re in console (aka virtual console) doing an install or repairs on a system, it’s good to know how to get help if problems occur.

“Ground Control…”

To get help in console you can use a chat client. Read this page on how to set up irssi – a terminal/console IRC program. The guide will walk you through setting up irssi and connecting to freenode where many Linux distribution chat channels are located.

“Waiting for details, Houston…”

When you tell the people in the chat-room what your problem is, sometimes they will need to know additional information. This could be the output of a command or the contents of a configuration file. To do a command without leaving irssi do Ctrl+Alt+F2 (F3, F4… can also be used) to enter another console, then enter the command.

Be better not to have to write everything down on a notepad and then type it into irssi, this is where it becomes useful to use a collaborative debugging tool like pastebin. Pastebin is a website that temporarily holds configurations, bug outputs… that you can refer other people to get help. There are several tools that can be used from the command line that can send files to a pastebin service, for example pastebinit. Add pastebinit from your distro, then upload a file. For example, your xorg.conf file:

pastebinit /etc/X11/xorg.conf

For uploading the output of a command, first you have to put it into a file:

sudo fdisk -l &> partitions.txt

&> will redirect all output to a text file (both standard output and error output) and now it can be uploaded.

“I have visual…”

Occasionally you might need to actually show a picture of what your question is about (e.g. if you have a question about a console-based installer). For this you can use fbshot. fbshot is a framebuffer screenshot program. To take a screenshot of the first console (Ctrl+Alt+F1):

fbshot -c 1 console1.png

Then you can use a console web-browser and a image-hosting website to upload the image.

Customize man page colors with ‘less’ definitions

Man pages by default use less for displaying. I’ve used vim before to for colored text in man pages but something got bjorked in an update. To have color with man pages termcap will need to be invoked. Thanks to nico for the tip.

All that needs to be done is to export bold and underline values of termcap. Adding the values to the ~/.bashrc will make sure that they are always used:

# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m'       # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m'  # begin bold
export LESS_TERMCAP_me=$'\E[0m'           # end mode
export LESS_TERMCAP_se=$'\E[0m'           # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m'    # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m'           # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline

And source the ~/.bashrc to have it work:

source ~/.bashrc

Notice I used Arch and Gentoo colors, my two favorite distros :) :

Updating BIOS with Linux

If you don’t have Windows installed and you need to upgrade your BIOS, Linux does have the tools to be able to create a BIOS flash CD. Not many companies make Linux flash utilities and alot of these utilites are DOS utilities so a bootable DOS disk is needed. This is a simple, easy way to create a BIOS flash CD.

First, get a BIOS image. You’ll need to download a BIOS image for your board. For information on what Flash utility to use, a good place to look is your computer manufacturers homepage. Award BIOS and American Megatrends BIOS are the most popular BIOS’s used on motherboards.

Editing FreeDOS Minimal Boot Image

Note: This didn’t work for me but plenty of people have had success with it, fdboot.img is a bit old and may not work on newer hardware. Look at flashrom below for another alternative.

FreeDOS provides a bootable DOS image. Download the DOS image to the Desktop:

wget http://www.ibiblio.org/pub/micro/pc-stuff/FreeDOS/files/distributions/1.0/fdboot.img

and mount it:

sudo mount -t vfat -o loop /home/user/Desktop/fdboot.img /media/ISO

The BIOS flash utility and BIOS image will need to be added to the FreeDOS image. I prefer to use /media/ISO but any empty directory will do. The bootable image has a fixed size (1,440 Kb, the size of a floppy disk) and hence /media/ISO will also have that limited memory. The size needs to remain fixed in order to create a bootable floppy of it. You can see the space used in the image by:

du -b /media/ISO

Add the flash utility DOS executable and the BIOS image (there should be just enough room for it). I prefer to put these in a new directory but it’s up to you.

cd /media/ISO
mkdir bios
cp /home/user/Desktop/flashprog.exe /home/user/Desktop/bios-image /media/ISO/bios

The data added to the FreeDOS image will be saved when the ISO is unmounted:

sudo umount /media/ISO

Now return to the Desktop and convert the appended FreeDOS image to a bootable ISO:

mkisofs -r -b fdboot.img -c boot.cat -o fdboot-bios.iso fdboot.img

The -b option defines the floppy image used for booting; the -c option will create a boot.cat file that directs to fdboot.img and is necessary for booting; the -o option defines the output file, in this case a bootable iso; and finally the image file needs to be added.

Now burn the iso to the CD/DVD however you like. For example, from the command line:

cdrecord fdboot-bios.iso

Flash BIOS in Linux with Flashrom

Flashrom is a utility to directly flash the bios directly in Linux. It’s design to be a comprehensive utility and supports a good number of hardware devices. Above that, flashrom is easy to use. Check their page for compatibility, or install flashrom and see if it recognizes your chipset. I’d tell more but the flashrom website does a good job of telling about the utility. I also updated the Gentoo ebuild for flashrom.

Questions

Because BIOS sizes are getting larger, we may need to learn how to create larger bootable images. mkisofs mentions that is can create an El Torito (bootable) iso with either 1200 Kb, 1440 Kb, or 2880 Kb images. I know how to create an empty vfat image can be created with:

mkfs.msdos -C newimage.img 2880

And, of course, it can be mounted and the FreeDOS files can be copied there, but how could we make it bootable?

Resources

Color Output on Bash Scripts (Advanced)

I talked in a previous post about basic bash script colored output using the tput command. The tput command works for basic coloring (providing seven colors to choose from) but ANSI also provides a 256 color palette.

Note: Not all terminals support ANSI, but most do.

ANSI color coding is in this form:

\033[01;38;5;160m

The ANSI sequence: {ESC}[{attr};{bg};{256colors};{fg}m

{ESC} or \033 represents the ANSI escape-sequence. {attr} represents the outputs attributes (properties such as blinking and bold text), {fg} is the foreground color, {bg} is the background color, m means the sequence ends.

Note: The escape-sequence \033 works fine but at times you might have to use \e.

An example:

echo -e "My favorite color is \033[38;5;148mYellow-Green\033[39m"

The variable -e is required because echo doesn’t normally interpret backslashes and 033[39m tells bash to end the seqeunce. The 38 Value will use no background color. Notice too that I omitted the attribution value which if isn’t used will use the default (regular text) value.

To get color values a good program is colortest.

colortest -w

Colortest will show the ANSI color value to the corresponding hex value. Then just insert the ANSI value into the either the foreground or background value.
There’s also a program called conv-rgb2xterm that a hex value can be put in and it will give and ANSI sequence for the foreground color.

References

Color Output on Bash Scripts

Users who have been using Linux for awhile often learn that creating a basic script is a good way to run multiple, often-repeated commands. Adding a little color to scripts can additionally provide nice feedback. This can be done in a fairly straight-forward way by using the tput command.

A common way of doing this is to define the colors that tput can produce by putting them at the beginning of the bash script:

#!/bin/bash
# scriptname - description of script

# Text color variables
txtund=$(tput sgr 0 1)          # Underline
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #  red
bldblu=${txtbld}$(tput setaf 4) #  blue
bldwht=${txtbld}$(tput setaf 7) #  white
txtrst=$(tput sgr0)             # Reset
info=${bldwht}*${txtrst}        # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}

When writing new scripts using templates with these variables already defined can quicken the creation process and help keep scripts organized (my Bash Templates).

If just needing to use tput colors for specific instances this script can display the tput definitions and their corresponding possibilities:

#!/bin/bash
# tputcolors

echo
echo -e "$(tput bold) reg  bld  und   tput-command-colors$(tput sgr0)"

for i in $(seq 1 7); do
  echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0)  \$(tput setaf $i)"
done

echo ' Bold            $(tput bold)'
echo ' Underline       $(tput sgr 0 1)'
echo ' Reset           $(tput sgr0)'
echo

To use additional colors see: Color Output on Bash Scripts (Advanced).

Follow

Get every new post delivered to your Inbox.

Join 57 other followers