-
Get a text file "from.txt" from a FTP server.
#!/bin/ksh FTP_HOST=ftp.example.com FTP_USER=ftpuser FTP_PASS=pass123 FTP_DIR=/home/ftpuser FTP_FILE=from.txt DEST_DIR=/home/localuser DEST_FILE=to.txt JOB_LOG_FILE=example.log ftp -nv >> $JOB_LOG_FILE << EOFMARK open $FTP_HOST user $FTP_USER $FTP_PASS acsii get $FTP_DIR/$FTP_FILE $DEST_DIR/$DEST_FILE EOFMARK return 0 -
List the files in a directory from a FTP server.
#!/bin/ksh FTP_HOST=ftp.example.com FTP_USER=ftpuser FTP_PASS=pass123 FTP_DIR=/home/ftpuser FTP_FILE=from.txt FTP_TMP_FILE=ftp_temp.txt JOB_LOG_FILE=example.log ftp -nv >> $JOB_LOG_FILE << EOFMARK open $FTP_HOST user $FTP_USER $FTP_PASS cd $FTP_DIR prompt off ls * $FTP_TMP_FILE EOFMARK return 0
A blog to log all the findings I have found during my programming life before I forget. ^.^
Showing posts with label KSH. Show all posts
Showing posts with label KSH. Show all posts
17 Jan 2013
FTP in KSH
Executing Oracle statements or Stored Procedures from KSH
- To execute an Oracle stored procedure,
#!/bin/ksh USER=scott PASS=tiger ORACLE_SID=example_sid JOB_LOG_FILE=example.log sqlplus -s $USER/$PASS"@"$ORACLE_SID >> $JOB_LOG_FILE << EOFMARK set serveroutput on set timing on exec sp_example('ABC'); exit EOFMARK return 0The above script uses SQL Plus as client to connect to Oracle database, and execute the stored procedure "sp_example". All dbms_out.put_line() outputs will be logged into the "example.log" file.
- To Run a query on an Oracle database, and output the results to a text file "test.txt",
#!/bin/ksh
USER=scott
PASS=tiger
ORACLE_SID=example_sid
JOB_LOG_FILE=example.log
ORA_TMP_FILE=test.txt
sqlplus -s $USER/$PASS"@"$ORACLE_SID >> $JOB_LOG_FILE << EOFMARK
set serveroutput on
set timing on
set heading off
spool $ORA_TMP_FILE
select TO_CHAR(SYSDATE, 'dd-Mon-yyyy'), COUNT(*) from dual;
spool off
EOFMARK
return 0
To check whether there is data selected,
if [[ -e $ORA_TMP_FILE ]];then
PARAM_INFO=`grep "no rows selected" $ORA_TMP_FILE | wc -l`
fi
if [[ $PARAM_INFO -eq 1 ]];then
echo "No data is selected."
fi
To get the data from the text file,
if [[ -e $ORA_TMP_FILE ]];then
PARAM_LIST=`cat $ORA_TMP_FILE`
fi
if [[ $PARAM_LIST != "" ]];then
set -A array $PARAM_LIST
CURRENT_DATE=${array[0]}
RECORD_COUNT=${array[1]}
echo "Current date is ${CURRENT_DATE}."
echo "Record count is ${RECORD_COUNT}."
fi
The output is:
Current date is 17-Jan-2013.
Record count is 1.
KSH Tips
1. To include a file,
2. To check whether a parameter is inputted or not,
3. To convert a variable value to upper case,
The output is:
4. To replace words in a file,
5. To trap an error,
6. To output the current time in specific format,
The output is:
7. To check whether a word appears in a file,
8. To execute a Java Jar file,
8. To housekeep files, e.g. for 30 days,
. ./error_handling.sh
2. To check whether a parameter is inputted or not,
if [[ -z $1 ]];then
echo "Usage: `basename $0` arg1"
echo "Please provide date in YYYYMMDD as the first parameter when running this script."
return 100
fi
3. To convert a variable value to upper case,
YOUR_NAME="Mickey Mouse"
typeset -u $YOUR_NAME
echo $YOUR_NAME
The output is:
MICKEY MOUSE
4. To replace words in a file,
sed -e 's/&/&/g' "from.txt" > "to.txt"
5. To trap an error,
JOB_LOG_FILE=example.log
trap 'RC=$?; echo `date` "Error Line No.: $LINENO (Error Code: $RC)" >> $JOB_LOG_FILE' ERR
6. To output the current time in specific format,
echo `date +%y/%m/%d` `date +%H:%M:%S` "Hello World!"
The output is:
Thu Jan 17 16:31:07 HKT 2013 Hello World!
7. To check whether a word appears in a file,
JOB_LOG_FILE=example.log
info=`grep -Ei "error" $JOB_LOG_FILE | wc -l`
if [[ $ftp_info -qt 0 ]];then
echo "Error(s) found."
else
echo "No Error found."
fi
8. To execute a Java Jar file,
JOB_LOG_FILE=example.logJAR_PATH=/home/scott
JAVA_NAMR=example.jar
PARAM_VALUE=test
echo `date` `java -jar $JAR_PATH/$JAR_NAME $PARAM_VALUE` >> $JOB_LOG_FILE
8. To housekeep files, e.g. for 30 days,
KEEP_FILES_IN_DAYS=30
find $LOG_PATH -name "job_*.log" -mtime +${KEEP_FILES_IN_DAYS} -exec rm -f {} \;
Send email with attachments in KSH
- Suppose there is an email template with empty body.
For example,
to: test_to@example.com cc: test_cc@example.com from: test_from@example.com subject: Test Example with empty body
To send this email text, for example, named email_tmpl.txt, through sendmail, simple issue the following command:
sendmail -t < email_tmpl.txt - Suppose there is an email template with plain text body.
For example,
to: test_to@example.com cc: test_cc@example.com from: test_from@example.com subject: Test Example with plain text body This is an email with plain text body.To send this email text, for example, named email_tmpl.txt, through sendmail, issue the same command in (1).
- Suppose there is an email template with plain text body as in (2). To send with attachments, e.g. attachments "a.html" and "b.html", issue the following command:
(cat "email_tmpl.txt"; uuencode "a.html" "a.html"; uuencode "b.html" "b.html") | sendmail -t - Suppose there is an email template with HTML body.
For example,
to: test_to@example.com cc: test_cc@example.com from: test_from@example.com subject: Test Example with HTML body Content-Type: text/html; charset=utf-8 <html> <head></head> <body> This is an email template with <b>HTML</b> body! <body> </html>To send this email text, use the same command as in (1).
- Suppose there is an email template with HTML body. To send with HTML attachments,
For example,
to: test_to@example.com cc: test_cc@example.com from: test_from@example.com subject: Test Example with HTML body Content-Type: multipart/mixed; boundary="test_example_boundary" --test_example_boundary Content-Type: text/html; charset=utf-8 <html> <head></head> <body> This is an email template with <b>HTML</b> body! <body> </html> --test_example_boundary Content-Type: text/html; charset=utf-8 Content-Disposition: attachment; filename=html_attachment.html <html> <head></head> <body> This is a <b>HTML</b> attachment! <body> </html> --test_example_boundary--
To send this email text, use the same command as in (1).
Subscribe to:
Posts (Atom)