user1098279
user1098279

Reputation: 1

Bash error reports

Ok, I can get this to run and put the file on the remote server. However when it does run, I want it to echo a Transfer failed, Transfer OK, Zero bytes in file and No such file or directory. It just runs down the code and sends an email of the last entry which is no such file or directory when clearly the file transferred to the server. What am I doing wrong ?? Thx in advance, I thank you.

#!/bin/sh
HOST=10.10.1.2
USER='test'
RECIP="[email protected]"
cd /home/test
FILE=ARG$(date '+%Y%m%d').txt
BYTES=`stat -c%s $FILE`
CONNECTION=`netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' |   sort | uniq -c | awk '{printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++)    {printf("*")}; print ""}'`
RUN_AS=`whoami`
RESULT=`sftp $USER@$HOST <<EOF
FILE=ARG$(date '+%Y%m%d').txt
cd /998979/DES
if [ -e $FILE ]; then
put $FILE
EOF`

echo $CONNECTION "Connection to network is established."

ls | xargs wc -wl for file in *; echo $FILE done  
if [ $? -eq 0 ]; then
echo "$RESULT" "Transfer of file failed. `date`"
SBJ="Transfer of file failed `date`"
fi
echo "$RESULT" "Transfer OK"
if [ $? -eq 0 ]; then
SBJ="Transfer OK. `date`"
echo "$RESULT" "Transfer OK"
fi
if \[ ! -s ${FILE} \]; then
echo "File: ${FILE} is not present or a ZERO byte file"
SBJ="Failed to upload. Zero bytes in file. `date`" # zero bytes sent
fi
if [ -a $FILE ]; then
echo "$RESULT" "No such file or directory"
SBJ="No such file or directory. `date`"
fi

BODY="Process Report....... 

SRC SERVER:`hostname` 
DST SEVER: TESTSERVER
SCRIPT: /usr/local/bin/put.sh
RUN AS: "$RUN_AS" 

RESULT: "$RESULT" "$FILE"

CONNECTION: "$CONNECTION" 

FILENAME: "$FILE" 

BYTES: "$BYTES" 
DATE/TIME: `date`"

echo "$BODY" | mail -s "$SBJ" "$RECIP"

Upvotes: 0

Views: 230

Answers (2)

user982733
user982733

Reputation:

count=$?

if [ $count -eq 0 ]; then 
 echo "$RESULT" "Transfer of file failed. 
 `date`" SBJ="Transfer of file failed `date`" 
else 
 echo "$RESULT" "Transfer OK" 
fi

Assign the result to a variable, Also, try to use exit when you don't need to do more processing.

Upvotes: 0

You can use /bin/sh -xv yourshellscript.sh to understand what is happening.

And you could log important messages to the system log using logger

Upvotes: 2

Related Questions