Thursday, March 8, 2012
Utility Program to Move a File from One Unix Directory to Another Unix Directory (Shell Script)
In many of our projects we have asked to create utility programs to move a file from one directory to another directory. Here is one of the example code which i created. Shared here.. I hope it may help my audiences
#! /usr/bin/ksh # #************************************************************************************************ # MODIFICATION HISTORY # # Person Version Date Comments # --------- -------- ------- ------------------------------------------------------------ # Team Search 0.1 09/03/2012 Initial creation #************************************************************************************************ # v_prog_name=`echo $1 | cut -f1 -d' '` v_req_id=`echo $1 | cut -f2 -d' ' | cut -f2 -d'='` v_sql_connect_string=`echo $1 | cut -f3 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_user_id=`echo $1 | cut -f4 -d' ' | cut -f2 -d'='` v_user_name=`echo $1 | cut -f5 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_printer_name=`echo $1 | cut -f6 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_save_output=`echo $1 | cut -f7 -d' ' | cut -f2 -d'='` v_num_copies=`echo $1 | cut -f8 -d' ' | cut -f2 -d'='` v_source_directory=`echo $1 |cut -f9 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_target_directory=`echo $1 |cut -f10 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_file_name=`echo $1 |cut -f11 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` v_archive_flag=`echo $1 |cut -f12 -d' ' | cut -f2 -d'=' | cut -f2 -d'"'` #echo "All Arguments : $1" echo " " echo "Program Name : $v_prog_name" echo "Request Id : $v_req_id" #echo "SQL Conn Str : $v_sql_connect_string" echo "Apps User ID : $v_user_id" echo "Apps User Name : $v_user_name" echo "Printer Name : $v_printer_name" echo "Save Output : $v_save_output" echo "Num of Copies : $v_num_copies" echo "Source Directory : $v_source_directory" echo "Target Directory : $v_target_directory" echo "File Name : $v_file_name" echo "Archive Flag : $v_archive_flag" echo " " echo " " echo "Basic Validation starts.." echo `who am i` #Check whether the Source Directory exists or not if [ -d "${v_source_directory}" ] ; then echo "1. Source Directory Exists" else echo "1. Source Directory Does not exists" echo " Directory: ${v_source_directory}" echo " Fatal error, program terminating." exit 1; fi #Check whether the Source Directory has read permission if [ -r "${v_source_directory}" ] ; then echo "2. Source Directory has read permission" else echo "2. Source Directory does not have read permission" fi #Check whether the Source Directory has write permission if [ -w "${v_source_directory}" ] ; then echo "3. Source Directory has write permission" else echo "3. Source Directory does not have write permission" fi #Check whether the Source Directory has execute permission if [ -x "${v_source_directory}" ] ; then echo "4. Source Directory has execute permission" else echo "4. Source Directory does not have execute permission" fi #Check whether the Destination Directory exists or not if [ -d "${v_target_directory}" ] ; then echo "5. Destination Directory Exists" else echo "5. Destination Directory Does not exists" echo " Directory: ${v_target_directory}" echo " Fatal error, program terminating." exit 1; fi #Check whether the Target Directory has read permission if [ -r "${v_target_directory}" ] ; then echo "6. Destination Directory has read permission" else echo "6. Destination Directory does not have read permission" fi #Check whether the Target Directory has write permission if [ -w "${v_target_directory}" ] ; then echo "7. Destination Directory has write permission" else echo "7. Destination Directory does not have write permission" fi #Check whether the Target Directory has execute permission if [ -x "${v_target_directory}" ] ; then echo "8. Destination Directory has execute permission" else echo "8. Destination Directory does not have execute permission" fi cd ${v_source_directory} echo "Basic Validation Ends.." echo " " echo " " echo "Process Begins" echo "Current directory : `pwd` \n " #echo "`ls -l ${v_file_name}*`" NTIME=`date +%H%M%S` NDATE=`date +%d%m%Y` TOTAL_FILE_COUNT=`find $v_source_directory -name "$v_file_name*" -type f | wc -l` if [ $TOTAL_FILE_COUNT -gt 0 ] then for i in `ls -rt "$v_file_name"* 2>/dev/null ` do filename=`echo "$i"|cut -f1 -d"."` fileext=`echo "$i"|awk -F"." '{if (NF>1){print $NF}}'` w_data_file="$i" if [ ${v_archive_flag} = 'Y' ] ; then v_destfile_name="$filename.$NDATE.$NTIME.$fileext" else v_destfile_name=${w_data_file} fi echo "Data_file_name : $w_data_file" echo "Destination_file_name: $v_destfile_name" if [ -s "$w_data_file" ] #file exists and have data then if [ "$i" = "" ] then echo "ERROR: Data file name is null. please check it!" else mv ${v_source_directory}/${w_data_file} ${v_target_directory}/${v_destfile_name} fi if [ -f "${v_target_directory}/$v_destfile_name" ] # If file exists then it was created OK then echo "File got moved successfully: $v_destfile_name\n" else echo "Failed to Move the file: $w_data_file\n" fi else echo "ERROR: Data file $w_data_file has zero records in it or $w_data_file does not exist! ." rm -f ${v_target_directory}/$v_destfile_name >/dev/null 2>&1 fi echo " " done else echo "ERROR: No file exist in Inbound folder." fi echo " " echo "Process Ends.."
Do you think this Article is useful?
Subscribe to:
Post Comments (Atom)
Disclaimer
The ideas, thoughts and concepts expressed here are my own. They, in no way reflect those of my employer or any other organization/client that I am associated. The articles presented doesn't imply to any particular organization or client and are meant only for knowledge Sharing purpose. The articles can't be reproduced or copied without the Owner's knowledge or permission.
0 Responses to “Utility Program to Move a File from One Unix Directory to Another Unix Directory (Shell Script)”
Post a Comment