2

Solved! See below

I have a script that should do the following:

  1. Read a line from a file, the line takes the form ftp://user:pass@ftp.server.com/path/to/dir/
  2. Launch a new instance of lftp in a new window in a given GNU screen session that uploads the contents of the directory from which the script was launched, to the directory given in 1.
  3. Repeat 1-2 with the next line in the file, for however many lines there are.

Since the default working directory of the screen session may be different than the current working directory, the script must change to the current working directory first.

At the moment the script looks something like the following:

#!/bin/bash

SERVERLIST=$1
INITIAL_WD="$PWD"
SCREEN_SESSION="mysession"

if ! screen -list | grep -q "$SCREEN_SESSION"; then
    echo "Creating new screen session..."
    screen -d -m -S "$SCREEN_SESSION"
fi

while IFS='' read line || [[ -n "$line" ]] ; do
echo "Uploading to ${line}"
screen -S "$SCREEN_SESSION" -X screen 'cd $INITIAL_WD ; lftp -e "set ftp:ssl-allow false; mirror -Rvc" "$line"'
done < <(cat $SERVERLIST)

Unfortunately, this doesn't work. The script seems to execute, but the lftp command does not run... when I reattach to the screen session, there is only one window (instead of #_of_lines windows) and no uploads have been done.

dimo414
  • 47,227
  • 18
  • 148
  • 244
right2clicky
  • 785
  • 7
  • 14
  • 1
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Oct 29 '17 at 08:50
  • `cd` is a bash builtin command. – Cyrus Oct 29 '17 at 08:50
  • Ok, let's see if I get what you're saying @Cyrus ... **cd:** you seem to be saying cd can't work there, but I thought it should work, since those are keystrokes that will be executed in the new screen window (which should be a bash shell)...? **quotes:** ok, so you're saying the variable expansion won't work because of the single quotes, and I should use double quotes but then escape the double quotes inside the double quotes? – right2clicky Oct 29 '17 at 09:34
  • @Cyrus implemented fixes... it's almost working :) only, for some strange reason it executes everything in ONE screen window instead of putting each lftp instance in different windows... no idea why! It even goes and creates a second window but doesn't seem to execute there. – right2clicky Oct 29 '17 at 11:14
  • @right2clicky rather than editing your question with the resolution please [self-answer your question](https://stackoverflow.com/help/self-answer) and mark it accepted. – dimo414 Mar 28 '18 at 16:09
  • @dimo414 done, thanks for the tip – right2clicky Mar 30 '18 at 13:27

1 Answers1

0

At the moment (after some help from @Cyrus in the comments) the script looks something like the following:

#!/bin/bash

SERVERLIST=$1
INITIAL_WD="$PWD"
SCREEN_SESSION="mysession"

if ! screen -list | grep -q "$SCREEN_SESSION"; then
    echo "Creating new screen session..."
    screen -d -m -S "$SCREEN_SESSION"
fi

while IFS='' read line || [[ -n "$line" ]] ; do
echo "Uploading to ${line}"
screen -S "$SCREEN_SESSION" -X chdir "$INITIAL_WD"
screen -S "$SCREEN_SESSION" -X screen bash -c "lftp -e \"set ftp:ssl-allow false; mirror -Rvc\" \"$line\" ; exec bash"
done < <(cat $SERVERLIST)

This seems to work!

right2clicky
  • 785
  • 7
  • 14