- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell script help needed
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2009 07:08 PM
09-14-2009 07:08 PM
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
Solved! Go to Solution.
- Tags:
- expect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2009 07:22 PM
09-14-2009 07:22 PM
Re: Shell script help needed
>>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2009 07:24 PM
09-14-2009 07:24 PM
Re: Shell script help needed
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2009 04:05 AM
09-15-2009 04:05 AM
Re: Shell script help needed
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 08:51 AM
09-16-2009 08:51 AM
Re: Shell script help needed
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 08:55 AM
09-16-2009 08:55 AM
Re: Shell script help needed
One glaring error I see is this line:
filecount=`ls ${directory}/wc -c`
It should be:
filecount=`ls ${directory}|wc -c`
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 09:25 AM
09-16-2009 09:25 AM
Re: Shell script help needed
I did fix that error but still there is no message displayed when there are no files to be uploaded.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 09:34 AM
09-16-2009 09:34 AM
Re: Shell script help needed
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 11:27 AM
09-16-2009 11:27 AM
Re: Shell script help needed
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2009 01:38 PM
09-16-2009 01:38 PM
SolutionYour 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...