[Solved] Need help in editing a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Need help in editing a script
# 1  
Old 02-06-2014
[Solved] Need help in editing a script

Hi,
I have one script in my cronjob, which is fetching file from a ftp site and making a copy with today's date and time. This is a new setup. There was a instance when test_bill.txt was not present on ftp.xxxx_xxxx.com and when this job ran and did not fetched file, still it send mail of successful status to indocs_user@scrach.com (SFTP file transfer is successfull for file test_bill.txt)
Code:
#!/bin/sh
HOST='ftp.xxxx_xxxx.com'
USER='user_1'
FILE='test_bill.txt'
sftp $USER@$HOST <<END_SCRIPT
 get $FILE
END_SCRIPT
if [ $? != 0 ];then
   echo "SFTP file transfer is failed for file $FILE" |mailx indocs_user@scrach.com
else
echo "SFTP file transfer is successfull for file $FILE" |mailx indocs_user@scrach.com
cp $FILE $FILE.`date +%m%d%y`
fi

Below is the output
Code:
sftp> get toyota_bill.txt
Couldn't stat remote file: No such file or directory
File "/test_bill.txt" not found.

I want, if this cronjob is fail to fetch file, it should send mail. Can somebody help in editing this script ?
# 2  
Old 02-06-2014
sftp exit status will only reflect if it managed to connect to the server or not and disregards any transfer that fails. Using batch input -b script_file tends to report these errors better.

Try:
Code:
#!/bin/sh
HOST='ftp.xxxx_xxxx.com'
USER='user_1'
FILE='test_bill.txt'
cat > /tmp/script_file_$$ <<EOF
    get $FILE
    exit
EOF

sftp -b /tmp/script_file_$$ $USER@$HOST
exist_status=$?
rm -f /tmp/script_file_$$

if [ $exit_status != 0 ];then
   echo "SFTP file transfer is failed for file $FILE" |mailx indocs_user@scrach.com
else
echo "SFTP file transfer is successfull for file $FILE" |mailx indocs_user@scrach.com
cp $FILE $FILE.`date +%m%d%y`
fi

# 3  
Old 02-06-2014
In case it wasn't obvious, there is a small typo in Chubler_XL's script. Change:
Code:
exist_status=$?

to:
Code:
exit_status=$?

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 02-06-2014
Thanks Chubler and Don. Here is the current shape of script
Code:
#!/bin/sh
HOST='ftp.xxxx_xxxx.com'
USER='user_1'
FILE='test_bill.txt'
cat > /tmp/script_file_$$ <<EOF
    get $FILE
    exit
EOF
 sftp -b /tmp/script_file_$$ $USER@$HOST
exit_status=$?
rm -f /tmp/script_file_$$
if [ $? != 0 ];then
  echo "SFTP file transfer is failed for file $FILE" |mailx indocs_user@scrach.com
else
echo "SFTP file transfer is successfull for file $FILE" |mailx indocs_user@scrach.com
cp $FILE $FILE.`date +%m%d%y`
fi

It produced below output, failed again, but still sent me mail - SFTP file transfer is successfull for file test_bill.txt
Code:
sftp>     get test_bill.txt
Couldn't stat remote file: No such file or directory
File "/test_bill.txt" not found.

Where I am missing now ? I am more concerned about fail status mail. If I do not want successful status mail, it is also fine (If that can make this script easier).

Last edited by solaris_1977; 02-06-2014 at 11:21 PM..
# 5  
Old 02-06-2014
If statement should be testing $exit_status not $?:

Code:
if [ $exit_status != 0 ];then

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-06-2014
Sorry, it was my miss.
It worked. Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Shell Programming and Scripting

[Solved] Editing the alphabet's based on position information

I do have a file of the following format file 1 >SAM ATGCTCCTTAGCTACGTAGCAAGTAGAAAAAA AGCGCGAGCATTGAAGCGGAGGAGAGGAGGA TGAGATGATGACCCAGTATAAAGAGTGATGAT like this above file. file 1 has 1000's of lines. I would like to edit this file1 using the information from file2 (see below), by... (16 Replies)
Discussion started by: Lucky Ali
16 Replies

3. Shell Programming and Scripting

editing a file in a script

Gurus, I need to write a shell script that will calculate hash value of a file, opens the file in an application for example vi editor. The application can read or modify the contents of the file. When application exists second part of my script will kick in and recalculate the hash value. File... (1 Reply)
Discussion started by: c0kazaz
1 Replies

4. UNIX for Dummies Questions & Answers

Making a script for editing

Hello all. I am trying to make a script to edit any file by replacing a string with a string. The script should be able to be applied to any given file. for instance it should be able to replace "foo" with "bar" in the file myFile.txt. I know i need to make a back up file for this to work. I just... (2 Replies)
Discussion started by: iwatk003
2 Replies

5. UNIX for Dummies Questions & Answers

Editing Shell Script

Hi I'm a newbie, but I understand that there's some space difference between unix and the pc, which is why I can't write shell script on my pc but I can view it using notepad, wordpad, etc. Is there any program I can use that will let me view the shell script and edit it without screwing up... (6 Replies)
Discussion started by: qtip
6 Replies

6. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

7. UNIX for Dummies Questions & Answers

dos2unix - editing script - Help Urgent Please

Hi this is my first time on this forum so sorry if I posted this to the wrong area. I need help with the following. I have a basic FormMail cgi script I use. When I upload the script using filezilla, there is one line in the script that changes. The line should read &get_date; instead it... (2 Replies)
Discussion started by: lydp101
2 Replies

8. Shell Programming and Scripting

SH Script help. editing string

I have a string that looks like this username|field1|field2|field3 the data has a delimiter of "|" how can i edit field1, keeping the rest of the data the same also how can i edit field2 and 3. (3 Replies)
Discussion started by: nookie
3 Replies

9. Shell Programming and Scripting

Editing a file using a script

Hi I have about 10 config files belonging to software that runs on SCO UNIX. These files contain, amongst many other things, a path which points to the software locations. We normally have to change them manually every time the software is coppied to a new location and when you gotta do a few... (45 Replies)
Discussion started by: Ypnos
45 Replies
Login or Register to Ask a Question