0

I want to create shell script by reading a file like below.

/shared36/Upload/IG/01014330122022010599/
/ftpvolume/FileUpload/01014330122022010599

/shared36/Upload/IG/01010504012023003961/
/ftpvolume/FileUpload/01010504012023003961

/shared36/Upload/IG/01011005012023007397/
/ftpvolume/FileUpload/01011005012023007397

/shared36/Upload/IG/01011403012023006138/
/ftpvolume/FileUpload/01011403012023006138

/shared36/Upload/IG/01013804012023001622/
/ftpvolume/FileUpload/01013804012023001622

I want to automate below task.

scp /ftpvolume/FileUpload/01014330122022010599/ABC.PDF   /shared36/Upload/IG/01014330122022010599/ABC.PDF

scp /ftpvolume/FileUpload/01010504012023003961/ABC.PDF   /shared36/Upload/IG/01010504012023003961/ABC.PDF

and so on....

[![enter image description here][1]][1]

My script is below

#!/usr/bin/bash
dest=""
src=""
filename="test.txt"
while read -r line; do
    name="$line"
    if [[ $name =~ /shared && $name != "" ]]
    then
    echo "$name - Destination"
    fi

    if [[ $name =~ /ftpvolume && $name != "" ]]
    then
    echo "$name - Source"
    fi
    sleep 1
done< "$filename"

but I just want to put its source and destination into scp command

2 Answers2

0

Set variables after each match. Then after you read the source, substitute them into the scp command.

#!/usr/bin/bash
dest=""
src=""
filename="test.txt"
while read -r line; do
    name="$line"
    if [[ $name =~ /shared ]]
    then
        dest=$name
    elif [[ $name =~ /ftpvolume ]]
    then
        src=$name
        scp "$src/ABC.PDF" "$dest/ABC.PDF"
    fi
    sleep 1
done< "$filename"
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That code is work for me I add some other code in existing code due to destination server is remote server and I want to check if directory is not exits in destination code will create directory first then scp command will run. please suggest. – abdul arshad Jan 19 '23 at 11:06
  • I suggest you use `rsync` instead of `scp`. It will create directories as needed. – Barmar Jan 19 '23 at 11:23
0

That code is working for me and I change some code in this due to destination server is remote server and I want to check if directory is not exist in remote server then code will create directory first and then scp command will executed. Please suggest.

New Code :

    #!/bin/sh

dest=""
src=""
filename="test.txt"
while read -r line; do
    name="$line"
    if [[ $name =~ /shared ]]
    then
        dest=$name
    elif [[ $name =~ /ftpvolume ]]
    then
        src=$name
        echo "scp $src/ABC.PDF username@X.X.X.X:$dest/ABC.PDF"
        scp "$src/ABC.PDF" "username@X.X.X.X:$dest/ABC.PDF"
    fi
    sleep 1
done< "$filename"
  • The very first two characters on the first line need to be `#` and `!` in order for the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) to work. You can't reliably use `#!/bin/sh` for scripts which use Bash syntax like `=~`; see also [Difference between `sh` and `bash`](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Jan 19 '23 at 11:18
  • And why do you copy the same value to three different variables? Just say `dest=$line` and then `scp "$line/ABC.PDF" "oracle@101.125.243.5:$dest/ABC.PDF"` – tripleee Jan 19 '23 at 11:20