0

I am completely new to bash scripting. I am trying to write a bash script to copy the files from a staging to multiple production servers.

I am using three files for the same.

  1. serverPass.txt : Contains multiple server ips along with their passwords (Space separated).

  2. paths.txt : Contain paths where files need to be copied at each server mentioned in serverPass.txt

  3. copy.sh : The bash script file.

copy.sh :

#!/bin/bash
 while read SERVER PASSWORD;
 do
     while read PATH;
     do
         sshpass -p "$PASSWORD" scp * "$SERVER":"$PATH"
     done <./paths.txt  
 done <./serverPass.txt

The output that I am getting is :

sshpass: command not found

But sshpass has already been installed and working fine when used directly into the command line. What am I doing wrong?

Output of whereis and which :

dmtntimespacedev1:/usr/local/tempcheck/Test # whereis sshpass
sshpass: /usr/bin/sshpass /usr/share/man/man1/sshpass.1.gz
dmtntimespacedev1:/usr/local/tempcheck/Test # which sshpass
/usr/bin/sshpass
2
  • Post the output of whereis sshpass and/or which sshpass from your command line. Commented Jul 29, 2019 at 8:21
  • @KamilCuk Have edited the question description accordingly Commented Jul 29, 2019 at 8:24

1 Answer 1

1
while read PATH;

The PATH is a special variable. From POSIX shell manual:

PATH

A string formatted as described in the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 8, Environment Variables, used to effect command interpretation; see Command Search and Execution.

...

Command Search and Execution

.... the command shall be searched for using the PATH environment variable ....

The PATH environment variable specifies paths to search for command. Example on my system PATH is equal to:

/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/bin:/sbin:/home/kamil/bin

These are the paths my commands are in. The paths are separated by :.

When you do while read PATH you overwrite the PATH environment variable with some other string, so your command cannot be found.

Change the name of the variable. It is common to make exported variables upper case in shells. It is common to use lower case for variables in your script that are not exported. Also use -r option with read so that \ backslashes are not special and specify IFS= if you don't want to remove leading and trailing whitespaces from lines (dunno if you want to do that). Your script fixed could look like this:

#!/bin/bash
 while read -r server password; do
     while read -r path; do
         sshpass -p "$password" scp * "$server":"$path" </dev/null
     done <./paths.txt  
 done <./serverPass.txt
Sign up to request clarification or add additional context in comments.

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.