LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   check the condition in multiple file and FTP that multiple file to server and need mail confirmation. (https://www.linuxquestions.org/questions/linux-software-2/check-the-condition-in-multiple-file-and-ftp-that-multiple-file-to-server-and-need-mail-confirmation-4175633177/)

saphirasiva 07-03-2018 08:02 AM

check the condition in multiple file and FTP that multiple file to server and need mail confirmation.
 
Hi Everyone,

I am new to Unix shell scripting.

I need FTP script to move the multiple files to server.
That file should be checked with some condition. I will describe in detail.
The file_names will be like g20180703_somecode, need to open those file and need to be checked wheather these file are having Pcode1,or Pcode2 or Pcode3 and in next line wheather it is ABC or XYZ. if these two condition are in those file. Need to FTP those file to server.


g20180703_somecode these file will be placed in x/y/z path in server.

TB0ne 07-03-2018 09:38 AM

Quote:

Originally Posted by saphirasiva (Post 5874944)
Hi Everyone,
I am new to Unix shell scripting. I need FTP script to move the multiple files to server.
That file should be checked with some condition. I will describe in detail.
The file_names will be like g20180703_somecode, need to open those file and need to be checked wheather these file are having Pcode1,or Pcode2 or Pcode3 and in next line wheather it is ABC or XYZ. if these two condition are in those file. Need to FTP those file to server.

g20180703_somecode these file will be placed in x/y/z path in server.

Ok, so since you're new to shell scripting, this will be an *EXCELLENT* thing for you to learn with, because there are many easily-found examples, sample scripts, and tutorials on bash scripting to get you started. You can find these with a simple Google search...start there.

You also need to read the "Question Guidelines" link in my posting signature. We are happy to help you if you're stuck, but we WILL NOT write scripts for you. Doing basic research and showing your own efforts should be the first things you do.

Turbocapitalist 07-03-2018 09:43 AM

Quote:

Originally Posted by saphirasiva (Post 5874944)
I need FTP script to move the multiple files to server.

I second what TB0ne wrote above and add to that advice to eschew FTP. It is almost certain that you have SFTP (which is a completely different protocol/service with a similar UI) already installed and running by default. It is part of the OpenSSH server and is not only much easier to set up, can be run securely unlike FTP which can never be made even remotely secure and would be a hazard to us all if it were to be put out on the net.

That said, please show us your script (with SFTP) so we can see how far you have gotten and please say where specifically you have gotten stuck or need advice.

saphirasiva 07-09-2018 03:24 AM

for i in 'cd /path'
do
dsh -n $i ls -la|grep -i ABC g201878* >>/flame/spinner/arc/temp
FILE="/x/y/z"
then
put -p $FILE or (/x/y/z)
sftp -b /x/y/z username@hostname: /z/y/x/path where to copy/
then
rm -rf /x/y/z

TB0ne 07-09-2018 02:18 PM

Quote:

Originally Posted by saphirasiva (Post 5877233)
for i in 'cd /path'
do
dsh -n $i ls -la|grep -i ABC g201878* >>/flame/spinner/arc/temp
FILE="/x/y/z"
then
put -p $FILE or (/x/y/z)
sftp -b /x/y/z username@hostname: /z/y/x/path where to copy/
then
rm -rf /x/y/z

Use CODE tags when posting code, and as you were asked, tell us where your script is failing, and with what message(s). Using DSH for such things isn't the best approach, but if you want to feed it the result of the "ls -la...." command, you need to enclose that in backticks (to the left of the "1" on your keyboard). Also, you need to read the man page on sftp, since you can run that in batch mode to upload the files that match the pattern.

You also have a missing "if" in your "if/then" statements, the FILE variable seems to be doing nothing (since you're identifying the files in the line before that...). And since you're now using SSH/SFTP (as you should), consider using scp instead, since you could then just issue a simple one-line copy, after doing a key exchange, like:
Code:

scp user@remote.host:/x/y/z/g2018* /some/local/path
..and be done.

saphirasiva 07-11-2018 10:11 AM

I have modified the code. Could everyone please check and advice me.


#!/usr/bin/ksh
HOST='IP'
USER='xxxxx'
PASSWD='0000'
FILE="/x/y/g$(date +%Y%m%d%)_*"
if [ -f $FILE ] ;
then
ls -la |grep -i ABC $FILE >> /z/y/x
ftp -v -n $HOST >>END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mput $FILE
quit
END_SCRIPT
exit 0
else
printf " we can't find the current data file" #|mailx -s "file report"
fi



Output:

i am getting we can't find the current data file.

I have not checked the condition Pcode1,Pcode2,Pcode3.Please do the needful

Turbocapitalist 07-11-2018 10:21 AM

Please remember to use [[b]code] [/code] tags around your script so that indents and other spacing is retained.

You'll also need to say clearly what you are trying to accomplish since there are no comments in your code about why you are trying specifics.

Current guess:

Code:

#!/bin/sh

set -e;
set -x;

host='IP'
user='xxxxx'

file="/x/y/g$(date +%Y%m%d%)_*"
if [ -f $file ]; then
        ls -la | grep -i ABC $file >> /z/y/x';
        echo "mput $file" \
        | sftp -i ~/.ssh/$host.rsa -b - -l $user $host;
        exit 0;
else
        printf " we can't find the current data file" |mailx -s "file report"
        exit 1;
fi

Again, you'll have to both work through the steps yourself as well as clarify what you are trying to accomplish. But either way, FTP is not allowed any more.

michaelk 07-11-2018 10:40 AM

You can check your script using https://www.shellcheck.net/
As suggested you can add set -xv to help debug your script.

Your if statement as written will probably error with too many arguments due to file globing. You need to use a loop to traverse each file found in the desired directory and then check for ABC and XYZ.

As stated ftp is not recommended but the proper way to write a heredoc is
Code:

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
put $FILE
quit
END_SCRIPT



All times are GMT -5. The time now is 10:18 AM.