Mowgli Mowgli - 1 year ago 222
Bash Question

Passing password during git clone from shell script

I have written a shell script to clone the project from multiple location which is sent as a file.
I read the file and pass the each location in a loop and perform my operation.

LocationFile.txt

Project1=https://enduser1@stash.my.org/my/path/testProject1.git
Project2=https://enduser2@stash.my.org/my/path/testProject2.git
Project3=https://enduser3@stash.my.org/my/path/testProject3.git
Project4=https://enduser4@stash.my.org/my/path/testProject3.git


Password.txt

Project1=password1
Project2=password2
Project3=password3
Project4=password4


I have a password file encrypted and stored in server, which I can read as part of script and passing into my script
When I run my script during clone step its prompting for a password, I tried to pass the password like below which is not working

##############
gitpassword=$password1 #retrieved from Password.txt

for xxx
do
...
...
git clone "${ProjectArrayList[1]}"
read $gitpassword
...
..
done
##############


I know I can do password less if I use ssh, which I don't want to do.
Is there a way to achieve this with my exsiting approach

Answer Source

You can use the expect and send functions to simulate user input automatically.

Here is what the syntax would be like:

expect -c 'spawn sudo git clone "${ProjectArrayList[1]}"; expect assword; send "password\n"; interact'