18 Mar 2011

How little you know, and some useful commands for the week 

By - Quick Tip 5 Comments

Early in my career, I was writing a shell script and needed to print a line-number for each line in a text file. I ended up coming up with some function that did just what I needed.

I don’t recall the exact method, but it was something like:

cat file | while read line
do
    lno=$[${lno:=0}+1] 
    echo $lno $line
done

I was quite happy with the solution, until I realized that I could have just run ‘cat -n file’.

A few takeaways:

A better solution already exists, keep looking.
The more you learn, the more you realize how little you know

Every week, I still find new commands, new methods, and alternate ways to accomplish things. Below are a few items I found this week that you may also find helpful.

Connect directly to your serial console, and log the session.
# screen -L /dev/ttyS0 9600

Use ‘mtr’ when debugging network issues:
$ mtr www.google.com

Re-run a command changing a parameter quickly:
$ ls -l 500.*
-rw-r--r-- 1 nwilkens nwilkens 0 2011-03-13 11:32 500.lst
-rw-r--r-- 1 nwilkens nwilkens 0 2011-03-13 11:32 500.txt
$ ^500^600
ls -l 600.*
-rw-r--r-- 1 nwilkens nwilkens 0 2011-03-13 11:32 600.lst
-rw-r--r-- 1 nwilkens nwilkens 0 2011-03-13 11:32 600.txt

Return your current IP address:
$ curl ifconfig.me
n.n.n.n

Typing a long command on the command line? Convert it to your favorite editor using:
cntrl-x cntrl-e

Update the default editor on Ubuntu.
$ sudo update-alternatives --config editor
There are 6 choices for the alternative editor (providing /usr/bin/editor).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/ng 80 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
3 /usr/bin/emacs23 0 manual mode
4 /usr/bin/ng 80 manual mode
* 5 /usr/bin/vim.basic 30 manual mode
6 /usr/bin/vim.tiny 10 manual mode

Press enter to keep the current choice[*], or type selection number:

Watch Star Wars via telnet
$ telnet towel.blinkenlights.nl

5 Responses to “How little you know, and some useful commands for the week”

  1. says:

    Wow, good stuff. Just the other day I hacked up a fairly long and ugly script to feed one line at a time into a series of commands; knowing about `read` would have made things so much easier…

  2. linuxbasiccommand says:

    Thanks! very good arcticles

  3. dave jarvis says:

    Bad example:

    $ ls -l 500.*
    $ ^500^600

    Fewer keystrokes (using bash):

    $ ls -l 500.*
    Up-arrow, control-left, delete, 6

  4. Harsh says:

    The day I realized a similar thing, I got myself the entire coreutils’ docs in one file, and read it like a book on my phone.

  5. maht says:

    larval stage (n) [Jargon File 3.0.0]: Describes a period of monomaniacal concentration on coding apparently passed through by all fledgling hackers. Common symptoms include the perpetration of more than one 36-hour hacking run in a given week; neglect of all other activities including usual basics like food, sleep, and personal hygiene; and a chronic case of advanced bleary-eye. Can last from 6 months to 2 years, the apparent median being around 18 months. A few so afflicted never resume a more ‘normal’ life, but the ordeal seems to be necessary to produce really wizardly (as opposed to merely competent) programmers. A less protracted and intense version of larval stage (typically lasting about a month) may recur when one is learning a new OS or programming language.

Leave a Reply