Common Useful Examples
(May 2006)

Table of contents

find + exec

sed

gnuplot

awk

dialog

dc

cut

netcat

.exrc

xterm

Making a filesystem inside a single file

find + exec

find /tmp -exec ls -ld {} \;

sed

The following can be used for auto-extract installs:
#!/bin/sh

sed '1,/^# begin/d' $0 > /tmp/extracted_file
exit

# begin
xxxx
yyyyyy
etc...

gnuplot

shell> gnuplot

gnuplot> plot sqrt(x)
gnuplot> set xrange [0:5]
gnuplot> plot exp(x)
gnuplot> plot "-"
1 1
2 4
3 9
<CTRL-D>

awk

awk 'BEGIN { print "starting..."; }
{ print $2 " and " $4; }
/^1/ { print "line begins with 1"; }'

dialog

answer_file=`mktemp /tmp/answer.XXXXXX`
dialog --backtitle "Main screen" --title "input box" --inputbox "enter your name" \
	0 0 "myname" 2>$answer_file
if [ $? -ne 0 ]; then
	exit 1
fi
answer=`cat $answer_file`
rm -f $answer_file
dialog --backtitle "your name is $answer" --title "welcome" --msgbox "hello $answer" 0 0

dc

echo "6 k 23 45 / p" | dc
k sets precision to 6 decimals.
p shows the value on top of the stack.

cut

ls -l | cut -b 16-22
This shows characters 16 to 22 of each line.

netcat

You can make two xterm windows dialog with the two following commands:
The listening part (in the first xterm):
netcat -u -l -p 12345
The client part (in the second xterm):
netcat -u localhost 12345
netcat can generally be used to communicate on a network socket (TCP or UDP).

.exrc

Sample .exrc file (for vi configuration):
se ic
se autoindent
map #1 :!date^M
map #2 :se number^M
map f I/*^[A*/^[
map F :.s/\/\*//^M:.s/\*\///^M
^M means 'end-of-line' and can be obtained in vi by hitting control+V, Enter.
^[ means 'ESC' and can be obtained in vi by hitting control+V, ESC.

xterm

xterm -cc 46:1,47:1
The -cc option here specifies that double-clicking selection stops at the "." (ascii code in decimal is 46) and "/" (47) characters.
The "1" is a character class chosen randomly (it must be different from the class used for the letters and digits).

Making a filesystem inside a single file

shell> dd if=/dev/zero of=disk-image count=1000
shell> /sbin/mkfs -t ext2 -q disk-image
disk-image is not a block special device.
Proceed anyway? (y,n) y
shell> mkdir mydir
shell> mount -o loop=/dev/loop0 disk-image mydir
This can be useful for the testing of some program with no space left on the disk.