Passing inputs to Commands in Command line for user prompt - shell

Suppose I execute a command which prompts the user for input. Is there a way by which I can pass the input in the command itself, rather than taking the input from the user. Like for example, in cloning a private github repo I type:
!git clone https://github.com/username/repo.git
instead I want to do something like
!echo "username" | git clone https://github.com/username/repo.git.
Instead of echo can I have something which automatically inputs username when prompted, by reading it from a file or specifying it directly in my command?
Thanks for helping!

Related

automation script for git cmds

i'm trying to write a bash script that will automate some of the operation that we are doing on daily basic:
1. clone different repos
2. checkout some specific commits
3. compile
4. etc.
my challenge is, that I want this script to be used by different users and I don't want the script to prompt for password for each git cmd.
I thought to get the username + pwd as arguments to the script and then
git clone https://username:pasword#gitlab.../repoName/usename/proj.git
but I always get an error of "TCP connection reset by peer".
obviously running ssh command or https command without the username builtin working for me fine
any idea?
If you must use "https" instead "ssh" try with expect script.
#!/usr/bin/expect -f
spawn git pull
expect "Username"
send "put here your username\r"
expect "Password"
send "put here your password\r"
interact
You can use bash and expect in the same script as well.

how to answer to runtime questions with shell script and expect

When I do
git push
I get the command prompt like
Username for 'https://github.com':
then I enter my username manually like
Username for 'https://github.com': myusername
and then I hit Enter and I get prompt for my password
Password for 'https://myusername#github.com':
I want the username to be written automatically instead of manually having to type it all the time.
i tried expect but could not use it
can anyone help me to write a shell script for it?plz:)
Well here is a very small script that would do what you want
#!/bin/expect
spawn git push origin master
expect "Username for 'https://github.com': "
send "MY_NAME_IN_GITHUB"
interact
This way you just have to type your password. You can bind this script to some git command and it can take in extra parameters so that it changes your branches.
But I would advise you to create yourself ssh keys and use them. Here is a great tutorial how to do that :) ssh_tutorial and connect GH to ssh

unable to execute multiple commands in a shell script

I am new to unix and scripting, need your help for the below scenario.
These are the contents of my .sh file
#!/bin/bash
usrun xyz
whoami
When I am calling this bash file from putty its asking me for my xyz user's password some other information to properly log in the xyz user.
After successfully login, the $ sign in putty changes to #### xyz$, so I am guessing its opening a new session for the xyz user.
However, after that, the whoami command is not getting executed. Only after I type exit the whoami command is getting executed.
why is this happening? How to execute the whoami command after successful authentication of xyz user?
The #### represents the last four digits of my server to which I am currently connected to via putty.
The usrun command without any parameters blocks the execution of the bash script. Thus, until the command is not finished (when you type exit), the next command (whoami) is not executed.
If you want to login into the machine and execute a command you should try:
#!/bin/bash
usrun -u xyz whoami
The -u option allows you to specify the user and next you can provide the command to execute.
If you want to execute more than one command in the remote machine using Putty I suggest you to follow something similar to this post:
https://superuser.com/questions/1103873/how-to-type-commands-in-putty-by-creating-batch-file

Passing parameters to hg pull command using bat file

I want to create a bat file that allow me to run the hg pull command. When i run hg pull, it get some server information and then ask for "user name" and "password". i want to add user name and password automatically. Could you some one please tell me how to solve this.
Here's how you can pass credentials to command-line invocation of Mercurial:
hg pull http://.../ --config ui.username={username} --config ui.password={password}
And I recommend using environment variables to store username and password:
hg pull --config ui.username=%HG_USERNAME% --config ui.password=%HG_PASSWORD%
I have found a solution for this. What we have to do is modify the URl as follow with user input
hg pull http://UserName:Password#yourserver.com/hg/yourRepo

run command as other user in unix shell script csh

Assume I am user A(not root user) and I want to run a c-shell script which will execute command in User B(password for B is known to me) and will use the result of that command here.
How can I run a command in User-B.
sudo -u username command1
Above command may prompt for password but I want this to be done in script only.
Thanks for all your suggestions.
you could use a ssh key to allow your user A to log in as user B using user A's private key (and with user A's public key in ~B/.ssh/authorised_keys)
then you simply execute the script as B with:
ssh B#localhost "/path/to/script and maybe some arguments here"
you have following options,
run your command with root so that su - username wont prompt for password
create passordless login for user like passwordless ssh,remove password for user etc. and then run your command
for getting output here to work,
write it in a file and access it in code
store it in a variable and access it in code

Resources