Tuesday, October 28, 2008

How to trim leading and trailing spaces in a string?

To trim leading spaces
string=`echo $string | sed -e 's/^ *//g'`
echo string=$string

To trim trailing spaces
string=`echo $string | sed -e 's/ *$//g'`
echo string=$string

Converting a string from upper case to lower case (in Unix)

curDate=`date "+%d-%h-%Y"`
echo curDate=$curDate
curDateInUpperCase=`echo $curDate | tr "[:lower:]" "[:upper:]"`
echo curDateInUpperCase=$curDateInUpperCase

Display date in 'DD-Mon-YYYY' format in unix

Command: date "+%d-%h-%Y"
Output: 28-Oct-2008

Wednesday, October 22, 2008

How to send email with attachments using mailx utility?

Here is a code stolen from orafaq.com that helps to send email with attachments using mailx utility (with slight modifications, ofcourse)
#!/bin/ksh
# -----------------------------------------------------------------------
# Filename: mailx.ksh
# Purpose: Demonstrates how one can attach files when sending E-Mail
# messages from the Unix mailx utility.
# Author: Frank Naude, Oracle FAQ
# -----------------------------------------------------------------------
SUBJECT="Send mail from Unix with file attachments"
TO="address1@domain.com address2@domain"
echo "Send the E-mail message..."
/usr/bin/mailx -s "$SUBJECT" "$TO" <<-EOF
Hi,
This sample E-mail message demonstrates how one can attach files when sending messages with the Unix mailx utility.

First attachment: mailx.ksh (this script)
Second attachment: /etc/passwd file

Best regards
your name

~< ! uuencode mailx.ksh mailx.txt
~< ! uuencode /etc/passwd passwords
~.
EOF
echo "Done!"

When given a pathname, how to delete any prefix up to the last slash ('/')?

basename is a standard UNIX computer program.
When basename is given a pathname, it will delete any prefix up to the last slash ('/') character

Usage
basename string [suffix]
string
A pathname
suffix
If specified, basename will also delete the suffix.


Example
$ basename /usr/home/jsmith/basename.wiki ki
basename.wi

Difference between TRIMSPOOL and TRIMOUT

SET TRIMSPOOL ON
Determines whether SQL*Plus allows trailing blanks at the end of each
spooled line. ON removes blanks at the end of each line, which may
improve performance especially when you access SQL*Plus from a slow
communications device. TRIMSPOOL ON does not affect terminal output.
SET TRIMOUT ON
Determines whether SQL*Plus allows trailing blanks at the end of each
displayed line. ON removes blanks at the end of each line, which may
improve performance especially when you access SQL*Plus from a slow
communications device. TRIMOUT ON does not affect spooled output.

Tuesday, October 21, 2008

How to delete many files ending with an extension?

Some times you cannot rely on ‘rm’ command for deleting huge number of files. Even ‘-f’ switch will fail you. In those situations the following command can be used.

find . -name '*.ext' | xargs rm