Wednesday, March 30, 2011

Send email using telnet by port 25 of SMTP server with attachment

#! /usr/bin/ksh
#########################################
# send email with attachment out of KP #
#########################################
# Usage: mailout email@address
# or mailout email@address filename
#########################################
if [[ $# == 0 ]] then
echo argument number: $#
echo "Usage: mailout email@address filename"
echo " or: mailout email@address"
else
if [[ $# == 1 ]] then
SMTPserver="your.mail.server"
SMTPport=25
(
echo "helo your.unix.machine"
echo "MAIL From:"
echo "RCPT To:<$1>"
echo "DATA"
echo "To: <$1>"
echo "From: Your Name "
echo "Subject: your subject"
echo "your mail body"
echo "."
echo "quit"
) | /path/of/telnet $SMTPserver $SMTPport
else
SMTPserver="your.smtp.server"
SMTPport=25
(
echo "helo your.unix.machine"
echo "MAIL From:"
echo "RCPT To:<$1>"
echo "DATA"
echo "To: <$1>"
echo "From: Your Name "
echo "Subject: your_subject"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $2)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $2)'"'
uuencode -m $2 $(basename $2)
echo '---q1w2e3r4t5--'
echo "."
echo "quit"
) | /path/of/telnet $SMTPserver $SMTPport
fi
fi

Wednesday, March 23, 2011

debian 6 for acer aspire one

use dd if=debian6.iso of=/dev/sda to generate flash bootable net inst source.
Install with wire netcard
install wireless card by ndiswrapper and windows driver. Instead of using ifup/ifdown, or network start with interface file, should use iwconfig essid and dhclient. Put the script into /etc/init.d and link to rc2-5.d. run m-a to make it works.
For sound card, use alsactl init and reboot system.
For chinese input, install scim and rxvt-unicode.

Wednesday, March 16, 2011

Tuesday, February 22, 2011

Search whole disk content

grep -i -a -B10 -A100 'myTextFile' /dev/sda1 > myRecoveryFile.txt
vi myRecoveryFile.txt

Friday, October 22, 2010

Show short path on shell prompt

If your configuration file have

PS1='$NAME@$PWD'

and the path is deep, the line is almost covered by path name. You can just show the deepest folder by

PS1='$NAME@$(PWD##*/)'

If you need last three folder name, you need some complicated code like the following:

function ncd {
cd "$*"
PS1=$(echo $NAME@$PWD|awk -F'/' '{printf("%s/",$1);for(i=2;i< NF ; i++) {if(i",$i);}')
}
alias cd=ncd

Thursday, September 30, 2010

awk examples

data file employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

awk '{print;}' employee.txt
awk '/Thomas/
 /Nisha/' employee.txt
awk '{print $2,$5;}' employee.txt
awk '{print $2,$NF;}' employee.txt
awk 'BEGIN {print "Name \tDesignation\tDepartment\tSalary";}
 {print $2,"\t",$3,"   \t",$4,"   \t",$NF;}
 END{print "Report Generated\n--------------";
 }' employee.txt
awk '$1 >200' employee.txt
# compare variable with string using ~
awk '$4 ~/Technology/' employee.txt
awk 'BEGIN { count=0;}
$4 ~ /Technology/ { count++; }
END { print "Number of employees in Technology Dept =",count;}' employee.txt

data file: bookdetails.txt
1 Linux-programming 2 450
2 Advanced-Linux 3 300
3 Computer-Networks 4 400
4 OOAD&UML 3 450
5 Java2 5 200
awk code file: book-calculation.awk

BEGIN {
    total=0;
}
{
    itemno=$1;
    book=$2;
    bookamount=$3*$4;
    total=total+bookamount;
    print itemno," ", book,"\t","$"bookamount;
}
END {
    print "Total Amount = $"total;
}

awk -f book-calculation.awk bookdetails.txt

data file: student-marks.txt
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122 38 37 65
Edwin 2537 78 67 45
Dayan 2415 30 47 20

awk code: student.awk

BEGIN {
    test1=0;
    test2=0;
    test3=0;
    print "Name\tRollNo\t Average Score";
}
{
    total=$3+$4+$5;
    test1=test1+$3;
    test2=test2+$4;
    test3=test3+$5;
    print $1"\t"$2"\t",total/3;
}
END{
    print "Average of Test1="test1/NR;
    print "Average of Test2="test2/NR;
    print "Average of Test3="test3/NR;
}

awk code to generate HTML file: student-html.awk


BEGIN{
title="AWK";
print "\n"title"\n
Student Details"; } { name=$1; rollno=$2; avg=($3+$4+$5)/3; print " "; } END {     print "
"name""rollno""avg"
\n";
}