Operating System - HP-UX
1828340 Members
3943 Online
109976 Solutions
New Discussion

Re: Shell script help needed

 
SOLVED
Go to solution
rhansen
Frequent Advisor

Shell script help needed

Hello,

I am using a shell script which in turn calls a expect script to upload files to a remote server. My script is working files to upload the files, the only logic that I cannot get to work is when there are no files to upload to the remote server, I would like a check to be performed and a message to be displayed stating " There are no files to be uploaded".

I am pasting part of my script.

# Counters
uSuccess=0
uFailure=0

cd $HOME

if [[ $1 == "-u" ]]
then
echo "Uploading..."

while read line
do
# test
upload_directory=${line}/submission/test

directory=${HOME}/toupload/test/${line}
cd ${directory}
echo "in directory ${directory}"

counter=0
for filename in *
do

if [[ -f ${filename} ]]
then

let counter=counter+1

filename_abs=${directory}/${filename}

${HOME}/ftp_upload.exp ${user} ${pword} ${host} ${upload_directory} ${filename}


if [[ $? -eq 0 ]]
then

echo "$(date): ${directory}/${filename} uploaded" >> $LOGDIR/sftp_audit.log
rm ${filename}
let uSuccess=uSuccess+1
else
echo "$(date): ${directory}/${filename} not able to upload" >> $LOGDIR/sftp_failure.log
let uFailure=uFailure+1
fi
fi
done

if [[ $counter -eq 0 ]]
then
echo "$(date): No files to upload from ${directory} directory" >> $LOGDIR/sftp_audit.log
fi
9 REPLIES 9
Suraj K Sankari
Honored Contributor

Re: Shell script help needed

Hi,
>>I cannot get to work is when there are no files to upload to the remote server, I would like a check to be performed and a message to be displayed stating " There are no files to be uploaded".


Take a "ls -lrt > chkfile" now check if chkfile size is 0 that means there is no file to upload.

Suraj
Mel Burslan
Honored Contributor

Re: Shell script help needed

Just before getting into the for loop for uploading, you can perform a chack like this:

filecount=`ls ${directory}|wc -c`

if [ ${filecount} -eq 0 ]
then
echo "There is nothing to upload in ${directory}"
else
# go into the for loop here
# ...
# end of for loop and any other things
# you want to do before relinquishing control
fi

Hope this helps...
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Shell script help needed

Hi:

Since you didn't present the whole script, it's hard to offer too many suggestions.

However:

Good practice declares the interpreter in the 'shebang' line as the first of the script:

#!/usr/bin/sh

...for example.

if [[ $1 == "-u" ]]

...should be:

if [[ $1 = "-u" ]]

You can use loops to structure your script like this:

#!/usr/bin/sh
typeset -i counter=0
echo "Enter directory name; CTL_D to quit"
while read directory
do
[ -d "${directory}" ] || { echo "not a directory"; continue; }
[ "$(ls -a ${directory} 2>/dev/null | wc -l)" -le 2 ] && { echo "empty dir"; continue; }
counter=$((counter+1))
done

...

The 'continue' statement restarts the loop and allow us to gracefully reject non-directories and directories that are empty. The 'ls -a' causes the dot and dot-dot directories to be returned along with any "hidden" files (those that begin with a dot). Hence, an empty directory is one with only two entries.

Regards!

...JRF...
rhansen
Frequent Advisor

Re: Shell script help needed

As advised, I tried it but still does not work. I am pasting thq full section of my upload script.

#!/usr/bin/ksh
# set -x
#
# Establish and set variables
host_id="xyz.test.org"
port_number="\#22"
user_id="abcd"
password="password"
HOME="/home/abcd"
TMP=${HOME}/tmp
LOGDIR="/var/adm/ras/"

if [[ $# -ne 1 ]]
then
echo "Usage script -u|-d"
exit 1
fi

if [[ $1 = "-u" ]]
then
echo "Upload"
while read line
do

upload_directory=${line}/submission/test
directory=${HOME}/upload/test/${line}
cd ${directory}
echo "in directory ${directory}"
for filename in *
do
if [[ -f ${filename} ]]
then
filename_abs=${directory}/${filename}
filecount=`ls ${directory}/wc -c`
if [[ ${filecount} -eq 0 ]]
then echo "There is nothing to upload in ${directory}"
else
${HOME}/sftp.exp ${user_id} ${password} ${host_id} ${port_number} ${upload_directory} ${filename}
if [[ $? -eq 0 ]]
then
echo "$(date): ${directory}/${filename} uploaded >> $LOGDIR/sftp_audit.log

else

echo "$(date): ${directory}/${filename} not able to upload >> $LOGDIR/sftp_failure.log
fi
fi
fi
done
James R. Ferguson
Acclaimed Contributor

Re: Shell script help needed

Hi (again):

One glaring error I see is this line:

filecount=`ls ${directory}/wc -c`

It should be:

filecount=`ls ${directory}|wc -c`

Regards!

...JRF...
rhansen
Frequent Advisor

Re: Shell script help needed

Hi James,

I did fix that error but still there is no message displayed when there are no files to be uploaded.

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help needed

Hi (again):

> I did fix that error but still there is no message displayed when there are no files to be uploaded.

Actually your script won't even run as posted. You lack closing double quotes on lines 41-and-45 and the 'if/then' on line-20 isn't closed with a matching 'fi'.

Regards!

...JRF...
rhansen
Frequent Advisor

Re: Shell script help needed

Sorry for the missing quotes and the if loop. I have it all covered, it was a mistake in copy n paste. The real issue in my script that I am trying to fix it is to display a message when there are no files to be uploaded else it is working as expected.

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell script help needed

Hi (again):

Your problem is that when you 'cd' into an empty directory, an empty list is returned by the statement:

for filename in *

The test for the number of files needs to be moved forward in the code, so that it all looks something like this:

#!/usr/bin/ksh
# set -x
#
# Establish and set variables
host_id="xyz.test.org"
port_number="\#22"
user_id="abcd"
password="password"
HOME="/home/abcd"
TMP=${HOME}/tmp
LOGDIR="/var/adm/ras/"

if [[ $# -ne 1 ]]; then
echo "Usage script -u|-d"
exit 1
fi

if [[ $1 = "-u" ]]; then
echo "Upload"
while read line
do
upload_directory=${line}/submission/test
directory=${HOME}/upload/test/${line}

cd ${directory}
echo "in directory ${directory}"
filecount=`ls ${directory}|wc -c`
if [[ ${filecount} -eq 0 ]]; then
echo "There is nothing to upload in ${directory}"
continue
fi

for filename in *
do
if [[ -f ${filename} ]]; then
filename_abs=${directory}/${filename}
${HOME}/sftp.exp ${user_id} ${password} ${host_id} ${port_number} ${upload_directory} ${filename}
if [[ $? -eq 0 ]]; then
echo "$(date): ${directory}/${filename} uploaded" ### >> $LOGDIR/sftp_audit.log
else
echo "$(date): ${directory}/${filename} not able to upload" ### >> $LOGDIR/sftp_failure.log
fi
fi
done
done
fi

...

I would also suggest that you re-read my earlier responses in this thread relating to techniques to control loops, but this should solve your essential problem. Too, if you have hidden files (those filenames beginning with a dot character) then you need to re-read my earlier comments again.

Regards!

...JRF...