0

My file have below data

IP      USER    PASSWORD
ip1     user1   password1
ip2     user2   password2
ip3     user3   password3

So I write below script for same

#!/bin/bash

for val in  `awk 'NR>1 {print}' servers.list`
do
        echo  $val
done

But for loop is been execute 9 times and if I wrote awk command in quotes then its execute only single time.

How can I write a script so that for loop treats every line as single so that I could parse each line and can use pscpto copy data into multiple server.

Command

pscp -p mypassword /home/user/myfile.txt user@ip:/home/user/myfile.txt

If is there any better way then this to copy file into multiple server then pls specify it bcz I am new in scripting.

11
  • Why do you need to run shell loop with awk. awk can generate same output easily without any loop. Commented Jul 17, 2020 at 21:03
  • @anubhava I am learning the scripting, so if you can share you knowledge or any blog (if already been describe over there) then it will be grateful for me. BTW I need to execute pscp command too thats why I am doing this way. Commented Jul 17, 2020 at 21:05
  • Can you share an example of pscp command? Commented Jul 17, 2020 at 21:08
  • @anubhava pscp -p mypassword /home/user/myfile.txt user@ip:/home/user/myfile.txt Commented Jul 17, 2020 at 21:09
  • As a start copy/paste your code into shellcheck.net and fix the issues that it tells you about. Commented Jul 17, 2020 at 21:10

2 Answers 2

3

It sounds like what you actually need is:

while read -r ip user password; do
    pscp -p "$password" "/home/${user}/myfile.txt" "${user}@${ip}:/home/${user}/myfile.txt"
done < <(tail -n +2 servers.list)

which really has nothing to do with what you asked for at the top of your question.

The above assumes no white space within fields nor other undesirable characters in your servers.list file.

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

$ awk 'NR>1 {printf "IP --> %s, user --> %s, password --> %s\n",$1,$2,$3}' servers.list
IP --> ip1, user --> user1, password --> password1
IP --> ip2, user --> user2, password --> password2
IP --> ip3, user --> user3, password --> password3

When you want formatted output, like in your example, printf is very useful. The command format is printf fmt, expr-list where fmt is a string with placeholders, like %s, that are filled in from the values in the expression list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.