LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2022, 10:13 AM   #1
MsComX
LQ Newbie
 
Registered: May 2022
Posts: 2

Rep: Reputation: 0
bash script: ftp then check some conditions before copy files



Hi everybody.
I'm writing a bash script to connect to a remote server by FTP, come back to local client and do some stuff, before going back to remote server and start copying files. And all of this in one FTP session.

I want to:

1) connect to a server by ftp

Code:
/usr/bin/ftp -ivn $FTP_HOST <<EOF
user $FTP_USER $FTP_PASS
binary
EOF
2) check some conditions on local client

Code:
YES2DAY=$(date  +'%Y%m%d')
if [[ ! -d $YES2DAY ]]; then
mkdir -p $YES2DAY
cd $YES2DAY
.... and other things
fi
3) Come back to ftp session and copy files according to the stuffs I checked in 2)

Code:
cd $YES2DAY  // changing dir in remote server
mget *

The problem is that once I get connected to remote server 1) I got stucked between <<EOF and can't get out without disconnecting.

Any idea on how to implement that ????
 
Old 05-10-2022, 12:30 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
you can do all the local activities before connecting and mget.
https://blog.eduonix.com/shell-scrip...ell-scripting/
 
Old 05-10-2022, 12:42 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,721

Rep: Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914
Welcome to linuxQuestions.

Without knowing what the "and other things" are as posted there is nothing in step 2 that is dependent on step 1.

Code:
YES2DAY=$(date  +'%Y%m%d')
if [[ ! -d $YES2DAY ]]; then
  mkdir -p $YES2DAY
  cd $YES2DAY
.... and other things
fi

/usr/bin/ftp -ivn $FTP_HOST <<EOF
user $FTP_USER $FTP_PASS
binary
cd $YES2DAY 
mget *
EOF
 
Old 05-10-2022, 01:35 PM   #4
MsComX
LQ Newbie
 
Registered: May 2022
Posts: 2

Original Poster
Rep: Reputation: 0
You are right @michaelk. Let me explain it:

In the remote folder, I have many folders sctructured like this

# folder of 2022/04/10
ASPX_4T7_20220410_0000
ASPX_4T7_20220410_0030
ASPX_4T7_20220410_0100
ASPX_4T7_20220410_0130
....

# folder of 2022/04/11
ASPX_4T7_20220411_0000
ASPX_4T7_20220411_0030
ASPX_4T7_20220411_0100
ASPX_4T7_20220411_0130
...


In the "Other things", I want to tell the script:
Code:
[[ ! -f $LOCAL_DIR ]] && mkdir -p $LOCAL_DIR 2>/dev/nul

cd $LOCAL_DIR

DATE_OF_TODAY=$(date +'%Y%m%d')

# In the remote directory, split evry folder and compare it's name to TODAY folder

for f in $REMOTE_DIRECTORY/*; do

     SUFFIX2=`basename "$f"`

	if [[ $SUFFIX2 =~ ^ASPX_4T7_* ]]; then

		SUFFIX1=${SUFFIX2:0:${#SUFFIX2}-5}

		# if matches, copy all TODAY folders except BLACKLISTED
		if [[ "$SUFFIX1" == "ASPX_4T7_$DATE_OF_TODAY" ]]; then

			# .... here comes the copy of remote files excluding a balcklist array ()
		fi

	fi

done
 
Old 05-10-2022, 02:34 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,721

Rep: Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914
If the remote folder is the server then no, what you are trying to do can not accomplished using ftp.

You can create a file from the output of a ftp command then use that to create a file list array and then run a mget using that file list. It might take multiple ftp sessions.

If you have access to the remote server via ssh/sftp then it would extremely easy using the sshfs fuse filesystem because the remote server would be accessible as a local.
 
  


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
Python/Bash script to check conditions for running "poweroff" command gnuub Programming 3 12-04-2012 03:31 AM
BASH script "if then" with multiple conditions dohpaz Programming 8 05-24-2012 04:31 PM
[SOLVED] BASH script conditions roud9 Programming 5 02-27-2012 09:21 AM
LXer: if then else Conditions LXer Syndicated Linux News 0 04-15-2011 12:00 PM
[SOLVED] HOWTO: find {conditions} then create tar-ball {matching files} SaintDanBert Linux - Software 5 07-10-2010 02:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:09 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