LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-03-2018, 08:02 AM   #1
saphirasiva
LQ Newbie
 
Registered: May 2018
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 07-03-2018, 09:38 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by saphirasiva View Post
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.
 
1 members found this post helpful.
Old 07-03-2018, 09:43 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by saphirasiva View Post
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.
 
1 members found this post helpful.
Old 07-09-2018, 03:24 AM   #4
saphirasiva
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-09-2018, 02:18 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by saphirasiva View Post
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.
 
Old 07-11-2018, 10:11 AM   #6
saphirasiva
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-11-2018, 10:21 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Post

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.
 
1 members found this post helpful.
Old 07-11-2018, 10:40 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to check for multiple file existence and then scp to another server Iyyappan Linux - Server 2 01-24-2014 12:07 AM
[SOLVED] Check Multiple line string exists in a file Arun Shankar Programming 12 09-30-2013 07:27 AM
how to transfer multiple file to multiple server using public and private key sampadray81 Linux - Newbie 5 02-27-2012 12:31 PM
File and directory permissions for FTP server multiple users (howto) jwilliams Linux - Newbie 1 11-01-2011 09:03 PM
Multiple File Transfers FTP Client 4 Linx aaronluke Linux - Software 2 10-30-2002 09:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration