Tuesday, November 29, 2011

How to replace string with shell metacharacters in bash

Its very tricky to replace a string containing shell metacharacters. Following is how I did it. This is not the complete list but just what was required for me

::::::::::::::
encode.sed
::::::::::::::
s/\[/START_SQUARE/g
s/\]/END_SQUARE/g
s/\}/END_CURLY/g
s/{/START_CURLY/g
s/[$]/DOLLAR_SIGN/g
s/["]/DOUBLE_QUOTES/g
s/[|]/PIPE_SIGN/g
s/[/]/FRONT_SLASH/g

::::::::::::::
decode.sed
::::::::::::::
s/START_SQUARE/[/g
s/END_SQUARE/]/g
s/END_CURLY/}/g
s/START_CURLY/{/g
s/DOLLAR_SIGN/$/g
s/DOUBLE_QUOTES/"/g
s/PIPE_SIGN/|/g
s/FRONT_SLASH/\//g

::::::::::::::
rep.sh
::::::::::::::
old='[ -n ${MY_HOME:=""} ] || source /home/${USER}/app/myvariables.sh'
new='[ -n ${MY_APPS:=""} ] || source ${MY_HOME}/myvariables.sh'

oldx=`echo "${old}" | sed -f encode.sed`
newx=`echo "${new}" | sed -f encode.sed`

cat myshell.sh | sed -f encode.sed | sed 's/'"${oldx}"'/'"${newx}"'/g' | sed -f decode.sed

Friday, November 4, 2011

How to see the memory stats of a box running linux

Following file contains all the information  about the memory of the machine running linux
       cat /proc/meminfo
The output of this command is something like

MemTotal:     74178200 kB
MemFree:      14204988 kB
Buffers:       7106472 kB
Cached:       43864060 kB
SwapCached:          0 kB
Active:       20877324 kB
Inactive:     36139244 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     74178200 kB
LowFree:      14204988 kB
SwapTotal:     2096472 kB
SwapFree:      2096196 kB
Dirty:          278512 kB
Writeback:           0 kB
AnonPages:     6045916 kB
Mapped:          71672 kB
Slab:          2836748 kB
PageTables:      38336 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:  39185572 kB
Committed_AS: 33337792 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    264000 kB
VmallocChunk: 34359473963 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB

Wednesday, November 2, 2011

How to see calendar for any month or year in Linux Bash command line

Following command returns calendar for the current month with current day highlighted in black background [tikku@tata ~]$ cal
   November 2011
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Following command returns calendar for Jan 2012.
[tikku@tata ~]$ cal 1 2012
    January 2012
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Following command returns calendar for year 2012
[tikku@tata ~]$ cal 2012
                               2012

       January               February                 March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4                1  2  3
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    4  5  6  7  8  9 10
15 16 17 18 19 20 21   12 13 14 15 16 17 18   11 12 13 14 15 16 17
22 23 24 25 26 27 28   19 20 21 22 23 24 25   18 19 20 21 22 23 24
29 30 31               26 27 28 29            25 26 27 28 29 30 31

        April                   May                   June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7          1  2  3  4  5                   1  2
 8  9 10 11 12 13 14    6  7  8  9 10 11 12    3  4  5  6  7  8  9
15 16 17 18 19 20 21   13 14 15 16 17 18 19   10 11 12 13 14 15 16
22 23 24 25 26 27 28   20 21 22 23 24 25 26   17 18 19 20 21 22 23
29 30                  27 28 29 30 31         24 25 26 27 28 29 30

        July                  August                September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4                      1
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    2  3  4  5  6  7  8
15 16 17 18 19 20 21   12 13 14 15 16 17 18    9 10 11 12 13 14 15
22 23 24 25 26 27 28   19 20 21 22 23 24 25   16 17 18 19 20 21 22
29 30 31               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
       October               November               December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6                1  2  3                      1
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    2  3  4  5  6  7  8
14 15 16 17 18 19 20   11 12 13 14 15 16 17    9 10 11 12 13 14 15
21 22 23 24 25 26 27   18 19 20 21 22 23 24   16 17 18 19 20 21 22
28 29 30 31            25 26 27 28 29 30      23 24 25 26 27 28 29
                                              30 31



How to get detailed information about the machine's CPU in Linux

You can run the following command to see all information about the processors of the machine
    less /proc/cpuinfo

Note: This file shows logical cpu's seen by the OS on the machine, i.e. the CPU shown can be
- physical CPU
- hyperthreading 
- shared (e.g. dual or quad) core


Following command will return number of physical CPU's
    cat /proc/cpuinfo | egrep '(physical id).*:'  | sort -u | wc -l

Following command will return number of shared cores
    cat /proc/cpuinfo | egrep '(cpu cores).*:' | awk -F ":" '{print $2}'   | sort | uniq

If following command returns 1 then hyper-threading is disabled on then machine, if  >1 then hyper threading is enabled on the machine
    echo $(( `cat /proc/cpuinfo | egrep '(siblings).*:' | awk -F ":" '{print $2}'   | sort | uniq` \
           / `cat /proc/cpuinfo | egrep '(cpu cores).*:' | awk -F ":" '{print $2}'   | sort | uniq` ))

To get total virtual processors available to the OS, multiply above 3 values.

How to perform an FTP transaction using Bash scripting in Linux

If you want to automate an FTP operation then you can easily use Bash scripting for that.


For example, following script gets file file.txt from ftp server


ftp -ivn ftp.internet.com<<FTP_COMMANDS
        user <username> <password>
        type binary
        cd /
        lcd /home/<username>
        get file.txt
        quit
FTP_COMMANDS

Redirections and file descriptors in bash

Following are some redirection options in bash that are not widely used but are very useful

- To redirect both stdout and stdin to a file in bash
     echo tan &> filename 
 OR
     echo tan > filename 2>&1

- To redirect stdout of a command to stderr
       echo tan 2>&1

- Following command will open a file for reading and writing and assign the interger file description N to it. If the file does not exist then it will be created
    9<>filename
Following is an example of how you can use this
     exec 9<>temp     # open file temp and assign file descriptor 9 to it
     echo aman >&9  # Write aman to the file
     exec 9>&-         # Close the file descriptor
 Now read the file and it will contain string aman
     cat temp

Before closing the file descriptor, you can see it opened

    cd /proc/$$/fd 



Closing File Descriptors
n<&-
Close input file descriptor n.
0<&-, <&-
Close stdin.
n>&-
Close output file descriptor n.
1>&-, >&-
Close stdout

How to convert a binary number to decimal in Bash

Following is how you can convert a binary number to decimal in Linux Bash
    echo $(( 2#101 ))
This  command will return
    5