I'm trying to wirte ksh script for given requirement, but i unable to achive it.
In dir1 directory I need to check for the files which suffixed with .csv or .txt, If there is no files, then i need to exit. If any files found I need to move the each file found to dir2 directory. I have to repeat this for each file found.
#####sample code#######
a=/kayal/dir1
b=/kayal/dir2
ls -ltr | egrep -i '*.csvg|*.txt'| awk '{print $NF}'|while read filename
do
count=cat $filename|wc -l
done
if [ "$count" -eq 0 ]
then
echo "No files found , exiting RC=0"
RC=0
exit 1
elif [ "$count" -gt 0 ]
then
echo "Processing file $filename"
if [ -d "$b" ];
then
cd $b
mv $a/$filename $b/$filename
else
echo "unable $b"
echo "Unable to move file $filename to $b"
fi
fi
fi
done<filename
What are you trying to do here? egrep -i '*.csvg|*.txt'|
Assuming your cut/paste was fine, unsure the rationale of the 'g' in 'csvg'
And you are doing the egrep to find either of those filetypes of .csv and .txt
This has taken a little time to dig out, but here are a couple of scripts that should do what you want - modify them as you please - and use at your own risk.
The fist shell is a general purpose logger and should sit in /usr/local/bin or somewhere similar depending on your system setup.
#!/usr/bin/bash
###############################################################################
#
# This shell used to write system log file.
#
###############################################################################
###############################################################################
#
# Script information.
#
###############################################################################
#
# Script : logger.sh
# Author :
# Date :
# Version: 1.00.00
#
###############################################################################
#
# If $LOG not set up then writes to live daily LOG file in a default daily log.
#
###############################################################################
#
# This script should be resident in the users bin directory and should be
# excutable, it will either inherit a $LOG variable from the calling script or
# failing that will create it's own log in the /var/log directory.
#
###############################################################################
#
# If $LOG not set up then writes to live daily LOG file in the location below.
#
###############################################################################
if [ ${LOG} ]
then
:
else
LOG="/export/home/e415243/logs/general_log`date +%Y``date +%m%d`"
export LOG
fi
MSG="`date '+%d %b %T'` $*"
###############################################################################
#
# Make sure that we can write to the logfile if it exists.
#
###############################################################################
if [ -f ${LOG} ]
then
:
else
touch ${LOG}
chmod 666 ${LOG}
fi
###############################################################################
#
# If we can't write to the logfile lets tell someone, anyone will do!
#
###############################################################################
if echo "`date '+%d %b %T'` $*" >> ${LOG}
then
:
else
ERROR=$?
echo "logger.sh FAILED TO WRITE TO LOG FILE, error=${ERROR}"
exit ${ERROR}
fi
This next scipt does what you want - I think. But it will need to be setup for your source and target directories. You will have to refine the error checking and logging to suit your environment.
#!/usr/bin/bash
##############################################################################################
#
# Author :-
# Date :-
# Version:-
#
# History Latest at the top.
#
##############################################################################################
##############################################################################################
#
# Short description of script and any dependencies
#
#
# Requires the logger.sh script to be in the bin directory and to have a log file and local
# log directory setup.
#
# Script to move files by extensin, from a source directory to a target directory.
#
##############################################################################################
#
# Using coloured text output on screen, in order to proceed with this option there is a
# requitement to manipulate screem attributes. This can be tested using the colour.sh script
# and the appropriate handling codes used to change the output. It should be noted that the
# code should be opened and closed as a single action, there is also a subset of three already
# configured messages below which should be commented out or uncommented as required.
#
##############################################################################################
#
# The colours.sh script - run from a shell to test the screen output available.
#
##############################################################################################
# #!/bin/bash
# ##################################################################################
# #
# # Test script to show the available colours on a monitor, this allows the generation
# # of highlighted output from ordinary shell scripts - to be integrated with the
# # logger shell to make pretty output.
# #
# ##################################################################################
#
# T='XXX' # The test text
#
# echo -e "\n 40m 41m 42m 43m\
# 44m 45m 46m 47m";
#
# for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
# '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
# ' 36m' '1;36m' ' 37m' '1;37m';
# do FG=${FGs// /}
# echo -en " $FGs \033[$FG $T "
# for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
# do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
# done
# echo;
# done
# echo
##############################################################################################
#
# Set up the logging requirements, pay some attention to the detail here as it will probably
# be helpful if your scripting is anything like mine.
#
# MSG Prints a message to the logfile in the following format.
# MSG "I'm a log entry."
#
# JMSG Prints a Journal Message to the screen in the following format.
# JMSG "Put this message on the screen."
#
# DMSG Prints to the screen and the logfile in a single call.
#
# SMSG Success Mesage Prints in Green - Edit for Keyword.
#
# WMSG Warning Message Prints in Amber - Edit for Keyword.
#
# EMSG Error Message Prints in Red - Edit for Keywork.
#
# ERR Prints two lines to the logfile, the first carries a text message as above and the
# second carries the system error code this is formatted as follows.
# ERR "Something has gone wrong."
#
# ERR_COUNT Prints three lines to the logfile but unlike the straight forward "ERR", it is not
# fatal and the script will continue.
# ERR_COUNT "Something has gone wrong - but script is continuing, generally accompanied by a
# JMSG to print something on the screen.
#
# STARTMSG Puts a timestamp message with the script name in the logfile.
#
# ENDMSG Puts a timestamp message with scriptname in logfile.
#
##############################################################################################
MSG()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
}
DMSG()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
echo "\n$*"
}
SMSG()
{
echo "$*" | /usr/bin/awk '/Success/ { $0="\x1b[1;42m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
WMSG()
{
echo "$*" | /usr/bin/awk '/Warning/ { $0="\x1b[1;43m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
EMSG()
{
echo " $*" | /usr/bin/awk '/Error/ { $0="\x1b[1;41m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
JMSG()
{
echo "$*"
}
ERR()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
/usr/local/bin/logger.sh "${THISPROG} FAILS"
exit 1
}
ERR_COUNT()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
/usr/local/bin/logger.sh "${THISPROG} Error Encountered."
COUNT=$((COUNT+1))
/usr/local/bin/logger.sh "${THISPROG} Error Count is now ${COUNT}."
}
ENDMSG()
{
/usr/local/bin/logger.sh "${THISPROG} COMPLETED"
exit 0
}
STARTMSG()
{
/usr/local/bin/logger.sh "${THISPROG} STARTED"
}
##############################################################################################
#
# Set up Globals.
#
##############################################################################################
BOLDON=`tput sgr1`
BOLDOFF=`tput sgr0`
export BOLDON BOLDOFF
TMPDIR=/var/tmp
BINDIR=/usr/local/bin
LOGDIR=/var/log
export BINDIR LOGDIR TMPDIR
THISPROG=$0
export THISPROG
LOG="${LOGDIR}/dailylog_`date +%Y``date +%m%d`.log"
REGRESS="${LOGDIR}/regress_`date +%Y``date +%m%d`"
COUNTER=0
COUNT=0
export LOG REGRESS COUNTER COUNT
HOSTNAME=`uname -n`
DATE=`date +%d%m%Y`
export HOSTNAME DATE
##############################################################################################
#
# Print the start message to the logfile.
#
##############################################################################################
STARTMSG
##############################################################################################
#
# Put the body of the script and any script specific variables in this section, it will make
# problem solving easier if you keep it all together.
#
##############################################################################################
SOURCE=/home/e434069/data
TARGET=/home/e434069/tmp
export SOURCE TARGET
if [ -d "${SOURCE}" ]
then
JMSG "Directory ${SOURCE} exists."
else
MSG "The source directory ${SOURCE} does not exist."
JMSG "The source directory ${SOURCE} does not exist."
ERR "Script Exiting."
fi
if [ -d "${TARGET}" ]
then
JMSG "Directory ${TARGET} exists."
else
MSG "The target directory ${TARGET} does not exist."
JMSG "The target directory ${TARGET} does not exist."
ERR "Script Exiting."
fi
##############################################################################################
# Setup file types to be moved from source to target directories.
##############################################################################################
FT1="*.txt"
FT2="*.csv"
export FT1 FT2
##############################################################################################
# Check that the file types exist and setup the counters.
##############################################################################################
FT1RC=`ls -l ${SOURCE}/${FT1} > /dev/null 2>&1`
FT2RC=`ls -l ${SOURCS}/${FT2} > /dev/null 2>&1`
COUNTFT1=`ls -l ${SOURCE}/${FT1} | wc -l | awk '{ print $1 }'`
COUNTFT2=`ls -l ${SOURCE}/${FT2} | wc -l | awk '{ print $1 }'`
##############################################################################################
# Move the files.
##############################################################################################
if [ "${FT1RC}" != "0" ]
then
if [ "${COUNTFT1}" != "0" ]
then
JMSG "There are ${COUNTFT1} files of type ${FT1} to move."
for i in `ls ${SOURCE}/${FT1}`
do
JMSG "Moving ${i} from ${SOURCE} to ${TARGET}"
mv ${i} ${TARGET}/
MVRC=$?
if [ "${MVRC}" == "0" ]
then
SMSG "Successfully moved file ${i}"
else
ERR_COUNT "Error there was a problem with ${i}"
fi
done
else
JMSG "There are no files of type ${FT1} to be moved."
fi
else
:
fi
if [ "${FT2RC}" != "0" ]
then
if [ "${COUNTFT2}" != "0" ]
then
JMSG "There are ${COUNTFT2} files of type ${FT2} to move."
for i in `ls ${SOURCE}/${FT2}`
do
JMSG "Moving ${i} from ${SOURCE} to ${TARGET}"
mv ${i} ${TARGET}/
MVRC=$?
if [ "${MVRC}" == "0" ]
then
SMSG "Successfully moved file ${i}"
else
ERR_COUNT "Error there was a problem with ${i}"
fi
done
else
JMSG "There are no files of type ${FT1} to be moved."
fi
else
:
fi
##############################################################################################
#
# Print the end message to the logfile.
#
##############################################################################################
if [[ ${COUNT} -ne 0 ]]
then
EMSG "The script encountered Errors - Please check ${LOGDIR}/${LOG}"
else
SMSG "The script completed with a status of Success."
fi
ENDMSG
You might have to tweak them both for your systems, it works like this.
[root@fbakirpomd4 bin]# ls ../data
file_01.dat test_01.csv test_01.txt test_02.dat test_03.txt test_05.csv
file_02.dat test_01.dat test_02.csv test_02.txt test_04.csv
[root@fbakirpomd4 bin]# ./mover.sh
Directory /home/e434069/data exists.
Directory /home/e434069/tmp exists.
There are 3 files of type *.txt to move.
Moving /home/e434069/data/test_01.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_01.txt
Moving /home/e434069/data/test_02.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_02.txt
Moving /home/e434069/data/test_03.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_03.txt
There are 4 files of type *.csv to move.
Moving /home/e434069/data/test_01.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_01.csv
Moving /home/e434069/data/test_02.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_02.csv
Moving /home/e434069/data/test_04.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_04.csv
Moving /home/e434069/data/test_05.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_05.csv
The script completed with a status of Success.
[root@fbakirpomd4 bin]# ls ../data
file_01.dat file_02.dat test_01.dat test_02.dat
[root@fbakirpomd4 bin]# ls ../tmp
test_01.csv test_01.txt test_02.csv test_02.txt test_03.txt test_04.csv test_05.csv
[root@fbakirpomd4 bin]# cat /var/log/daily*
18 Sep 14:34:29 ./mover.sh STARTED
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_01.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_02.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_03.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_01.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_02.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_04.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_05.csv
18 Sep 14:34:29 ./mover.sh The script completed with a status of Success.
18 Sep 14:34:29 ./mover.sh COMPLETED
[root@fbakirpomd4 bin]#