Script not working.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script not working.
# 1  
Old 12-23-2008
Script not working.

Hi,

I am a DBA, worked on Windows platforms for past 6 years, and now shifted in environment where HP UX is OS environment.

I have task to complete which involves Unix script to be prepared. This script should FTP the file to the destination server and if this FTP fails, then it should write in syslog file. For the same purpose, I have prepared following script.

#!/usr/bin/sh
# testftp : Script to transfer file from SAP to wowftp server
#-------------------------------------------------------------------

# Timestamp
echo ------------------------------------ >> /interface/logs/alhftplog.txt
echo `date` Start of FTP to WOW FTP >> /interface/logs/alhftplog.txt
echo ----------------------------------- >> /interface/logs/alhftplog.txt


# FTP

ftp -vn HOST NAME 2> /interface/logs/ftpfailedlog.log <<END_SCRIPT >> /interface/logs/ftplog.txt
quote USER USER NAME
quote PASS USER PASSWORD
ascii
prompt off
lcd /interface/outbound
cd /test
mput FILE PATTERN*
bye
END_SCRIPT

# End of FTP

EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
logger -p local6.info -t XXTIV9999E9 "FTP to Host Failed"
else
echo `date` "FTP Successful" >> /interface/logs/ftplog.txt
fi


I am able execute above-mentioned screen and not experiencing any error. However, when I check in the system log file, no error message is being recorded.

To test this problem, I have executed “logger” command from OS prompt and I have found that error is being written in the syslog file.

Please help me to resolve this problem.

Thanks,

Best regards,

Nishchal Nagre.
# 2  
Old 12-23-2008
  1. Please use the code environment around your script for readability
  2. Quote:
    Originally Posted by Nishchal_Nagre
    I am able execute above-mentioned screen and not experiencing any error. However, when I check in the system log file, no error message is being recorded.
    So your problem is....?
# 3  
Old 12-23-2008
Hi,

My problem is, below given part of my script is not working. After execution of the script it has not spitting any error.

EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
logger -p local6.info -t XXTIV9999E9 "FTP to Host Failed"
else
echo `date` "FTP Successful" >> /interface/logs/ftplog.txt
fi

Thanks,

Best regards,

Nishchal Nagre.
# 4  
Old 12-24-2008
Ok, I'm going to assume that you mean that the FTP transfer failed, but you didn't get an error message.

Try changing the lines
Code:
EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]

to
Code:
if [ $? != 0 ]

or even
Code:
if [ ! $? ]

# 5  
Old 12-24-2008
ftp will not throw an error if it connects and runs commands then exits ok. You will need to capture the error from ftp itself. So you need to look at the stderr from you ftp command and see if the error on file transfer failed.Do you get anything in /interface/logs/ftpfailedlog.log?
# 6  
Old 01-05-2009
I want to accomplish the same thing as Nischal, if anybody can shed light as to what's wrong with my script, I would appreciate it Smilie

Here is what i want to do, ftp a file to a server then if there are any errors, an email shall be sent to my account stating that the ftp process failed. However as BubbaJoe mentioned, eventhough I get an ftp error, the return code is still zero (upon checking with $?). I need to be able to capture the error code since this will be the basis for my if statement and for the script that will run after the ftp. Initially I tried this (which didn't work):
Code:
ftp -nv server.com<<-EOF
user USER Password                          
prompt
ascii
put testfile /export/dir/test/testfileftp               
bye
EOF

#Check if FTP is successful or not
ERRCD=$?
if [ $ERRCD -ne 0] then
echo "The FTP process failed" | mailx -s "The FTP process failed with error code $ERRCD on $(date '+%m-%d:%H:%M')" myemail@administrator.com
            print “ERRCD=${ERRCD}” << /tmp/Send_errcd.log
exit 1        
fi

I was able to search something in the net which seemed like a good idea, since in his code, he checked the return code, the bytes transferred and transfer complete as the basis for whether there was an error:
Code:
ftp -nv server.com<<-EOF
user USER Password                          
prompt
ascii
put testfile /export/dir/test/testfileftp               
bye"|ftp -iv > ftpreturn.log

EOF
 
#Check if FTP is successful or not
ftpresult=$?
bytesindatafile=wc -c testfile | cut -d " " -f 1
bytestransferred=grep -e '^[0-9]* bytes sent' ftpreturn.log | cut -d " " -f 1
ftptransfercomplete=grep -e '226 ' ftpreturn.log | cut -d " " -f 1
 
echo "-- FTP result code: $ftpresult" >> ftpreturn.log echo "-- bytes in datafile: $bytesindatafile bytes" >> ftpreturn.log echo "-- bytes transferred: $bytestransferred bytes sent" >> ftpreturn.log
if [ "$ftpresult" != "0" ] || [ "$bytestransferred" != "$bytesindatafile" ] || ["$ftptransfercomplete" != "226" ] then
          echo "The CANS process failed" | mailx -s "The FTP process failed with error code $ERRCD on $(date '+%m-%d:%H:%M')" myemail@administrator.com
fi

However in that code block above, i get the error that the -c and -e commands are not valid (I am using ksh). Please help me dissect the code or if you have a suggestion on how I could check the success or not of an ftp, as well as use that error code for the email to be sent and as the basis for whether my next script will be kicked off, i would highly appreciate it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with speeding up my working script to take less time - how to use more CPU usage for a script

Hello experts, we have input files with 700K lines each (one generated for every hour). and we need to convert them as below and move them to another directory once. Sample INPUT:- # cat test1 1559205600000,8474,NormalizedPortInfo,PctDiscards,0.0,Interface,BG-CTA-AX1.test.com,Vl111... (7 Replies)
Discussion started by: prvnrk
7 Replies

2. UNIX for Beginners Questions & Answers

Why is this script not working?

for file in $(find /home/p611568/*.rpt -mtime -2 | grep -v ftplog) do awk '$1 == "XifPages," {PAGE = $5} $1 == "XifEndOfDocuments," {LINE = $5} index($0,"Value") && index($1,"Info") {sevGTI = 1} END {if (sevGTI) printf "%7s%10s%s",PAGE,LINE ... (4 Replies)
Discussion started by: bcarosi
4 Replies

3. Shell Programming and Scripting

Script not working

Hi i have write the one scripts and the scripts is error. The scripts purpose select one directory to check the file is there or not. i will give the two format of file to search the mention the path one file is there to select the file one copy the another location.please check the my script give... (1 Reply)
Discussion started by: rajivgandhi
1 Replies

4. UNIX for Dummies Questions & Answers

My script is not working

Hi All, I am trying to calculate the total_amount based on two key fields. Here is the script. I am new to unix. So please help me to find out the error in this script. I'm getting error like "command not found" and ]; is missing.. PRE_LBNO=0 PRE_DATE=0 TOT_PAY=0 for line in `cat... (4 Replies)
Discussion started by: lathanandhini
4 Replies

5. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

6. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

7. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

8. Shell Programming and Scripting

HELP !! script not working..

Hi all, I have a piece of code which cork perfect. It copy the no of lines from some files (*.log) into finalLog file. It read the lines after the line have text "ntot". Here it is : #!/bin/bash ... (10 Replies)
Discussion started by: nrjrasaxena
10 Replies

9. Shell Programming and Scripting

Perl script 'system' linking to local shell script not working

Trying to figure out why this works: printpwd.pl #!/usr/bin/perl use CGI::Carp qw( fatalsToBrowser ); print "Content-type: text/html\n\n"; $A = system("pwd"); $A = `pwd`; print "$A\n"; ^^actually that works/breaks if that makes any sense.. i get the working directory twice but when... (5 Replies)
Discussion started by: phpfreak
5 Replies

10. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies
Login or Register to Ask a Question