Home > Mobile >  I have to read config file and after reading it will run scp command to fetch all details from the a
I have to read config file and after reading it will run scp command to fetch all details from the a

Time:05-13

I have a config file that has details like

 #pem_file username ip destination
 ./test.pem ec2-user 00.00.00.11 /Desktop/new/
 ./test1.pem ec2-user 00.00.00.22 /Desktop/new/

Now I need to know how can I fix the below script to get all the details using scp

while read "$(cat $conf | awk  '{split($0,array,"\n")} END{print array[]}')"; do
scp -i array[1] array[2]@array[3]:/home/ubuntu/documents/xyz.xml array[4]
done

please help me.

CodePudding user response:

Build your while read like this:

#!/bin/bash

while read -r file user ip destination
do
    echo $file
    echo $user
    echo $ip
    echo $destination
    echo ""
done < <(grep -Ev "^#" "$conffile")
  • Use these variables to build your scp command.
  • The grep is to remove commented out lines.

If you prefer using an array, you can do this:

#!/bin/bash

while read -a line
do
    echo ${line[0]}
    echo ${line[1]}
    echo ${line[2]}
    echo ${line[3]}
    echo ""
done < <(grep -Ev "^#" "$conffile")

See https://mywiki.wooledge.org/BashFAQ/001 for looping on files and commands output using while.

  • Related