0

I want to perform an operation where I am passing a file ( let's name it X ) in the "while loop" in an ssh command.

For an example:

ssh -n root@host "<<here I want to fit in my below while loop>>"

while read line
do
    cd $line
    cp file1 /location2/
done < X.txt

I hope you understand that I want a single line while loop here in my ssh command.

Please help me out.

Atul Dureja
  • 27
  • 10
  • Where is `X.txt` located (remote or local)? Where did you mean `cd` and `cp` have to be done? – F. Hauri - Give Up GitHub Jan 26 '18 at 15:46
  • what is the purpose of reading a line in the while loop if you're not doing anything with the line in the do? in any case, have you taken a look at this? https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/ – cptwonton Jan 26 '18 at 15:48
  • to write multiple commands in a single line, split them with a `;`. Or, save your while loop to a `script.sh` file on the host machine, and run it that way. – cptwonton Jan 26 '18 at 15:50
  • @F.Hauri , the X.txt is located at remote only. That is why I want to pass this while loop as a command in the ssh statement to run on host that I am trying to connect. – Atul Dureja Jan 26 '18 at 15:51
  • I have edited my code @cptwonton . And, now you can see the $line is a directory name. X.txt contains the list of directories. I am using $line with cd command. – Atul Dureja Jan 26 '18 at 15:52
  • @cptwonton , I have tried separating multiple commands by splitting them with ; but I how to use that in a single line while loop. I am not able to understand that. Though, saving the while loop in a script on remote host sounds good, I will give it a try. – Atul Dureja Jan 26 '18 at 15:54
  • and https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine – F. Hauri - Give Up GitHub Jan 26 '18 at 15:54
  • The method of putting my while loop in a separate script and running the same remotely actually worked. My issue is resolved. Thank you so much @cptwonton – Atul Dureja Jan 30 '18 at 11:51

0 Answers0