Dear friends!
I am wishing you all a very happy 60th independence day! India rocks!
"sare jhan se accha hindustan hamara"
I am adding an advanced bash scripting guide to the file section and
the following text for you.
To see the last 10 lines of a long file, use tail filename. To see the
first 10 lines, use head filename.
To see 50 lines
$tail -n 50 foo
$head -n 50 foo
Continuously monitor a file: e.g. monitoring a logfile.
#tail -f /var/log/messages
OR
#less /var/log/messages
and press F key
ctrl-c to interrupt continuous logging mode and q to quit, k and j
keys to scroll. Better than
tail -f huh?
Need to see your routing table? Type netstat -rn. The entry with the
G flag is your gateway.
anuj@freebsd$ netstat -nr
Routing tables
Internet:
Destination Gateway Flags Refs Use Netif
Expire
default 192.168.0.254 UGS 0 0 rl0
127.0.0.1 127.0.0.1 UH 0 0 lo0
192.168.0 link#2 UC 0 0 rl0
192.168.0.254 00:80:48:2a:aa:57 UHLW 2 1867 rl0
1200
Internet6:
Destination Gateway
Flags Netif Expire
::1 ::1
UH lo0
fe80::%rl0/64 link#2
UC rl0
fe80::250:baff:fe8b:c8b9%rl0 00:50:ba:8b:c8:b9
UHL lo0
fe80::%lo0/64 fe80::1%lo0
U lo0
fe80::1%lo0 link#4
UHL lo0
ff01:2::/32 link#2
UC rl0
ff01:4::/32 ::1
UC lo0
ff02::%rl0/32 link#2
UC rl0
ff02::%lo0/32 ::1
UC lo0
[anuj@server1 ~]$ netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window
irtt Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0
0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0
0 0 eth0
0.0.0.0 192.168.0.254 0.0.0.0 UG 0
0 0 eth0
Need to know last login details? Type lastlog OR last
root@server1~:#lastlog
Username Port From Latest
root pts/5 192.168.0.2 Sat Aug 12 01:36:23 +0530
2006
bin **Never logged in**
.
.
.
daemon **Never logged in**
adm **Never logged in**
mysql **Never logged in**
postgres **Never logged in**
anuj pts/4 192.168.0.2 Sat Aug 12 01:35:51 +0530
2006
snort **Never logged in**
sam pts/4 192.168.0.2 Sat Aug 12 01:34:49 +0530
2006
root@server1~:#last
root pts/5 192.168.0.2 Sat Aug 12 01:36 - 01:37
(00:00)
anuj pts/4 192.168.0.2 Sat Aug 12 01:35 - 01:37
(00:01)
sam pts/4 192.168.0.2 Sat Aug 12 01:34 - 01:35
(00:00)
anuj pts/3 :0.0 Sat Aug 12 01:34 still
logged in
anuj pts/2 :0.0 Sat Aug 12 01:34 still
logged in
wtmp begins Sat Aug 12 01:34:02 2006
Want to remove these logs?
root@server1~:#>/var/log/wtmp
root@server1~:#last
wtmp begins Sat Aug 12 01:43:52 2006
root@server1~:#>/var/log/lastlog
Now check the logs again:
root@server1~:#lastlog
root@server1~:#last
wtmp begins Sat Aug 12 01:43:52 2006
Switching between working directories:
use cd - command
you might already know fast switching between two directories. When
you login, there
is no $OLDPWD variable set ( check it with echo $OLDPWD ).
explanation:
[anuj@server1 ~]$ cd -
bash: cd: OLDPWD not set
[anuj@server1 ~]$ echo $OLDPWD
[anuj@server1 ~]$
This variable is changed immediately when you change a directory.
[anuj@server1 bin]$ cd ~/bin/
[anuj@server1 bin]$ pwd
/home/anuj/bin
check value of $OLDPWD
[anuj@server1 bin]$ echo $OLDPWD
/home/anuj/
Use cd - command to go to the old working directory
[anuj@server1 bin]$ cd -
/home/anuj
Now check the $OLDPWD variable again
[anuj@server1 ~]$ echo $OLDPWD
/home/anuj/bin
[anuj@server1 ~]$ cd -
/home/anuj/bin
[anuj@server1 bin]$
Want to remove all temporary files automatically every time you
logout? (under bash shell).
add this entry in your ~/.bash_logout file
# ~/.bash_logout
find /tmp -user foo -exec rm -rf {} \; >/dev/null
clear
Want to automatically logout?
Good for setting it in the .bashrc of root user in case you forgot to
logout from the root
account while working from other machine on the network.
In case if you don't want to set it up in your ~/.bashrc then you can
set it manually whenever
you logged into your server from a remote machine by setting TMOUT
variable with command
root@server1~:#TMOUT=10
root@server1~:#
This will cause your BASH terminal to terminate after if you leave
the shell idle for more
than 10 seconds.
TMOUT If set to a value greater than zero, TMOUT is treated as
the default timeout for the
read builtin. The select command termiates if input does not arrive
after TMOUT seconds when input
is coming from a terminal. In an interactive shell, the value is
interpreted as the number of
seconds to wait for input after issuing the primary prompt. Bash
terminates after waiting for
that number of seconds if input does not arrive.
You can install extra packages for FreeBSD by using the ports system.
If you have installed it, you can download, compile, and install
software by
just typing
# cd /usr/ports/<category>/<portname>
# make install && make clean
as root. The ports infrastructure will download the software,
change it so
it works on FreeBSD, compile it, install it, register the
installation so it
will be possible to automatically uninstall it, and clean out the
temporary
working space it used. You can remove an installed port you decide
you do not
want after all by typing
# cd /usr/ports/<category>/<portname>
# make deinstall
as root.
command to Mount a cdrom (FreeBSD)
#mount_cd9660 -o ro -v -s 0 /dev/acd0 /cdrom
keyboard bindings shortcuts are available to you?
[anuj@server1 ~]$ bind -p
press escapekey and then @ to complete the host name.
ctrl+x+v = display shell version
look at the established connections:
root@server1~:#netstat -taupen|grep 'ESTABLISHED'
check the packets on the network:
root@server1~:#tcpdump -neiv -i interfaceName
check the link status:
root@server1~:#mii-tool -v
colourful bash prompot:
Tip: set different colours to different host's, certainly it helps
when you work with many servers from tty's:
Red
root@server1~:PS1='\[\033[11;31m\]\u@\h\w\[\033[00m\]:\$'
Green
root@server1~:PS1='\[\033[11;32m\]\u@\h\w\[\033[00m\]:\$'
Brown
root@server1~:PS1='\[\033[11;33m\]\u@\h\w\[\033[00m\]:\$'
Blue
root@server1~:PS1='\[\033[11;34m\]\u@\h\w\[\033[00m\]:\$'
Magenta
root@server1~:PS1='\[\033[11;35m\]\u@\h\w\[\033[00m\]:\$'
and so on.
Colourful your welcome message on TTY
add this entry to /etc/issue
^[[1;31m Welcome ^[[0;39m to kernel ^[[1;32m \r ^[[1;33m on an \m
^[[0;39m
Or for the ssh login banner:
add this entry to a file e.g. /etc/ssh_banner
^[[1;31m Welcome ^[[0;39m to this ^[[1;32m educational ^[[1;34m
service
^[[0;39m
and then add an entry in sshd_config file
root@server1~:#vi /etc/ssh/sshd_config
Banner /etc/ssh_banner
restart your sshd
Unix utilities
Utility programs such as 'grep', 'awk', 'sort', 'sed' were developed
in Unix, but also have been ported to Windows/DOS.
Below are examples showing the power of these utilities for one line
commands for extracting information from text files and make changes.
Note that output of these utilities appear on the screen/xterm but
can be redirected to a file with '>' or '>>'
----------------------------------------------------------------------
----------
sort
Sort can be used to sort a data set by any field, in reverse or
direct order, in numeric or ASCII order, with leading spaces ignored
etc. It can also be used to merge and sort two data sets (of the same
fields).
sort grades.dat
# sort by first field, ascending order
sort +1 grades.dat
# sort by second field
sort -f grades.dat
# sort by first field, ascending order, treat upper/lower case as
equal
sort -b +0 grades.dat
# sort by first field, ascending order, ignore leading blanks or
spaces
sort -r grades.dat
# sort by first field, reverse or descending order
sort -n +1 grades.dat
# sort by second field, assuming second field is numeric and not
aligned
sort -n -r +6 grades.dat
# sort by reverse order on 7th field which is numeric
sort -t: tst.dat
# sort in file tst.dat which uses ':' as a delimiter
----------------------------------------------------------------------
----------
grep
The Unix utility 'grep' prints out all lines in a list of files that
matches a given pattern. As with many Unix programs, '^' stands for
beginning of line and '$' stands for end of line.
A common use of 'grep' is to find a file (or files) that contains
some 'key words' if you forget which the file name but know the
directory.
grep 512 *.dat
# search for lines with the string '512' in the files ending with .dat
# in the present directory
grep '^512' my.dat
# search for lines beginning with the string '512'
grep '310$' my.dat
# search for lines ending with the string '310'
grep '^5[0-9]8' my.dat
# search for lines beginning with the string '508', or '518', ...,
or '598'.
grep -v '^5' my.dat
# print out all lines except those that begin with 5
grep '^5[^1,6]8' my.dat
# search for lines that begin with form 5?8 where ? is not 1 or 6
grep '^52[0-9]*UE' my.dat
# search for lines that begin with 52 then have any number of digits,
then UE
grep '17 20' my.dat
# search for lines with the string '17 20'
grep -i 'extreme value' manuscript.txt
# search for lines with the string 'extreme value' ignore case (ie,
will find
# also 'Extreme value', etc.
grep -n 'extreme value' manuscript.txt
# search for lines with the string 'extreme value' and the line
numbers.
----------------------------------------------------------------------
----------
sed
sed (stream editor) is a Unix utility that can make substitutions to
certain patterns. It is similar to the use of the edits in 'vi/vim'
starting with ':'.
'sed' is a powerful tool for making the same change to many files.
sed 's/TD/AB/' my.dat
# change (first if any) occurrence on any line of TD to AB
sed 's/1/2/' my.dat
# change (first if any) occurrence on any line of 1 to 2
sed 's/1/2/g' my.dat
# change globally occurrences of 1 to 2
sed 's/[A-L]/Z/' my.dat
# change first occurrence on any line of any character from A-L to Z
sed 's/2$/0/' my.dat
# change any 2 in last position of line to 0
sed 's/\([A-Z]\)\([A-Z]\)/\2 \1/' my.dat
# For first 2 adjacent upper cases letters, reverse and put a space
in between.
sed 's/\([0-9]\)/\1 /' my.dat
# Put space after first digit
sed 's/\([0-9,A-Z,a-z]\)/\1 /' my.dat
# Put space after first letter or digit
sed 's/\([0-9]\)/\1 /g' my.dat
# Put space after every digit
sed 's/\([0-9]\)/\1\1/g' my.dat
# Duplicate every digit
sed 's/\([0-9,a-z,A-Z]*[ ]*\)/\1 \1/g' my.dat
# Duplicate every word
sed 's/\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9]\)/\3\1\2/g' my.dat
# convert dates from mm/dd/yy notation to yy/mm/dd form (for sorting
by dates)
In the last few examples a pattern is stored in \1, \2, \3 for
further use. The patterns are enclosed in \( and \)
----------------------------------------------------------------------
----------
awk
'awk' is a pattern scanning and processing language. 'gawk' is the
GNU version or enhancement of 'awk'. Check the man/info pages for a
more detailed description.
awk '{print $1,$3}' my.dat
# select variables or columns; for statistics this is probably the
most
# useful feature of awk.
awk '{print NF}' my.dat
# word count or field count by line
awk '{printf "%4d %8.4f\n", $1, $2}' my.dat
# nicer print, in C printf notation
awk '{s+=$1} END {print s}' my.dat
# sum for column 1
awk -F: '{print $1,$4}' my.dat
# field separator is ':'
awk -F"\t" '{print $1,$4}' my.dat
# tab field separator
awk -f awkfile filename
# awk commands/program are in awkfile
awk '/999/' filename
# same as grep 999 file
awk '/[[:alnum:]]/' filename
# lines with alphanumeric
awk '$2 ~/999/' filename
# lines with ...999... in field 2
awk '$2 =="999"' filename
# lines with exactly 999 in field 2
awk 'NR%2==0' filename
# print even numbered lines
awk 'print (NR%10)' filename
# print every tenth line
awk 'BEGIN {FIELDWIDTHS="5 5 5 5 2 3 2 2 9 8 8 8 8 8"} {print $10}'
filename
# print 10th field, when some fields may have missing values or no
spaces
# between them
----------------------------------------------------------------------
----------
Some miscellaneous utilities.
tac: this is 'cat' backwards, the lines are of file are printed out
starting from the last line to the first line.
find: eg.,
find . -path "*.txt"
will find all files under present directory with txt extension
paste:
paste file1 file2 > merge
paste -d" " file1 file2 > merge
Merge two files by columns, default is a tab delimiter (second
example is for a 'space' delimiter.
fmt: formating a text file
fmt filename
fmt -40 filename
The second example will format lines to have a width of 40
characters.
----------------------------------------------------------------------
----------
Usage of utilties inside vi/vim/gvim
Unix commands/scripts can be executed inside 'vi' overwriting the
space will the command is executed. For example,
:30,45!sort
will sort lines 30 to 45. To operate on a paragraph (i.e. to the next
blank line), try
!}sort
at the beginning of your paragraph (sort can be replaced by another
Unix command or script).
Usage of utilties inside emacs
Unix commands can be executed inside 'emacs', by using the 'viper'
mode for 'vi' editing, or using
M-x shell-command-on-region
(M-x means Alt with x) after highlighting or marking the region where
changes are to be made. The changes appear in a separate buffer which
can then be copied to overwrite the original.
To find emacs commands relating to 'shell', type Ctrl-h a (help with
apropos option).
to be contd....
anuj