|
|
Note: The archives category content is an automatically generated focus channel and does not neccessarily reflect the opinions of this blog. No responsibility is taken for the external links presented here, follow at your own discretion. The archives content is never scraped from sites - but an abstract obtained from search engines.
Category: 'UNIX Coding Training'
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
The binary calculator, installed with pretty much all version of UNIX, can be used for an array of tasks. Not just basic calculations, but also converting between bases and it can automated, etc.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
It's simpliest use, it just to run bc and add, subtract divide. bc 888+999 1887 Be aware though, that to perform precision point calculations, you must specify the "-l" long option. For example: bc 999/222 4 Obviously wrong! Now with the long option: bc -l 999/222 4.50000000000000000000 Can also be automated like this, to convert between decimal and hex: i=0; while ((i < 20)) ; do echo -n ' $i: '; echo 'base=10;obase=16;$i' | bc; ((i++)); done | paste - - - - 0: 0 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: A 11: B 12: C 13: D 14: E 15: F 16: 10 17: 11 18: 12 19: 13
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Started using vi probably 13 years ago and ever so often, still learn new tricks with it! Some of my favorites are: turning on control characters; mapping function keys; performing global search and replace; remapping columns.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
Editing a file from a specific line number or pattern: vi +lineno filename vi +/patt/ filename To turn on control characters, once in vi - do the following (exactly) :set list To turn it off again: :set nolist Global replace: :%s/patt/rep/g Map function keys: :map #1 :w![ctrl v ctrl m] that is type colon,map,#,1,space,colon,w,!,^M then hit enter. More on this later. Remap columns: :%s/\(col1patt\) \(col2patt\)/\2 \1/ To finish here is a cool but slightly bizarre one: :g/^/m0 It reverses the lines of file. Effectively it says - global (all lines) with this pattern (a beginning line - so all lines), move to 0 - which is shorthand for the current line. So as vi applies the command to each line, it bubbles up to the top.
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Ever wanted to display a line number, with each matching record from awk. Useful as a way of counting the number of matching lines too.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
Here’s how with a demo which grabs network stats (netstat) off my linux box. It looks for connections to port 80 and in TIME_WAIT state - prints a line number and the matching line.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
[marcus@bree marcus]$ netstat -an -t | awk ' /\:80 / && /TIME_WAIT/ { print ++i,$0} ' 0 tcp 0 0 x.x.x.x:80 x.x.x.x:44154 TIME_WAIT If you wanted the line number, that matches your pattern - this would show that too: [marcus@bree marcus]$ netstat -an -t | awk ' /\:80 / && /TIME_WAIT/ { print "line:"NR,"count:"++i,$0} ' line:17 count:1 tcp 0 0 x.x.x.x:4298 x.x.x.x:80 TIME_WAIT
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Most shells (zsh, ksh, bash …) can either run with a minus x (-x) option or by specifying set -x. If you have an existing shell, that you’d like to run in debug mode - just run it like this: bash -x scriptname. Or ksh -x for korn shell.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
You can also edit the follow and put a minus x after the interpreter line, for example: #!/bin/ksh -x or #!/bin/bash -x Debugging on the command line can be performed, simply by typing set -x. Like this: $ set -x + set -x $ date + date Tue Jun 13 18:59:44 WST 2006 $ echo "hello world" + echo 'hello world' hello world $ set +x + set +x $ date Tue Jun 13 18:59:57 WST 2006
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
To do this you first need to know the Process ID (PID) of the cron process, then to list all children of that process. Luckily Solaris has ptree and pgrep, so it is simple.
|
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Just use the time command. Really simple and an excellent way to see how long your scripts take. Plus showing the anticipated load on the system.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
[marcus@bree marcus]$ time ps PID TTY TIME CMD 27973 pts/0 00:00:00 bash 28120 pts/0 00:00:00 ps real 0m0.025s user 0m0.010s sys 0m0.020s
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
There are a number of ways to search the process tree. Here are a few: Show all processes for user apache. ps -fu apache Show all process on a given terminal (try command tty to see your own) ps -ftpts/0
|
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
You can also display the line number it occured on (-n) and ignore case (-i): grep -n -i "pattern" filename Also you can match more than one pattern: egrep "patternA|patternB" filename To match the beginning of a line (^) or the end of line ($) - this will only match lines with this specific pattern on: grep "^patternA$" filename We can also use wildcards and ranges - this will match any line with PATTERN in it followed by any amount of numbers: grep "^.*PATTERN[0-9]*$" filename
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Once you have your line number - see previous post on grep, we can extract lines around the pattern. To do this sed (stream editor) can be used to print just desired lines - this says don’t print all lines (-n); start at line 456 and finish at line 466 - print: sed -n 456,466p filename
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
Also with sed, we can say delete specific lines - in this case remove lines 5 to 10: sed 5,10d filename That's not all sed can accept patterns, as start/end identifiers: sed /start_pattern/,/end_pattern/d filename
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
So to replace all occurences of Unix with UNIX: sed 's/Unix/UNIX/g' filename We can also replace patterns, with variable values, like this: sed "s/PATT/${THEVAR}/g" filename
|
No Comments »
|