Removing a file whose name begings with "-" e.g. -C

This is funny. I was doing some tar's of directories. The tar happened, but due to my mistake the tar name became -C . I was trying to move/delete the same, tried single quote/double quote/escape.. Nothing worked.Came to know about the solution from one of my colleague.

 Since the file name begins with -, commands like mv, rm treats the file name itself as a parameter(option) to the command. To nullify this affect, we have to put -- before the file name.

rm -- -C

Setting default value for a BASH variable

To set a default value for a BASH variable, the syntax is: (good for setting default value for a BASH command line parameter)

VARIABLE=${1:-DEFAULTVALUE}    #set VARIABLE with the value of 1st Arg to the script,
                                                          #If 1st arg is not entered, set it to DEFAULTVALUE

The following simple script illustrate this:

tmpdir=/tmp
defvalue=1

DIR=${1:-$tmpdir}   # Defaults to /tmp dir.
VALUE=${2:-$defvalue}           # Default value is 1.

echo $DIR
echo $VALUE


Now while running the script, specify values for both the arguments.
$ ./defvaue.sh /dev 23
/dev
23

This time don't mention their values.
$ ./defvaue.sh
/tmp
1


so

DIR=${1:-$tmpdir}
VALUE=${2:-$defvalue}


is a replacement for the following:

[ -z $1 ] && DIR="/tmp"
[ -z $2 ] && VALUE=1

dialog - display dialog boxes from shell scripts, Its uses

"dialog"  utility is superb for developing "small" front-ends.  I was aware of it , but never tried it.  Some days ago I came up with some small applications using it, those looks pretty good(as per my friends views). Here I will discuss about how to program with "dialog" in BASH.
To start with, first lets discuss some of the basic controls available in "dialog", later using them we will develop a simple application.

A copy-paste from man pages of dialog:

Usage: dialog --clear
       dialog --create-rc <file>
       dialog [--title <title>] [--separate-output] [--backtitle <backtitle>] [--clear] <Box options>

Box options:

  --yesno     <text> <height> <width>
  --msgbox    <text> <height> <width>
  --infobox   <text> <height> <width>
  --inputbox  <text> <height> <width> [<init>]
  --textbox   <file> <height> <width>
  --menu      <text> <height> <width> <menu height> <tag1> <item1>...
  --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --guage     <text> <height> <width> <percent>
  --file      <dir>  <height> <width> <mode> [<init>]


CHECKLIST

dialog --checklist "Choose OS:" 10 40 5 \
        1 Linux on \
        2 Solaris off \
        3 "HP UX" off \
        4 AIX off

INPUTBOX

dialog --title "Inputbox - Example" --backtitle "www.jaduks.livejournal.com" \
       --inputbox "Enter your favourite OS here" 8 60


MENU

dialog --title "A dialog Menu Example" \
        --menu "Please choose the command:" 15 55 5 \
        "nestat -r" "Display the kernel routing tables" \
        "netstat -a" "listening / non-listening sockets" \
        "/sbin/ifconfig"  "configure a network interface"



MSGBOX

dialog --title "Example Dialog message box" \
       --msgbox "\n Installation Completed on 172.22.23.124" 6 50

RADIOLIST

dialog --backtitle "Flims" \
  --radiolist "Select Flim:" 10 40 3 \
        1 "Life is beautiful" off \
        2 "Beautiful Mind" on \
        3 "Walk in the clouds" off

Find all files between two dates

To print all the files from 19-10-2007 to 21-11-2007

$ find <DIR> -type f -exec ls -l --time-style=full-iso  {} \; | awk '{print $6,$NF}' | awk '{gsub(/-/,"",$1);print}' | awk '$1>= 20071019 && $1<= 20071121 {print $2}'

And to print all files from 2nd Nov 2007 to today, (Using -mtime with find command, and calculating the mtime based on the epoch times.)

$ FROM=`date --date='11/2/2007' +%s`
$ TO=`date +%s`


$ find /home/jsaikia/harness/rough/ -type f -mtime -`echo "(((($TO - $FROM ) / 24) / 60) / 60)" | bc`  -print

If anyone got any better idea, please put in the comment section below.

Pushing data to Mysql using BASH

Assume your application generates some output in a file named "fan.out" in the following format.

FAN_NAME|TIME|RPM|STATUS
FAN1|1195699322|4566|Moderate Condition
FAN12|1195699112|3562|Bad Condition
FAN11|1195699321|5676|Good Condition
FAN15|1195699117|8923|Good Condition
..
..



You want these data to be automatically pushed to your mysql "FAN.DETAILS" table through a BASH script.

This is how I did that.

Step1: Created the database and table in mysql as below.


so the db is ready.

Step2: From the above fan.out, my idea for creating the corresponding .sql

$ sed -e 1d -e 's/|/","/g'  -e 's/^/insert into FAN.DETAILS values ("/' -e 's/$/");/' fan.out > /tmp/fan.out.sql
insert into FAN.DETAILS values ("FAN1","1195699322","4566","Moderate Condition");
insert into FAN.DETAILS values ("FAN12","1195699112","3562","Bad Condition");
insert into FAN.DETAILS values ("FAN11","1195699321","5676","Good Condition");
insert into FAN.DETAILS values ("FAN15","1195699117","8923","Good Condition");

Step3: Here my script "push2mysql.sh" is ready

#!/bin/bash
#http://jaduks.livejournal.com/

SQLFILE=/tmp/fan.out.sql
trap "rm -f $SQLFILE" EXIT

MYUSER=root
INPUTF=./fan.out
TABLE="FAN.DETAILS"

sed -e 1d -e 's/|/","/g'  -e 's/^/insert into '"$TABLE"' values ("/' -e 's/$/");/' $INPUTF > $SQLFILE
echo "quit" >> $SQLFILE

/usr/local/mysql/bin/mysql -u $MYUSER  < $SQLFILE
echo "Completed"



<A Sample RUN>
$ ./push2mysql.sh
Completed

<FROM MYSQL>

Hope you like this post :-) Got to go to office, getting late.

Transpose of a matrix : BASH way of doing

A one liner for generating of a transpose of a matrix.


$ echo -  | awk '{print "1\t2\t3\n4\t5\t6\n7\t8\t9\n"}'
1       2       3
4       5       6
7       8       9

$ echo -  | awk '{print "1\t2\t3\n4\t5\t6\n7\t8\t9\n"}' | awk '{for(j=1;j<=NF;j++){arr[j]=arr[j]"\t"$j}} END {for(i in arr) print arr[i]}' | sed 's/^\t//'
1       4       7
2       5       8
3       6       9

Colors in BASH

BASH got this feature as well, background and foreground color options for text. Here is a short discussion on the color codes and their use.





Note:
echo -e '\E[COLOR1;COLOR2m THE TEXT.'
tput sgr0 = > Reset text attributes to normal without clear. 

The Following BASH code will print all the color combinations:

#!/bin/bash

for i in `seq 30 37`
        do
        for j in `seq 40 47`
                do
                echo -e '\E['$i';'$j'm TEXT'
                tput sgr0
                done
done

A CHESS board

Using BASH, here I present before you a chess board, :-) 



The Code for generating the above chess board is :

#!/bin/sh

for m in `seq 8` ### Outer for loop ###
        do
                for n in `seq 8` ### Inner for loop ###
                        do
                                T=`expr $m + $n`
                                S=`expr $T % 2`
                                        if [ $S -eq 0 ]; then
                                                echo -e -n "\033[47m " ### white ###
                                        else
                                                echo -e -n "\033[40m " ### black ###
                                        fi
                        done
                echo -e -n "\033[40m" #### background colour=black ###
                echo "" #### printing new line ###
        done

 

Add/Change/Insert lines using sed


 Using sed we can  Add/Change/Insert lines in a file, I found it very useful, hope you too !

$ cat namedb.txt
Nina:London
Apen:India
Lokesh:India

#Add a line
a)
$ sed '
/Apen/ a\
Add this line after Apen
' namedb.txt

<Output>
Nina:London
Apen:India
Add this line after Apen
Lokesh:India

b)
$ sed '
2 a\
Add this line after 2nd line
' namedb.txt

<Output>
Nina:London
Apen:India
Add this line after 2nd line
Lokesh:India

#Insert a new line before
c)
$ sed '
/Apen/ i\
Insert this line after Apen
' namedb.txt

<Output>
Nina:London
Insert this line after Apen
Apen:India
Lokesh:India

Similary one can mention the line number (case b above)

#Change a line

d)
$ sed '
/Apen/ c\
Change the line with Apen to this line
' namedb.txt

<output>
Nina:London
Change the line with Apen to this line
Lokesh:India

Similarly one can mention the line number to change (case b above)