Pages

Friday, May 29, 2009

my quick ksh

Numeric test -

1.

nonums="$(echo $answer | sed 's/[0-9]//g')"


if [ ! -z "$nonums" ] ; then
echo "Not an integer value."
fi

2.
echo -n "Pick a number between 1 and 20: " read answer

nonums="$(echo $answer | sed 's/[0-9]//g')"

if [ ! -z "$nonums" ] ; then
echo "Not an integer value." exit 0
fi

if [ $answer -lt 10 ] ; then
echo "Your answer is less than ten"
else
echo "Your answer is not less than ten"
fi

3.case xxx in
+([0-9]))
echo 1
;;
* )
echo 0
;;
esac

4. 
stackflow:
re='^[0-9]+$'
if ! [[ $yournumber =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi


Parse file Name & Ext -

file=xxxx.yyy

nam=`echo ${file%.*}`
ext=`echo ${file#*.}`



remove CR from EOF -

  • tr '\012' ' '; echo
  • awk '{printf "%s ", $0} END { print "" }'



Ksh Version -

at a ksh prompt - print xxx

$ set|egrep -i ver

$ what /bin/ksh | egrep Version


Email -

simple text body:

  • cat file1 | mailx -s "subject" user@host.com
  • mailx -s "subject" user@host.com <>
attachments:
  • uuencode file1 file1 | mailx -s "subject" user@host.com
  • (uuencode file1 file1; uuencode file2 file2) | mailx -s "subject" user@host.com
both:
  • (cat file1; uuencode file1 file1;uuencode file2 file2 ) | mailx -s "subject" user@host.com
save attachment in mbox then uuencode to retrieve the file.

sendmail:

#!/bin/ksh
set -xv
# sendmail header function
fn_sendmail()
{
# Build sendmail header.
echo "From: user1@test.com"
echo "To: user2@test2.com"
echo "Reply-to: user3@test3.com, user4@user4"
echo "Subject: ${SUBJECT}"
echo "X-MSMail-Priority: High"
echo "importance: 1"
#echo "Mime-Version: 1.0"
#echo "Content-Type: text/plain"
#echo "Content-Type: multipart/mixed"
echo "

# Run the data function.
Hi XXX,

This is a test email

YYY
"
# uuencode the attachments, add checks if required file extension,
for k in file1 file2
do
uuencode $k $k
done

}

SUBJECT=test_subject

# Call sendmail header and pipe to sendmail.
(fn_sendmail)| sendmail -t -oi
##